001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.provider;
020:
021: import java.awt.*;
022: import java.awt.image.BufferedImage;
023: import java.io.OutputStream;
024: import java.io.StringReader;
025: import java.io.StringWriter;
026: import java.util.Iterator;
027:
028: import javax.imageio.IIOImage;
029: import javax.imageio.ImageWriter;
030: import javax.imageio.stream.ImageOutputStream;
031: import javax.xml.soap.MessageFactory;
032: import javax.xml.soap.SOAPConstants;
033: import javax.xml.soap.SOAPException;
034: import javax.xml.soap.SOAPMessage;
035: import javax.xml.transform.Result;
036: import javax.xml.transform.Source;
037: import javax.xml.transform.Transformer;
038: import javax.xml.transform.TransformerFactory;
039: import javax.xml.transform.stream.StreamResult;
040: import javax.xml.transform.stream.StreamSource;
041:
042: /**
043: * This will serve as a helper class for attachments utility methods. All methods
044: * are intended to be referenced staticly.
045: *
046: */
047: public class AttachmentUtil {
048: public static final String SOAP11_NAMESPACE = "http://schemas.xmlsoap.org/soap/envelope";
049: public static final String SOAP12_NAMESPACE = "http://www.w3.org/2003/05/soap-envelope";
050:
051: public static final String MU_TEXT = "soap message mustUnderstand header request";
052: public static final String UNDERSTOOD_MU_TEXT = "understood headers soap message mustUnderstand header request";
053: public static final String TEXT = "soap message request";
054: public static final String VALUE = "value";
055: public static final String VALUE_NODE = "<" + VALUE + ">";
056: public static final String VALUE_NODE_SLASH = "</" + VALUE + ">";
057: public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
058: public static final String MUHEADER_CLIENT = "ns1:muclient";
059: public static final String MUHEADER_SERVER = "ns1:muserver";
060: public static final String MUHEADER_CLIENT_UNDERSTOOD = "ns1:muclientunderstood";
061: public static final String MUHEADER_SERVER_UNDERSTOOD = "ns1:muserverunderstood";
062: public static final String msgEnvMU = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
063: + "<soapenv:Header>"
064: + "<"
065: + MUHEADER_CLIENT
066: + " xmlns:ns1=\"http://ws.apache.org/axis2\" soapenv:mustUnderstand=\"1\">MUinfo</"
067: + MUHEADER_CLIENT
068: + ">"
069: + "</soapenv:Header>"
070: + "<soapenv:Body>"
071: + "<ns1:invoke xmlns:ns1=\"http://ws.apache.org/axis2\">"
072: + VALUE_NODE
073: + MU_TEXT
074: + VALUE_NODE_SLASH
075: + "</ns1:invoke>"
076: + "</soapenv:Body>" + "</soapenv:Envelope>";
077:
078: public static final String msgEnv = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
079: + "<soapenv:Body>"
080: + "<ns1:invoke xmlns:ns1=\"http://ws.apache.org/axis2\">"
081: + VALUE_NODE
082: + MU_TEXT
083: + VALUE_NODE_SLASH
084: + "</ns1:invoke>"
085: + "</soapenv:Body>" + "</soapenv:Envelope>";
086:
087: public static final String msgEnvPlain = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
088: + "<soapenv:Body>"
089: + "<ns1:invoke xmlns:ns1=\"http://ws.apache.org/axis2\">"
090: + VALUE_NODE
091: + TEXT
092: + VALUE_NODE_SLASH
093: + "</ns1:invoke>"
094: + "</soapenv:Body>" + "</soapenv:Envelope>";
095:
096: public static final String msgEnvMU_understood = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
097: + "<soapenv:Header>"
098: + "<"
099: + MUHEADER_CLIENT_UNDERSTOOD
100: + " xmlns:ns1=\"http://ws.apache.org/axis2\" soapenv:mustUnderstand=\"1\">MUinfo</"
101: + MUHEADER_CLIENT_UNDERSTOOD
102: + ">"
103: + "</soapenv:Header>"
104: + "<soapenv:Body>"
105: + "<ns1:invoke xmlns:ns1=\"http://ws.apache.org/axis2\">"
106: + VALUE_NODE
107: + UNDERSTOOD_MU_TEXT
108: + VALUE_NODE_SLASH
109: + "</ns1:invoke>"
110: + "</soapenv:Body>"
111: + "</soapenv:Envelope>";
112:
113: public static final String msgEnv_understood = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
114: + "<soapenv:Body>"
115: + "<ns1:invoke xmlns:ns1=\"http://ws.apache.org/axis2\">"
116: + VALUE_NODE
117: + UNDERSTOOD_MU_TEXT
118: + VALUE_NODE_SLASH
119: + "</ns1:invoke>"
120: + "</soapenv:Body>"
121: + "</soapenv:Envelope>";
122:
123: /**
124: * Store a given image to an Image output stream
125: * @param mimeType
126: * @param image
127: * @param os
128: * @throws Exception
129: */
130: public static void storeImage(String mimeType, Image image,
131: OutputStream os) throws Exception {
132: ImageWriter imageWriter = null;
133: BufferedImage bufferedImage = (BufferedImage) image;
134:
135: Iterator iterator = javax.imageio.ImageIO
136: .getImageWritersByMIMEType(mimeType);
137: if (iterator.hasNext()) {
138: imageWriter = (ImageWriter) iterator.next();
139: }
140: ImageOutputStream ios = javax.imageio.ImageIO
141: .createImageOutputStream(os);
142: imageWriter.setOutput(ios);
143:
144: imageWriter.write(new IIOImage(bufferedImage, null, null));
145: ios.flush();
146: imageWriter.dispose();
147: }
148:
149: /**
150: * Adapter method used to convert any type of Source to a String
151: *
152: * @param input
153: * @return
154: */
155: public static String toString(Source input) {
156:
157: if (input == null)
158: return null;
159:
160: StringWriter writer = new StringWriter();
161: Transformer trasformer;
162: try {
163: trasformer = TransformerFactory.newInstance()
164: .newTransformer();
165: Result result = new StreamResult(writer);
166: trasformer.transform(input, result);
167: } catch (Exception e) {
168: return null;
169: }
170:
171: return writer.getBuffer().toString();
172: }
173:
174: /**
175: * Adapter method used to convert any type of SOAPMessage to a String
176: *
177: * @param input
178: * @return
179: */
180: public static String toString(SOAPMessage input) {
181:
182: if (input == null)
183: return null;
184:
185: Source result = null;
186: try {
187: result = input.getSOAPPart().getContent();
188: } catch (SOAPException e) {
189: e.printStackTrace();
190: }
191:
192: return toString(result);
193: }
194:
195: /**
196: * Method used to convert Strings to SOAPMessages
197: *
198: * @param msgString
199: * @return
200: */
201: public static SOAPMessage toSOAPMessage(String msgString) {
202:
203: if (msgString == null)
204: return null;
205:
206: SOAPMessage message = null;
207: try {
208: MessageFactory factory = null;
209:
210: // Force the usage of specific MesasgeFactories
211: if (msgString.indexOf(SOAP11_NAMESPACE) >= 0) {
212: factory = MessageFactory
213: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
214: } else {
215: factory = MessageFactory
216: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
217: }
218: message = factory.createMessage();
219: message.getSOAPPart().setContent(
220: (Source) new StreamSource(new StringReader(
221: msgString)));
222: message.saveChanges();
223: } catch (SOAPException e) {
224: System.out.println("toSOAPMessage Exception encountered: "
225: + e);
226: e.printStackTrace();
227: }
228: return message;
229: }
230:
231: }
|