1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.axis2.transport.http.util;
21
22 import org.apache.axiom.soap.SOAPEnvelope;
23 import org.apache.axis2.AxisFault;
24 import org.apache.axis2.Constants;
25 import org.apache.axis2.builder.BuilderUtil;
26 import org.apache.axis2.context.MessageContext;
27 import org.apache.axis2.description.AxisBindingOperation;
28 import org.apache.axis2.description.AxisEndpoint;
29 import org.apache.axis2.description.AxisOperation;
30 import org.apache.axis2.description.AxisService;
31 import org.apache.axis2.description.WSDL2Constants;
32 import org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher;
33 import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
34 import org.apache.axis2.dispatchers.RequestURIOperationDispatcher;
35 import org.apache.axis2.engine.AxisEngine;
36 import org.apache.axis2.engine.Handler;
37 import org.apache.axis2.transport.TransportUtils;
38 import org.apache.axis2.transport.http.HTTPConstants;
39 import org.apache.axis2.transport.http.HTTPTransportUtils;
40
41 import javax.xml.stream.XMLStreamException;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.OutputStream;
45
46 /**
47 *
48 */
49 public class RESTUtil {
50
51 public static Handler.InvocationResponse processXMLRequest(MessageContext msgContext,
52 InputStream in,
53 OutputStream out, String contentType)
54 throws AxisFault {
55 try {
56 msgContext.setDoingREST(true);
57 String charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
58 msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEncoding);
59 dispatchAndVerify(msgContext);
60 in = HTTPTransportUtils.handleGZip(msgContext, in);
61 SOAPEnvelope soapEnvelope = TransportUtils
62 .createSOAPMessage(msgContext, in, contentType);
63 msgContext.setEnvelope(soapEnvelope);
64 msgContext.setProperty(Constants.Configuration.CONTENT_TYPE,
65 contentType);
66
67 msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
68
69 } catch (AxisFault axisFault) {
70 throw axisFault;
71 } catch (XMLStreamException e) {
72 throw AxisFault.makeFault(e);
73 } catch (IOException e) {
74 throw AxisFault.makeFault(e);
75 } finally {
76 String messageType =
77 (String) msgContext.getProperty(Constants.Configuration.MESSAGE_TYPE);
78 if (HTTPConstants.MEDIA_TYPE_X_WWW_FORM.equals(messageType) ||
79 HTTPConstants.MEDIA_TYPE_MULTIPART_FORM_DATA.equals(messageType)) {
80 msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE,
81 HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
82 }
83 }
84 return invokeAxisEngine(msgContext);
85 }
86
87 public static Handler.InvocationResponse processURLRequest(MessageContext msgContext,
88 OutputStream out, String contentType)
89 throws AxisFault {
90 // here, only the parameters in the URI are supported. Others will be discarded.
91 try {
92
93 if (contentType == null || "".equals(contentType)) {
94 contentType = HTTPConstants.MEDIA_TYPE_X_WWW_FORM;
95 }
96
97 // set the required properties so that even if there is an error during the dispatch
98 // phase the response message will be passed to the client well.
99 msgContext.setDoingREST(true);
100 msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
101 String charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
102 msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEncoding);
103 // 1. First dispatchAndVerify and find out the service and the operation.
104 dispatchAndVerify(msgContext);
105 SOAPEnvelope soapEnvelope;
106 try {
107 soapEnvelope = TransportUtils
108 .createSOAPMessage(msgContext, null, contentType);
109 } catch (XMLStreamException e) {
110 throw AxisFault.makeFault(e);
111 }
112
113 msgContext.setEnvelope(soapEnvelope);
114
115
116 } catch (AxisFault axisFault) {
117 throw axisFault;
118 }
119 catch (IOException e) {
120 throw AxisFault.makeFault(e);
121 } finally {
122 String messageType =
123 (String) msgContext.getProperty(Constants.Configuration.MESSAGE_TYPE);
124 if (HTTPConstants.MEDIA_TYPE_X_WWW_FORM.equals(messageType) ||
125 HTTPConstants.MEDIA_TYPE_MULTIPART_FORM_DATA.equals(messageType)) {
126 msgContext.setProperty(Constants.Configuration.MESSAGE_TYPE,
127 HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
128 }
129 }
130 return invokeAxisEngine(msgContext);
131 }
132
133 private static Handler.InvocationResponse invokeAxisEngine(MessageContext messageContext)
134 throws AxisFault {
135 return AxisEngine.receive(messageContext);
136
137 }
138
139 private static void dispatchAndVerify(MessageContext msgContext) throws AxisFault {
140 RequestURIBasedDispatcher requestDispatcher = new RequestURIBasedDispatcher();
141 requestDispatcher.invoke(msgContext);
142 AxisService axisService = msgContext.getAxisService();
143 if (axisService != null) {
144 HTTPLocationBasedDispatcher httpLocationBasedDispatcher =
145 new HTTPLocationBasedDispatcher();
146 httpLocationBasedDispatcher.invoke(msgContext);
147 if (msgContext.getAxisOperation() == null) {
148 RequestURIOperationDispatcher requestURIOperationDispatcher =
149 new RequestURIOperationDispatcher();
150 requestURIOperationDispatcher.invoke(msgContext);
151 }
152
153 AxisOperation axisOperation;
154 if ((axisOperation = msgContext.getAxisOperation()) != null) {
155 AxisEndpoint axisEndpoint =
156 (AxisEndpoint) msgContext.getProperty(WSDL2Constants.ENDPOINT_LOCAL_NAME);
157 if (axisEndpoint != null) {
158 AxisBindingOperation axisBindingOperation = (AxisBindingOperation) axisEndpoint
159 .getBinding().getChild(axisOperation.getName());
160 msgContext.setProperty(Constants.AXIS_BINDING_OPERATION, axisBindingOperation);
161 }
162 msgContext.setAxisOperation(axisOperation);
163 }
164 }
165 }
166
167
168 }
169
170