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 java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: import javax.xml.ws.WebServiceException;
035: import javax.xml.ws.http.HTTPException;
036: import javax.xml.soap.MimeHeader;
037: import javax.xml.soap.MimeHeaders;
038: import com.sun.xml.internal.ws.spi.runtime.WSConnection;
039: import com.sun.xml.internal.ws.pept.ept.MessageInfo;
040: import com.sun.xml.internal.ws.encoding.xml.XMLMessage;
041: import java.io.ByteArrayInputStream;
042: import java.net.HttpURLConnection;
043: import javax.xml.transform.stream.StreamSource;
044:
045: import static com.sun.xml.internal.ws.developer.JAXWSProperties.CONTENT_NEGOTIATION_PROPERTY;
046:
047: /**
048: * @author WS Development Team
049: */
050: public class XMLConnectionUtil {
051:
052: public static XMLMessage getXMLMessage(WSConnection con,
053: MessageInfo mi) {
054: try {
055: Map<String, List<String>> headers = con.getHeaders();
056: MimeHeaders mh = new MimeHeaders();
057: if (headers != null) {
058: for (Map.Entry<String, List<String>> entry : headers
059: .entrySet()) {
060: String name = entry.getKey();
061: for (String value : entry.getValue()) {
062: try {
063: mh.addHeader(name, value);
064: } catch (IllegalArgumentException ie) {
065: // Not a mime header. Ignore it.
066: }
067: }
068: }
069: }
070: return new XMLMessage(mh, con.getInput());
071: } catch (Exception e) {
072: throw (HTTPException) new HTTPException(
073: HttpURLConnection.HTTP_INTERNAL_ERROR).initCause(e);
074: }
075: }
076:
077: private static void send(WSConnection con, XMLMessage xmlMessage) {
078: try {
079: Map<String, List<String>> headers = new HashMap<String, List<String>>();
080: MimeHeaders mhs = xmlMessage.getMimeHeaders();
081: Iterator i = mhs.getAllHeaders();
082: while (i.hasNext()) {
083: MimeHeader mh = (MimeHeader) i.next();
084: String name = mh.getName();
085: List<String> values = headers.get(name);
086: if (values == null) {
087: values = new ArrayList<String>();
088: headers.put(name, values);
089: }
090: values.add(mh.getValue());
091: }
092: con.setHeaders(headers);
093: xmlMessage.writeTo(con.getOutput());
094:
095: } catch (Exception e) {
096: throw (HTTPException) new HTTPException(
097: HttpURLConnection.HTTP_INTERNAL_ERROR).initCause(e);
098: }
099: try {
100: con.closeOutput();
101: } catch (Exception e) {
102: throw (HTTPException) new HTTPException(
103: HttpURLConnection.HTTP_INTERNAL_ERROR).initCause(e);
104: }
105: }
106:
107: public static void sendResponse(WSConnection con,
108: XMLMessage xmlMessage) {
109: setStatus(con, xmlMessage.getStatus());
110: send(con, xmlMessage);
111: }
112:
113: public static void sendResponseOneway(WSConnection con) {
114: setStatus(con, WSConnection.ONEWAY);
115: con.getOutput();
116: con.closeOutput();
117: }
118:
119: public static void sendResponseError(WSConnection con,
120: MessageInfo messageInfo) {
121: try {
122: StreamSource source = new StreamSource(
123: new ByteArrayInputStream(DEFAULT_SERVER_ERROR
124: .getBytes()));
125: String conneg = (String) messageInfo
126: .getMetaData(CONTENT_NEGOTIATION_PROPERTY);
127: XMLMessage message = new XMLMessage(source,
128: conneg == "optimistic");
129: setStatus(con, WSConnection.INTERNAL_ERR);
130: send(con, message);
131: } catch (Exception e) {
132: throw new WebServiceException(e);
133: }
134: }
135:
136: public static Map<String, List<String>> getHeaders(WSConnection con) {
137: return con.getHeaders();
138: }
139:
140: /**
141: * sets response headers.
142: */
143: public static void setHeaders(WSConnection con,
144: Map<String, List<String>> headers) {
145: con.setHeaders(headers);
146: }
147:
148: public static void setStatus(WSConnection con, int status) {
149: con.setStatus(status);
150: }
151:
152: private final static String DEFAULT_SERVER_ERROR = "<?xml version='1.0' encoding='UTF-8'?>"
153: + "<err>Internal Server Error</err>";
154:
155: }
|