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:
020: package org.apache.axis2.transport.http;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.wsdl.WSDLConstants;
025: import org.apache.axis2.context.MessageContext;
026: import org.apache.axis2.context.OperationContext;
027: import org.apache.axis2.i18n.Messages;
028: import org.apache.axis2.transport.MessageFormatter;
029: import org.apache.axis2.transport.TransportUtils;
030: import org.apache.commons.httpclient.Header;
031: import org.apache.commons.httpclient.HttpClient;
032: import org.apache.commons.httpclient.HttpMethod;
033: import org.apache.commons.httpclient.HttpMethodBase;
034: import org.apache.commons.httpclient.HttpStatus;
035: import org.apache.commons.httpclient.HttpVersion;
036: import org.apache.commons.httpclient.methods.DeleteMethod;
037: import org.apache.commons.httpclient.methods.GetMethod;
038: import org.apache.commons.httpclient.methods.PostMethod;
039: import org.apache.commons.httpclient.methods.PutMethod;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: import java.io.IOException;
044: import java.net.URL;
045:
046: public class HTTPSender extends AbstractHTTPSender {
047:
048: private static final Log log = LogFactory.getLog(HTTPSender.class);
049:
050: public void send(MessageContext msgContext, URL url,
051: String soapActionString) throws IOException {
052:
053: // execute the HtttpMethodBase - a connection manager can be given for
054: // handle multiple
055:
056: String httpMethod = (String) msgContext
057: .getProperty(Constants.Configuration.HTTP_METHOD);
058:
059: if ((httpMethod != null)) {
060:
061: if (Constants.Configuration.HTTP_METHOD_GET
062: .equalsIgnoreCase(httpMethod)) {
063: this .sendViaGet(msgContext, url, soapActionString);
064:
065: return;
066: } else if (Constants.Configuration.HTTP_METHOD_DELETE
067: .equalsIgnoreCase(httpMethod)) {
068: this .sendViaDelete(msgContext, url, soapActionString);
069:
070: return;
071: } else if (Constants.Configuration.HTTP_METHOD_PUT
072: .equalsIgnoreCase(httpMethod)) {
073: this .sendViaPut(msgContext, url, soapActionString);
074:
075: return;
076: }
077: }
078:
079: this .sendViaPost(msgContext, url, soapActionString);
080: }
081:
082: /**
083: * Used to send a request via HTTP Get method
084: *
085: * @param msgContext - The MessageContext of the message
086: * @param url - The target URL
087: * @param soapActiionString - The soapAction string of the request
088: * @throws AxisFault - Thrown in case an exception occurs
089: */
090: private void sendViaGet(MessageContext msgContext, URL url,
091: String soapActiionString) throws AxisFault {
092:
093: GetMethod getMethod = new GetMethod();
094: HttpClient httpClient = getHttpClient(msgContext);
095: MessageFormatter messageFormatter = populateCommonProperties(
096: msgContext, url, getMethod, httpClient,
097: soapActiionString);
098:
099: // Need to have this here because we can have soap action when using the soap response MEP
100: String soapAction = messageFormatter.formatSOAPAction(
101: msgContext, format, soapActiionString);
102:
103: if (soapAction != null) {
104: getMethod.setRequestHeader(
105: HTTPConstants.HEADER_SOAP_ACTION, soapAction);
106: }
107: try {
108: executeMethod(httpClient, msgContext, url, getMethod);
109: handleResponse(msgContext, getMethod);
110: } catch (IOException e) {
111: log.info("Unable to sendViaGet to url[" + url + "]", e);
112: throw AxisFault.makeFault(e);
113: } finally {
114: cleanup(msgContext, getMethod);
115: }
116: }
117:
118: private void cleanup(MessageContext msgContext, HttpMethod method) {
119: if (msgContext
120: .isPropertyTrue(HTTPConstants.AUTO_RELEASE_CONNECTION)) {
121: method.releaseConnection();
122: }
123: }
124:
125: /**
126: * Used to send a request via HTTP Delete Method
127: *
128: * @param msgContext - The MessageContext of the message
129: * @param url - The target URL
130: * @param soapActiionString - The soapAction string of the request
131: * @throws AxisFault - Thrown in case an exception occurs
132: */
133: private void sendViaDelete(MessageContext msgContext, URL url,
134: String soapActiionString) throws AxisFault {
135:
136: DeleteMethod deleteMethod = new DeleteMethod();
137: HttpClient httpClient = getHttpClient(msgContext);
138: populateCommonProperties(msgContext, url, deleteMethod,
139: httpClient, soapActiionString);
140:
141: try {
142: executeMethod(httpClient, msgContext, url, deleteMethod);
143: handleResponse(msgContext, deleteMethod);
144: } catch (IOException e) {
145: log.info("Unable to sendViaDelete to url[" + url + "]", e);
146: throw AxisFault.makeFault(e);
147: } finally {
148: cleanup(msgContext, deleteMethod);
149: }
150: }
151:
152: /**
153: * Used to send a request via HTTP Post Method
154: *
155: * @param msgContext - The MessageContext of the message
156: * @param url - The target URL
157: * @param soapActionString - The soapAction string of the request
158: * @throws AxisFault - Thrown in case an exception occurs
159: */
160: private void sendViaPost(MessageContext msgContext, URL url,
161: String soapActionString) throws AxisFault {
162:
163: HttpClient httpClient = getHttpClient(msgContext);
164:
165: /* What's up with this, it never gets used anywhere?? --Glen
166: String charEncoding =
167: (String) msgContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
168:
169: if (charEncoding == null) {
170: charEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
171: }
172: */
173:
174: PostMethod postMethod = new PostMethod();
175: MessageFormatter messageFormatter = populateCommonProperties(
176: msgContext, url, postMethod, httpClient,
177: soapActionString);
178:
179: postMethod.setRequestEntity(new AxisRequestEntity(
180: messageFormatter, msgContext, format, soapActionString,
181: chunked, isAllowedRetry));
182:
183: if (!httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)
184: && chunked) {
185: postMethod.setContentChunked(true);
186: }
187:
188: String soapAction = messageFormatter.formatSOAPAction(
189: msgContext, format, soapActionString);
190:
191: if (soapAction != null) {
192: postMethod.setRequestHeader(
193: HTTPConstants.HEADER_SOAP_ACTION, soapAction);
194: }
195:
196: /*
197: * main excecution takes place..
198: */
199: try {
200: executeMethod(httpClient, msgContext, url, postMethod);
201: handleResponse(msgContext, postMethod);
202: } catch (IOException e) {
203: log.info("Unable to sendViaPost to url[" + url + "]", e);
204: throw AxisFault.makeFault(e);
205: } finally {
206: cleanup(msgContext, postMethod);
207: }
208: }
209:
210: /**
211: * Used to send a request via HTTP Put Method
212: *
213: * @param msgContext - The MessageContext of the message
214: * @param url - The target URL
215: * @param soapActionString - The soapAction string of the request
216: * @throws AxisFault - Thrown in case an exception occurs
217: */
218: private void sendViaPut(MessageContext msgContext, URL url,
219: String soapActionString) throws AxisFault {
220:
221: HttpClient httpClient = getHttpClient(msgContext);
222:
223: /* Same deal - this value never gets used, why is it here? --Glen
224: String charEncoding =
225: (String) msgContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
226:
227: if (charEncoding == null) {
228: charEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
229: }
230: */
231:
232: PutMethod putMethod = new PutMethod();
233: MessageFormatter messageFormatter = populateCommonProperties(
234: msgContext, url, putMethod, httpClient,
235: soapActionString);
236:
237: putMethod.setRequestEntity(new AxisRequestEntity(
238: messageFormatter, msgContext, format, soapActionString,
239: chunked, isAllowedRetry));
240:
241: if (!httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)
242: && chunked) {
243: putMethod.setContentChunked(true);
244: }
245:
246: String soapAction = messageFormatter.formatSOAPAction(
247: msgContext, format, soapActionString);
248: if (soapAction != null) {
249: putMethod.setRequestHeader(
250: HTTPConstants.HEADER_SOAP_ACTION, soapAction);
251: }
252:
253: /*
254: * main excecution takes place..
255: */
256: try {
257: executeMethod(httpClient, msgContext, url, putMethod);
258: handleResponse(msgContext, putMethod);
259: } catch (IOException e) {
260: log.info("Unable to sendViaPut to url[" + url + "]", e);
261: throw AxisFault.makeFault(e);
262: } finally {
263: cleanup(msgContext, putMethod);
264: }
265: }
266:
267: /**
268: * Used to handle the HTTP Response
269: *
270: * @param msgContext - The MessageContext of the message
271: * @param method - The HTTP method used
272: * @throws IOException - Thrown in case an exception occurs
273: */
274: private void handleResponse(MessageContext msgContext,
275: HttpMethodBase method) throws IOException {
276:
277: int statusCode = method.getStatusCode();
278: if (statusCode == HttpStatus.SC_OK) {
279: processResponse(method, msgContext);
280: } else if (statusCode == HttpStatus.SC_ACCEPTED) {
281: } else if (statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR
282: || statusCode == HttpStatus.SC_BAD_REQUEST) {
283: Header contenttypeHeader = method
284: .getResponseHeader(HTTPConstants.HEADER_CONTENT_TYPE);
285: String value = null;
286: if (contenttypeHeader != null) {
287: value = contenttypeHeader.getValue();
288: }
289: OperationContext opContext = msgContext
290: .getOperationContext();
291: if (opContext != null) {
292: MessageContext inMessageContext = opContext
293: .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
294: if (inMessageContext != null) {
295: inMessageContext.setProcessingFault(true);
296: }
297: }
298: if (value != null) {
299:
300: processResponse(method, msgContext);
301: }
302: Object isTransportNonBlocking = msgContext
303: .getProperty(MessageContext.TRANSPORT_NON_BLOCKING);
304: if (isTransportNonBlocking != null
305: && ((Boolean) isTransportNonBlocking)
306: .booleanValue()) {
307: throw new AxisFault(Messages.getMessage(
308: "transportError", String.valueOf(statusCode),
309: method.getStatusText()));
310: }
311: } else {
312: throw new AxisFault(Messages.getMessage("transportError",
313: String.valueOf(statusCode), method.getStatusText()));
314: }
315: }
316: }
|