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: */
019: package org.apache.axis2.transport.http.util;
020:
021: import org.apache.axiom.soap.SOAPEnvelope;
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.builder.BuilderUtil;
025: import org.apache.axis2.context.MessageContext;
026: import org.apache.axis2.description.AxisBindingOperation;
027: import org.apache.axis2.description.AxisEndpoint;
028: import org.apache.axis2.description.AxisOperation;
029: import org.apache.axis2.description.AxisService;
030: import org.apache.axis2.description.WSDL2Constants;
031: import org.apache.axis2.engine.AxisEngine;
032: import org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher;
033: import org.apache.axis2.engine.Handler;
034: import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
035: import org.apache.axis2.dispatchers.RequestURIOperationDispatcher;
036: import org.apache.axis2.transport.TransportUtils;
037: import org.apache.axis2.transport.http.HTTPConstants;
038: import org.apache.axis2.transport.http.HTTPTransportUtils;
039:
040: import javax.xml.stream.XMLStreamException;
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.io.OutputStream;
044:
045: /**
046: *
047: */
048: public class RESTUtil {
049:
050: public static Handler.InvocationResponse processXMLRequest(
051: MessageContext msgContext, InputStream in,
052: OutputStream out, String contentType) throws AxisFault {
053: try {
054: msgContext.setDoingREST(true);
055: String charSetEncoding = BuilderUtil
056: .getCharSetEncoding(contentType);
057: msgContext.setProperty(
058: Constants.Configuration.CHARACTER_SET_ENCODING,
059: charSetEncoding);
060: dispatchAndVerify(msgContext);
061: in = HTTPTransportUtils.handleGZip(msgContext, in);
062: SOAPEnvelope soapEnvelope = TransportUtils
063: .createSOAPMessage(msgContext, in, contentType);
064: msgContext.setEnvelope(soapEnvelope);
065: msgContext.setProperty(
066: Constants.Configuration.CONTENT_TYPE, contentType);
067:
068: msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
069:
070: } catch (AxisFault axisFault) {
071: throw axisFault;
072: } catch (XMLStreamException e) {
073: throw AxisFault.makeFault(e);
074: } catch (IOException e) {
075: throw AxisFault.makeFault(e);
076: } finally {
077: msgContext.setProperty(
078: Constants.Configuration.MESSAGE_TYPE,
079: HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
080: }
081: return invokeAxisEngine(msgContext);
082: }
083:
084: public static Handler.InvocationResponse processURLRequest(
085: MessageContext msgContext, OutputStream out,
086: String contentType) throws AxisFault {
087: // here, only the parameters in the URI are supported. Others will be discarded.
088: try {
089:
090: if (contentType == null || "".equals(contentType)) {
091: contentType = HTTPConstants.MEDIA_TYPE_X_WWW_FORM;
092: }
093:
094: // set the required properties so that even if there is an error during the dispatch
095: // phase the response message will be passed to the client well.
096: msgContext.setDoingREST(true);
097: msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
098: String charSetEncoding = BuilderUtil
099: .getCharSetEncoding(contentType);
100: msgContext.setProperty(
101: Constants.Configuration.CHARACTER_SET_ENCODING,
102: charSetEncoding);
103: // 1. First dispatchAndVerify and find out the service and the operation.
104: dispatchAndVerify(msgContext);
105: SOAPEnvelope soapEnvelope;
106: try {
107: soapEnvelope = TransportUtils.createSOAPMessage(
108: msgContext, null, contentType);
109: } catch (XMLStreamException e) {
110: throw AxisFault.makeFault(e);
111: }
112:
113: msgContext.setEnvelope(soapEnvelope);
114:
115: } catch (AxisFault axisFault) {
116: throw axisFault;
117: } catch (IOException e) {
118: throw AxisFault.makeFault(e);
119: } finally {
120: msgContext.setProperty(
121: Constants.Configuration.MESSAGE_TYPE,
122: HTTPConstants.MEDIA_TYPE_APPLICATION_XML);
123: }
124: return invokeAxisEngine(msgContext);
125: }
126:
127: private static Handler.InvocationResponse invokeAxisEngine(
128: MessageContext messageContext) throws AxisFault {
129: AxisEngine axisEngine = new AxisEngine(messageContext
130: .getConfigurationContext());
131: return axisEngine.receive(messageContext);
132:
133: }
134:
135: private static void dispatchAndVerify(MessageContext msgContext)
136: throws AxisFault {
137: RequestURIBasedDispatcher requestDispatcher = new RequestURIBasedDispatcher();
138: requestDispatcher.invoke(msgContext);
139: AxisService axisService = msgContext.getAxisService();
140: if (axisService != null) {
141: RequestURIOperationDispatcher requestURIOperationDispatcher = new RequestURIOperationDispatcher();
142: requestURIOperationDispatcher.invoke(msgContext);
143:
144: if (msgContext.getAxisOperation() == null) {
145: HTTPLocationBasedDispatcher httpLocationBasedDispatcher = new HTTPLocationBasedDispatcher();
146: httpLocationBasedDispatcher.invoke(msgContext);
147: }
148:
149: AxisOperation axisOperation;
150: if ((axisOperation = msgContext.getAxisOperation()) != null) {
151: AxisEndpoint axisEndpoint = (AxisEndpoint) msgContext
152: .getProperty(WSDL2Constants.ENDPOINT_LOCAL_NAME);
153: if (axisEndpoint != null) {
154: AxisBindingOperation axisBindingOperation = (AxisBindingOperation) axisEndpoint
155: .getBinding().getChild(
156: axisOperation.getName());
157: msgContext.setProperty(
158: Constants.AXIS_BINDING_OPERATION,
159: axisBindingOperation);
160: }
161: msgContext.setAxisOperation(axisOperation);
162: }
163: }
164: }
165:
166: public static String getConstantFromHTTPLocation(String httpLocation) {
167: if (httpLocation.charAt(0) != '?') {
168: httpLocation = "/" + httpLocation;
169: }
170: int index = httpLocation.indexOf("{");
171: if (index > -1) {
172: httpLocation = httpLocation.substring(0, index);
173: }
174: return httpLocation;
175: }
176:
177: }
|