1 package org.codehaus.xfire.type.mtom;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.Map;
7
8 import javax.activation.DataContentHandler;
9 import javax.activation.DataHandler;
10 import javax.activation.URLDataSource;
11
12 import org.codehaus.xfire.MessageContext;
13 import org.codehaus.xfire.attachments.Attachment;
14 import org.codehaus.xfire.attachments.Attachments;
15 import org.codehaus.xfire.attachments.SimpleAttachment;
16 import org.codehaus.xfire.fault.XFireFault;
17 import org.codehaus.xfire.message.MessageReader;
18 import org.codehaus.xfire.message.MessageWriter;
19 import org.codehaus.xfire.type.Type;
20
21 /***
22 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
23 */
24 public abstract class XOPType
25 extends Type
26 {
27 public final static String XOP_NS = "http://www.w3.org/2004/08/xop/include";
28 public final static String XML_MIME_NS = "http://www.w3.org/2004/11/xmlmime";
29
30 private static MTOMDataContentHandlerFactory factory =
31 new MTOMDataContentHandlerFactory();
32
33 private Map classToCType;
34 private Map CTypeToClass;
35
36 public XOPType()
37 {
38 }
39
40 public Object readObject(MessageReader reader, MessageContext context)
41 throws XFireFault
42 {
43 String uri = getURI(reader.getValue(null, "href"));
44 String contentType = reader.getValue(XML_MIME_NS, "contentType");
45
46 Attachment att = getAttachment( uri, context );
47
48 String type = att.getDataHandler().getContentType();
49 DataContentHandler handler = factory.createDataContentHandler(type);
50 try
51 {
52 if ( handler != null )
53 {
54 return handler.getContent(att.getDataHandler().getDataSource());
55 }
56 else
57 {
58 return att.getDataHandler().getContent();
59 }
60 }
61 catch (IOException e)
62 {
63 throw new XFireFault("Could not read the attachment " + uri, e, XFireFault.SENDER);
64 }
65 }
66
67 /***
68 * Parse the URI from the <code>xop:Include</code> href value.
69 * @param value
70 * @return
71 */
72 protected String getURI(String value)
73 {
74 int index = value.indexOf(":");
75 return value.substring(index+1);
76 }
77
78 public Attachment getAttachment(String id, MessageContext context)
79 throws XFireFault
80 {
81 Attachments attachments =
82 (Attachments) context.getProperty(Attachments.ATTACHMENTS_KEY);
83 Attachment att = null;
84
85 if ( attachments != null)
86 {
87 att = attachments.getPart(id);
88 }
89
90
91 try
92 {
93 URLDataSource source = new URLDataSource(new URL(id));
94 att = new SimpleAttachment(id, new DataHandler(source));
95 }
96 catch (MalformedURLException e)
97 {
98 throw new XFireFault("Invalid attachment id: " + id, e, XFireFault.SENDER);
99 }
100
101 return att;
102 }
103
104 public void writeObject(Object object, MessageWriter writer, MessageContext context)
105 throws XFireFault
106 {
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 throw new UnsupportedOperationException();
126 }
127
128
129 }