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: */package org.apache.cxf.jaxws.support;
019:
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.LinkedHashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.activation.DataHandler;
029: import javax.xml.ws.BindingProvider;
030: import javax.xml.ws.handler.MessageContext;
031: import javax.xml.ws.handler.MessageContext.Scope;
032:
033: import org.apache.cxf.binding.soap.SoapMessage;
034: import org.apache.cxf.configuration.security.AuthorizationPolicy;
035: import org.apache.cxf.endpoint.Endpoint;
036: import org.apache.cxf.headers.Header;
037: import org.apache.cxf.helpers.CastUtils;
038: import org.apache.cxf.jaxws.context.WrappedMessageContext;
039: import org.apache.cxf.message.Attachment;
040: import org.apache.cxf.message.Exchange;
041: import org.apache.cxf.message.Message;
042:
043: // Do some context mapping work from rt-core to jaxws standard.
044: // NOTE if there are changes in cxf Message property names, this
045: // class should be updated.
046:
047: public final class ContextPropertiesMapping {
048:
049: private static Map<String, String> cxf2jaxwsMap = new HashMap<String, String>();
050: private static Map<String, String> jaxws2cxfMap = new HashMap<String, String>();
051:
052: static {
053: cxf2jaxwsMap.put(Message.ENDPOINT_ADDRESS,
054: BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
055: cxf2jaxwsMap.put(Message.MAINTAIN_SESSION,
056: BindingProvider.SESSION_MAINTAIN_PROPERTY);
057:
058: cxf2jaxwsMap.put(Message.HTTP_REQUEST_METHOD,
059: MessageContext.HTTP_REQUEST_METHOD);
060: cxf2jaxwsMap.put(Message.RESPONSE_CODE,
061: MessageContext.HTTP_RESPONSE_CODE);
062: cxf2jaxwsMap.put(Message.PATH_INFO, MessageContext.PATH_INFO);
063: cxf2jaxwsMap.put(Message.QUERY_STRING,
064: MessageContext.QUERY_STRING);
065: cxf2jaxwsMap
066: .put("HTTP.REQUEST", MessageContext.SERVLET_REQUEST);
067: cxf2jaxwsMap.put("HTTP.RESPONSE",
068: MessageContext.SERVLET_RESPONSE);
069: cxf2jaxwsMap
070: .put("HTTP.CONTEXT", MessageContext.SERVLET_CONTEXT);
071:
072: jaxws2cxfMap.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
073: Message.ENDPOINT_ADDRESS);
074: jaxws2cxfMap.put(BindingProvider.SESSION_MAINTAIN_PROPERTY,
075: Message.MAINTAIN_SESSION);
076:
077: jaxws2cxfMap.put(MessageContext.HTTP_REQUEST_METHOD,
078: Message.HTTP_REQUEST_METHOD);
079: jaxws2cxfMap.put(MessageContext.HTTP_RESPONSE_CODE,
080: Message.RESPONSE_CODE);
081: jaxws2cxfMap.put(MessageContext.PATH_INFO, Message.PATH_INFO);
082: jaxws2cxfMap.put(MessageContext.QUERY_STRING,
083: Message.QUERY_STRING);
084:
085: jaxws2cxfMap
086: .put(MessageContext.SERVLET_REQUEST, "HTTP.REQUEST");
087: jaxws2cxfMap.put(MessageContext.SERVLET_RESPONSE,
088: "HTTP.RESPONSE");
089: jaxws2cxfMap
090: .put(MessageContext.SERVLET_CONTEXT, "HTTP.CONTEXT");
091: }
092:
093: private ContextPropertiesMapping() {
094:
095: }
096:
097: private static void mapContext(Map<String, Object> context,
098: Map<String, String> map) {
099: for (Map.Entry<String, String> entry : map.entrySet()) {
100: Object o = context.get(entry.getKey());
101: if (o != null) {
102: context.put(entry.getValue(), o);
103: }
104: }
105: }
106:
107: public static void mapRequestfromJaxws2Cxf(
108: Map<String, Object> context) {
109: //deal with PROTOCOL_HEADERS mapping
110: Object requestHeaders = context
111: .get(MessageContext.HTTP_REQUEST_HEADERS);
112: if (null != requestHeaders) {
113: context.put(Message.PROTOCOL_HEADERS, requestHeaders);
114: }
115: mapJaxws2Cxf(context);
116: }
117:
118: public static void mapResponsefromCxf2Jaxws(
119: Map<String, Object> context) {
120: //deal with PROTOCOL_HEADERS mapping
121: Object responseHeaders = context.get(Message.PROTOCOL_HEADERS);
122: if (null != responseHeaders) {
123: context.put(MessageContext.HTTP_RESPONSE_HEADERS,
124: responseHeaders);
125: }
126: mapContext(context, cxf2jaxwsMap);
127: }
128:
129: private static void mapJaxws2Cxf(Map<String, Object> context) {
130: mapContext(context, jaxws2cxfMap);
131: if (context.containsKey(BindingProvider.USERNAME_PROPERTY)) {
132: AuthorizationPolicy authPolicy = new AuthorizationPolicy();
133: authPolicy.setUserName((String) context
134: .get(BindingProvider.USERNAME_PROPERTY));
135: authPolicy.setPassword((String) context
136: .get(BindingProvider.PASSWORD_PROPERTY));
137: context
138: .put(AuthorizationPolicy.class.getName(),
139: authPolicy);
140: }
141: }
142:
143: private static void mapCxf2Jaxws(WrappedMessageContext context) {
144:
145: for (Map.Entry<String, String> entry : cxf2jaxwsMap.entrySet()) {
146: Object o = context.get(entry.getKey());
147: if (o != null) {
148: context.put(entry.getValue(), o, Scope.APPLICATION);
149: } else if (context.containsKey(entry.getValue())) {
150: context.put(entry.getValue(), context.get(entry
151: .getValue()), Scope.APPLICATION);
152: }
153: }
154:
155: if (context.containsKey(AuthorizationPolicy.class.getName())) {
156: AuthorizationPolicy authPolicy = (AuthorizationPolicy) context
157: .get(AuthorizationPolicy.class.getName());
158: context.put(BindingProvider.USERNAME_PROPERTY, authPolicy
159: .getUserName(), Scope.APPLICATION);
160: context.put(BindingProvider.PASSWORD_PROPERTY, authPolicy
161: .getPassword(), Scope.APPLICATION);
162: }
163: }
164:
165: public static MessageContext createWebServiceContext(
166: Exchange exchange) {
167: WrappedMessageContext ctx = new WrappedMessageContext(exchange
168: .getInMessage(), Scope.APPLICATION);
169: mapCxf2Jaxws(exchange, ctx, false);
170: return ctx;
171: }
172:
173: public static void mapCxf2Jaxws(Exchange exchange,
174: WrappedMessageContext ctx, boolean requestor) {
175:
176: ctx.put(Message.WSDL_DESCRIPTION, exchange
177: .get(Message.WSDL_DESCRIPTION));
178: ctx.put(Message.WSDL_INTERFACE, exchange
179: .get(Message.WSDL_INTERFACE));
180: ctx.put(Message.WSDL_OPERATION, exchange
181: .get(Message.WSDL_OPERATION));
182: ctx.put(Message.WSDL_PORT, exchange.get(Message.WSDL_PORT));
183: ctx.put(Message.WSDL_SERVICE, exchange
184: .get(Message.WSDL_SERVICE));
185:
186: mapCxf2Jaxws(ctx);
187: Message inMessage = exchange.getInMessage();
188: Message outMessage = exchange.getOutMessage();
189:
190: if (inMessage == null
191: && Boolean.TRUE
192: .equals(ctx.get(Message.INBOUND_MESSAGE))) {
193: //inbound partial responses and stuff are not set in the exchange
194: inMessage = ctx.getWrappedMessage();
195: }
196:
197: if (inMessage != null) {
198: addMessageAttachments(ctx, inMessage,
199: MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
200:
201: Object inHeaders = inMessage.get(Message.PROTOCOL_HEADERS);
202: if (null != inHeaders) {
203: if (requestor) {
204: ctx.put(MessageContext.HTTP_RESPONSE_HEADERS,
205: inHeaders, Scope.APPLICATION);
206: } else {
207: ctx.put(MessageContext.HTTP_REQUEST_HEADERS,
208: inHeaders, Scope.APPLICATION);
209: }
210:
211: outMessage = exchange.getOutMessage();
212: if (outMessage == null) {
213: Endpoint ep = exchange.get(Endpoint.class);
214: outMessage = ep.getBinding().createMessage();
215: exchange.setOutMessage(outMessage);
216: }
217: }
218:
219: }
220:
221: if (outMessage != null) {
222: addMessageAttachments(ctx, outMessage,
223: MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
224: Object outHeaders = outMessage
225: .get(Message.PROTOCOL_HEADERS);
226:
227: if (outHeaders != null && !requestor) {
228: ctx.put(MessageContext.HTTP_REQUEST_HEADERS,
229: outHeaders, Scope.APPLICATION);
230: }
231: }
232:
233: }
234:
235: private static void addMessageAttachments(
236: WrappedMessageContext ctx, Message message,
237: String propertyName) {
238: Map<String, DataHandler> dataHandlers = null;
239:
240: Collection<Attachment> attachments = message.getAttachments();
241: if (attachments != null) {
242:
243: //preserve the order of iteration
244: dataHandlers = new LinkedHashMap<String, DataHandler>();
245: for (Attachment attachment : attachments) {
246: dataHandlers.put(attachment.getId(), attachment
247: .getDataHandler());
248: }
249: }
250:
251: ctx.put(propertyName,
252: dataHandlers == null ? Collections.EMPTY_MAP
253: : Collections.unmodifiableMap(dataHandlers),
254: Scope.APPLICATION);
255: }
256:
257: public static void updateWebServiceContext(Exchange exchange,
258: MessageContext ctx) {
259: //get the context response code and setback to out message
260: if (ctx.containsKey(MessageContext.HTTP_RESPONSE_CODE)) {
261: exchange.getOutMessage().put(Message.RESPONSE_CODE,
262: ctx.get(MessageContext.HTTP_RESPONSE_CODE));
263: }
264:
265: // Guard against wrong type associated with header list.
266: // Need to copy header only if the message is going out.
267: if (ctx.containsKey(Header.HEADER_LIST)
268: && ctx.get(Header.HEADER_LIST) instanceof List<?>
269: && exchange.getOutMessage() instanceof SoapMessage) {
270: SoapMessage sm = (SoapMessage) exchange.getOutMessage();
271: Iterator iter = ((List) ctx.get(Header.HEADER_LIST))
272: .iterator();
273: while (iter.hasNext()) {
274: sm.getHeaders().add((Header) iter.next());
275: }
276: }
277: if (ctx.containsKey(MessageContext.HTTP_RESPONSE_HEADERS)) {
278: Map<String, List<String>> other = CastUtils
279: .cast((Map<?, ?>) ctx
280: .get(MessageContext.HTTP_RESPONSE_HEADERS));
281: Map<String, List<String>> heads = CastUtils
282: .cast((Map<?, ?>) exchange.getOutMessage().get(
283: Message.PROTOCOL_HEADERS));
284: if (heads != null) {
285: heads.putAll(other);
286: } else if (!other.isEmpty()) {
287: exchange.getOutMessage().put(Message.PROTOCOL_HEADERS,
288: ctx.get(MessageContext.HTTP_RESPONSE_HEADERS));
289: }
290: }
291: }
292:
293: }
|