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: package com.sun.xml.internal.ws.handler;
026:
027: import com.sun.net.httpserver.HttpExchange;
028: import com.sun.xml.internal.ws.developer.JAXWSProperties;
029: import com.sun.xml.internal.ws.encoding.soap.internal.AttachmentBlock;
030: import com.sun.xml.internal.ws.util.ByteArrayDataSource;
031:
032: import static com.sun.xml.internal.ws.handler.HandlerChainCaller.IGNORE_FAULT_PROPERTY;
033:
034: import java.util.List;
035: import java.util.Map;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import javax.xml.ws.handler.MessageContext;
039: import javax.xml.ws.handler.MessageContext.Scope;
040: import javax.xml.namespace.QName;
041: import javax.xml.soap.AttachmentPart;
042: import javax.xml.soap.SOAPException;
043: import javax.activation.DataHandler;
044:
045: /**
046: * Utility to manipulate MessageContext properties
047: *
048: * @author WS Development Team
049: */
050: public class MessageContextUtil {
051:
052: public static Integer getHttpStatusCode(MessageContext ctxt) {
053: return (Integer) ctxt.get(MessageContext.HTTP_RESPONSE_CODE);
054: }
055:
056: public static void setHttpStatusCode(MessageContext ctxt,
057: Integer code) {
058: ctxt.put(MessageContext.HTTP_RESPONSE_CODE, code);
059: ctxt.setScope(MessageContext.HTTP_RESPONSE_CODE,
060: Scope.APPLICATION);
061: }
062:
063: public static void setQueryString(MessageContext ctxt,
064: String queryString) {
065: ctxt.put(MessageContext.QUERY_STRING, queryString);
066: ctxt.setScope(MessageContext.QUERY_STRING, Scope.APPLICATION);
067: }
068:
069: public static void setPathInfo(MessageContext ctxt, String pathInfo) {
070: ctxt.put(MessageContext.PATH_INFO, pathInfo);
071: ctxt.setScope(MessageContext.PATH_INFO, Scope.APPLICATION);
072: }
073:
074: public static void setHttpExchange(MessageContext ctxt,
075: HttpExchange exch) {
076: ctxt.put(JAXWSProperties.HTTP_EXCHANGE, exch);
077: ctxt.setScope(JAXWSProperties.HTTP_EXCHANGE, Scope.APPLICATION);
078: }
079:
080: public static HttpExchange getHttpExchange(MessageContext ctxt) {
081: return (HttpExchange) ctxt.get(JAXWSProperties.HTTP_EXCHANGE);
082: }
083:
084: public static void setHttpRequestMethod(MessageContext ctxt,
085: String method) {
086: ctxt.put(MessageContext.HTTP_REQUEST_METHOD, method);
087: ctxt.setScope(MessageContext.HTTP_REQUEST_METHOD,
088: Scope.APPLICATION);
089: }
090:
091: public static void setHttpRequestHeaders(MessageContext ctxt,
092: Map<String, List<String>> headers) {
093: ctxt.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
094: ctxt.setScope(MessageContext.HTTP_REQUEST_HEADERS,
095: Scope.APPLICATION);
096: }
097:
098: public static void setHttpResponseHeaders(MessageContext ctxt,
099: Map<String, List<String>> headers) {
100: ctxt.put(MessageContext.HTTP_RESPONSE_HEADERS, headers);
101: ctxt.setScope(MessageContext.HTTP_RESPONSE_HEADERS,
102: Scope.APPLICATION);
103: }
104:
105: public static Map<String, List<String>> getHttpResponseHeaders(
106: MessageContext ctxt) {
107: return (Map<String, List<String>>) ctxt
108: .get(MessageContext.HTTP_RESPONSE_HEADERS);
109: }
110:
111: public static void setWsdlOperation(MessageContext ctxt, QName name) {
112: ctxt.put(MessageContext.WSDL_OPERATION, name);
113: ctxt.setScope(MessageContext.WSDL_OPERATION, Scope.APPLICATION);
114: }
115:
116: private static Map<String, DataHandler> getMessageAttachments(
117: MessageContext ctxt) {
118: String property = MessageContext.INBOUND_MESSAGE_ATTACHMENTS;
119: Boolean out = (Boolean) ctxt
120: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
121: if (out != null && out) {
122: property = MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS;
123: }
124:
125: Object att = ctxt.get(property);
126: if (att == null) {
127: Map<String, DataHandler> attMap = new HashMap<String, DataHandler>();
128: ctxt.put(property, attMap);
129: ctxt.setScope(property, Scope.APPLICATION);
130: return attMap;
131: }
132: return (Map<String, DataHandler>) att;
133: }
134:
135: public static void copyInboundMessageAttachments(
136: MessageContext ctxt, Iterator<AttachmentPart> attachments)
137: throws SOAPException {
138: Map<String, DataHandler> attachMap = getMessageAttachments(ctxt);
139: while (attachments.hasNext()) {
140: AttachmentPart ap = attachments.next();
141: DataHandler dh = new DataHandler(new ByteArrayDataSource(ap
142: .getRawContentBytes(), ap.getContentType()));
143: attachMap.put(ap.getContentId(), dh);
144: }
145: }
146:
147: public static void addMessageAttachment(MessageContext ctxt,
148: String cid, DataHandler dh) {
149: Map<String, DataHandler> attachMap = getMessageAttachments(ctxt);
150: attachMap.put(cid, dh);
151: }
152:
153: /*
154: * See HandlerChainCaller for full details. When a ProtocolException
155: * is caught from the handler chain, this method is used to tell
156: * the runtime whether to use the fault in the current message or
157: * use the exception and create a new message.
158: */
159: public static boolean ignoreFaultInMessage(MessageContext context) {
160: if (context.get(IGNORE_FAULT_PROPERTY) == null) {
161: return false;
162: }
163: return (Boolean) context.get(IGNORE_FAULT_PROPERTY);
164: }
165:
166: }
|