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