📄 httpmethodconverter.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package org.apache.ode.axis2.httpbinding;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;import org.apache.commons.httpclient.methods.DeleteMethod;import org.apache.commons.httpclient.methods.EntityEnclosingMethod;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.PutMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.methods.StringRequestEntity;import org.apache.commons.httpclient.params.HttpParams;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.ode.axis2.Properties;import org.apache.ode.axis2.util.URLEncodedTransformer;import org.apache.ode.axis2.util.UrlReplacementTransformer;import org.apache.ode.bpel.epr.MutableEndpoint;import org.apache.ode.bpel.iapi.PartnerRoleMessageExchange;import org.apache.ode.utils.DOMUtils;import org.apache.ode.utils.Namespaces;import org.apache.ode.utils.wsdl.Messages;import org.apache.ode.utils.wsdl.WsdlUtils;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import javax.wsdl.Binding;import javax.wsdl.BindingInput;import javax.wsdl.BindingOperation;import javax.wsdl.BindingOutput;import javax.wsdl.Message;import javax.wsdl.Operation;import javax.wsdl.Part;import javax.wsdl.extensions.UnknownExtensibilityElement;import javax.wsdl.extensions.http.HTTPOperation;import javax.wsdl.extensions.mime.MIMEContent;import javax.xml.namespace.QName;import java.io.UnsupportedEncodingException;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class HttpMethodConverter { private static final String CONTENT_TYPE_TEXT_XML = "text/xml"; private static final Log log = LogFactory.getLog(HttpMethodConverter.class); protected static final Messages msgs = Messages.getMessages(Messages.class); protected Binding binding; public HttpMethodConverter(Binding binding) { this.binding = binding; } public HttpMethod createHttpRequest(PartnerRoleMessageExchange odeMex, HttpParams params) throws UnsupportedEncodingException { Operation operation = odeMex.getOperation(); BindingOperation bindingOperation = binding.getBindingOperation(operation.getName(), operation.getInput().getName(), operation.getOutput().getName()); // message to be sent Element message = odeMex.getRequest().getMessage(); Message msgDef = operation.getInput().getMessage(); // base url String url = ((MutableEndpoint) odeMex.getEndpointReference()).getUrl(); // extract part values into a map and check that all parts are assigned a value Map<String, Element> partElements = extractPartElements(msgDef, message); // http method type // the operation may override the verb, this is an extension for RESTful BPEL String verb = WsdlUtils.resolveVerb(binding, bindingOperation); // build the http method itself HttpMethod method = prepareHttpMethod(bindingOperation, verb, partElements, url, params); return method; } /** * create and initialize the http method. * Http Headers that may been passed in the params are not set in this method. * Headers will be automatically set by HttpClient. * See usages of HostParams.DEFAULT_HEADERS * See org.apache.commons.httpclient.HttpMethodDirector#executeMethod(org.apache.commons.httpclient.HttpMethod) */ protected HttpMethod prepareHttpMethod(BindingOperation opBinding, String verb, Map<String, Element> partValues, final String rootUri, HttpParams params) throws UnsupportedEncodingException { if (log.isDebugEnabled()) log.debug("Preparing http request..."); // convenience variables... BindingInput bindingInput = opBinding.getBindingInput(); HTTPOperation httpOperation = (HTTPOperation) WsdlUtils.getOperationExtension(opBinding); MIMEContent content = WsdlUtils.getMimeContent(bindingInput.getExtensibilityElements()); String contentType = content == null ? "" : content.getType(); boolean useUrlEncoded = WsdlUtils.useUrlEncoded(bindingInput) || PostMethod.FORM_URL_ENCODED_CONTENT_TYPE.equalsIgnoreCase(contentType); boolean useUrlReplacement = WsdlUtils.useUrlReplacement(bindingInput); // the http method to be built and returned HttpMethod method = null; // the 4 elements the http method may be made of String relativeUri = httpOperation.getLocationURI(); String queryPath = null; RequestEntity requestEntity; String encodedParams = null; // ODE supports uri template in both port and operation location. // so assemble the final url *before* replacement String completeUri = rootUri; if (StringUtils.isNotEmpty(relativeUri)) { completeUri = completeUri + (completeUri.endsWith("/") || relativeUri.startsWith("/") ? "" : "/") + relativeUri; } if (useUrlReplacement) { // insert part values in the url completeUri = new UrlReplacementTransformer().transform(completeUri, partValues); } else if (useUrlEncoded) { // encode part values encodedParams = new URLEncodedTransformer().transform(partValues); } // http-client api is not really neat // something similar to the following would save some if/else manipulations. // But we have to deal with it as-is. // // method = new Method(verb); // method.setRequestEnity(..) // etc... if ("GET".equalsIgnoreCase(verb) || "DELETE".equalsIgnoreCase(verb)) { if ("GET".equalsIgnoreCase(verb)) { method = new GetMethod(); } else if ("DELETE".equalsIgnoreCase(verb)) { method = new DeleteMethod(); } if (useUrlEncoded) { queryPath = encodedParams; } // Let http-client manage the redirection // see org.apache.commons.httpclient.params.HttpClientParams.MAX_REDIRECTS // default is 100 method.setFollowRedirects(true); } else if ("POST".equalsIgnoreCase(verb) || "PUT".equalsIgnoreCase(verb)) { if ("POST".equalsIgnoreCase(verb)) { method = new PostMethod(); } else if ("PUT".equalsIgnoreCase(verb)) { method = new PutMethod(); } // some body-building... if (useUrlEncoded) { requestEntity = new StringRequestEntity(encodedParams, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, method.getParams().getContentCharset()); } else if (contentType.endsWith(CONTENT_TYPE_TEXT_XML)) { // get the part to be put in the body Part part = opBinding.getOperation().getInput().getMessage().getPart(content.getPart()); Element partValue = partValues.get(part.getName());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -