001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.util;
027:
028: import com.sun.xml.internal.ws.pept.ept.MessageInfo;
029: import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
030: import com.sun.xml.internal.ws.binding.BindingImpl;
031: import com.sun.xml.internal.ws.server.RuntimeContext;
032: import com.sun.xml.internal.ws.server.RuntimeEndpointInfo;
033: import com.sun.xml.internal.ws.spi.runtime.WSConnection;
034:
035: import javax.xml.soap.MimeHeader;
036: import javax.xml.soap.MimeHeaders;
037: import javax.xml.soap.SOAPMessage;
038: import javax.xml.transform.stream.StreamSource;
039: import javax.xml.ws.WebServiceException;
040: import javax.xml.ws.soap.SOAPBinding;
041:
042: import java.io.ByteArrayOutputStream;
043: import java.io.OutputStreamWriter;
044: import java.io.Writer;
045: import java.nio.ByteBuffer;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Locale;
051: import java.util.Map;
052:
053: import static com.sun.xml.internal.ws.developer.JAXWSProperties.CONTENT_NEGOTIATION_PROPERTY;
054:
055: /**
056: * @author WS Development Team
057: */
058: public class SOAPConnectionUtil {
059:
060: public static SOAPMessage getSOAPMessage(WSConnection con,
061: MessageInfo mi, String bindingId) {
062: try {
063: Map<String, List<String>> headers = con.getHeaders();
064: MimeHeaders mh = new MimeHeaders();
065: if (headers != null)
066: for (Map.Entry<String, List<String>> entry : headers
067: .entrySet()) {
068: String name = entry.getKey();
069: for (String value : entry.getValue()) {
070: try {
071: mh.addHeader(name, value);
072: } catch (IllegalArgumentException ie) {
073: // Not a mime header. Ignore it.
074: }
075: }
076: }
077: RuntimeContext rtCtxt = MessageInfoUtil
078: .getRuntimeContext(mi);
079: if (rtCtxt != null) {
080: RuntimeEndpointInfo endpointInfo = rtCtxt
081: .getRuntimeEndpointInfo();
082: if (bindingId == null)
083: bindingId = ((BindingImpl) endpointInfo
084: .getBinding()).getBindingId();
085: }
086: SOAPMessage soapMessage = SOAPUtil.createMessage(mh, con
087: .getInput(), bindingId);
088: return soapMessage;
089: } catch (Exception e) {
090: // e.printStackTrace();
091: throw new WebServiceException(e);
092: }
093: }
094:
095: private static void send(WSConnection con, SOAPMessage soapMessage) {
096: try {
097: if (soapMessage.saveRequired()) {
098: soapMessage.saveChanges();
099: }
100: Map<String, List<String>> headers = new HashMap<String, List<String>>();
101: MimeHeaders mhs = soapMessage.getMimeHeaders();
102: Iterator i = mhs.getAllHeaders();
103: while (i.hasNext()) {
104: MimeHeader mh = (MimeHeader) i.next();
105: String name = mh.getName();
106: List<String> values = headers.get(name);
107: if (values == null) {
108: values = new ArrayList<String>();
109: headers.put(name, values);
110: }
111: values.add(mh.getValue());
112: }
113: con.setHeaders(headers);
114: soapMessage.writeTo(con.getOutput());
115: con.closeOutput();
116: } catch (Exception e) {
117: throw new WebServiceException(e);
118: }
119: }
120:
121: public static void sendResponse(WSConnection con,
122: SOAPMessage soapMessage) {
123: //setStatus(con, WSConnection.OK);
124: send(con, soapMessage);
125: }
126:
127: public static void sendKnownError(MessageInfo messageInfo,
128: int status) {
129: WSConnection con = (WSConnection) messageInfo.getConnection();
130: setStatus(con, status);
131: con.getOutput();
132: con.closeOutput();
133: }
134:
135: public static void sendResponseOneway(MessageInfo messageInfo) {
136: WSConnection con = (WSConnection) messageInfo.getConnection();
137: setStatus(con, WSConnection.ONEWAY);
138: con.getOutput();
139:
140: // Ensure conneg is completed even if no data is sent back
141: if (messageInfo.getMetaData(CONTENT_NEGOTIATION_PROPERTY) == "optimistic") {
142: List<String> acceptList = null;
143: List<String> contentTypeList = null;
144: Map<String, List<String>> headers = con.getHeaders();
145:
146: // Go through the entries because a gets are case sensitive
147: for (Map.Entry<String, List<String>> entry : headers
148: .entrySet()) {
149: if (entry.getKey().equalsIgnoreCase("content-type")) {
150: contentTypeList = entry.getValue();
151: } else if (entry.getKey().equalsIgnoreCase("accept")) {
152: acceptList = entry.getValue();
153: }
154: }
155:
156: // If content-type is FI, FI must be listed in the accept header
157: assert contentTypeList != null && acceptList != null;
158:
159: // Use FI MIME type based on Accept header
160: contentTypeList.set(0, FastInfosetUtil
161: .getFastInfosetFromAccept(acceptList));
162: }
163:
164: con.closeOutput();
165: }
166:
167: public static void sendResponseError(WSConnection con,
168: String bindingId) {
169: try {
170: SOAPMessage message = SOAPUtil.createMessage(bindingId);
171: ByteArrayBuffer bufferedStream = new ByteArrayBuffer();
172: Writer writer = new OutputStreamWriter(bufferedStream,
173: "UTF-8");
174: if (bindingId.equals(SOAPBinding.SOAP12HTTP_BINDING))
175: writer.write(DEFAULT_SERVER_ERROR_SOAP12_ENVELOPE);
176: else
177: writer.write(DEFAULT_SERVER_ERROR_ENVELOPE);
178: writer.close();
179: message.getSOAPPart().setContent(
180: new StreamSource(bufferedStream.newInputStream()));
181: setStatus(con, WSConnection.INTERNAL_ERR);
182: send(con, message);
183: } catch (Exception e) {
184: throw new WebServiceException(e);
185: }
186: }
187:
188: public static Map<String, List<String>> getHeaders(WSConnection con) {
189: return con.getHeaders();
190: }
191:
192: /**
193: * sets response headers.
194: */
195: public static void setHeaders(WSConnection con,
196: Map<String, List<String>> headers) {
197: con.setHeaders(headers);
198: }
199:
200: public static void setStatus(WSConnection con, int status) {
201: con.setStatus(status);
202: }
203:
204: private final static String DEFAULT_SERVER_ERROR_ENVELOPE = "<?xml version='1.0' encoding='UTF-8'?>"
205: + "<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">"
206: + "<env:Body>"
207: + "<env:Fault>"
208: + "<faultcode>env:Server</faultcode>"
209: + "<faultstring>Internal server error</faultstring>"
210: + "</env:Fault>" + "</env:Body>" + "</env:Envelope>";
211:
212: private final static String DEFAULT_SERVER_ERROR_SOAP12_ENVELOPE = "<?xml version='1.0' encoding='UTF-8'?>"
213: + "<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">"
214: + "<env:Body>"
215: + "<env:Fault>"
216: + "<env:Code><env:Value>env:Receiver</env:Value></env:Code>"
217: + "<env:Reason><env:Text lang=\""
218: + Locale.getDefault().getLanguage()
219: + "\">"
220: + "Internal server error</env:Text></env:Reason>"
221: + "</env:Fault>" + "</env:Body>" + "</env:Envelope>";
222:
223: }
|