📄 httpexternalservice.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.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;import org.apache.commons.httpclient.URIException;import org.apache.commons.httpclient.params.HttpParams;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.ode.axis2.ExternalService;import org.apache.ode.axis2.ODEService;import org.apache.ode.axis2.Properties;import org.apache.ode.bpel.epr.EndpointFactory;import org.apache.ode.bpel.epr.WSAEndpoint;import org.apache.ode.bpel.iapi.BpelServer;import org.apache.ode.bpel.iapi.EndpointReference;import org.apache.ode.bpel.iapi.Message;import org.apache.ode.bpel.iapi.MessageExchange;import org.apache.ode.bpel.iapi.PartnerRoleMessageExchange;import org.apache.ode.bpel.iapi.ProcessConf;import org.apache.ode.bpel.iapi.Scheduler;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 javax.wsdl.Binding;import javax.wsdl.BindingOperation;import javax.wsdl.Definition;import javax.wsdl.Fault;import javax.wsdl.Operation;import javax.wsdl.Part;import javax.wsdl.Port;import javax.wsdl.Service;import javax.wsdl.extensions.mime.MIMEContent;import javax.xml.namespace.QName;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.Map;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;/** * @author <a href="mailto:midon@intalio.com">Alexis Midon</a> */public class HttpExternalService implements ExternalService { private static final Log log = LogFactory.getLog(HttpExternalService.class); private static final Messages msgs = Messages.getMessages(Messages.class); private MultiThreadedHttpConnectionManager connections; protected ExecutorService executorService; protected Scheduler scheduler; protected BpelServer server; protected ProcessConf pconf; private String targetNamespace; protected QName serviceName; protected String portName; protected WSAEndpoint endpointReference; protected HttpMethodConverter httpMethodConverter; protected Binding portBinding; public HttpExternalService(ProcessConf pconf, QName serviceName, String portName, ExecutorService executorService, Scheduler scheduler, BpelServer server) { this.portName = portName; this.serviceName = serviceName; this.executorService = executorService; this.scheduler = scheduler; this.server = server; this.pconf = pconf; Definition definition = pconf.getDefinitionForService(serviceName); targetNamespace = definition.getTargetNamespace(); Service serviceDef = definition.getService(serviceName); if (serviceDef == null) throw new IllegalArgumentException(msgs.msgServiceDefinitionNotFound(serviceName)); Port port = serviceDef.getPort(portName); if (port == null) throw new IllegalArgumentException(msgs.msgPortDefinitionNotFound(serviceName, portName)); portBinding = port.getBinding(); if (portBinding == null) throw new IllegalArgumentException(msgs.msgBindingNotFound(portName)); // validate the http binding if (!WsdlUtils.useHTTPBinding(port)) { throw new IllegalArgumentException(msgs.msgNoHTTPBindingForPort(portName)); } // throws an IllegalArgumentException if not valid // TODO re-enable this validation step// new HttpBindingValidator(this.portBinding).validate(); // initial endpoint reference Element eprElmt = ODEService.genEPRfromWSDL(definition, serviceName, portName); if (eprElmt == null) throw new IllegalArgumentException(msgs.msgPortDefinitionNotFound(serviceName, portName)); endpointReference = EndpointFactory.convertToWSA(ODEService.createServiceRef(eprElmt)); httpMethodConverter = new HttpMethodConverter(this.portBinding); connections = new MultiThreadedHttpConnectionManager(); } public String getPortName() { return portName; } public QName getServiceName() { return serviceName; } public void close() { connections.shutdown(); } public EndpointReference getInitialEndpointReference() { return endpointReference; } public void invoke(PartnerRoleMessageExchange odeMex) { if (log.isDebugEnabled()) log.debug("Preparing " + getClass().getSimpleName() + " invocation..."); try { // note: don't make this map an instance attribute, so we always get the latest version final Map<String, String> properties = pconf.getEndpointProperties(endpointReference); final HttpParams params = Properties.HttpClient.translate(properties); // build the http method final HttpMethod method = httpMethodConverter.createHttpRequest(odeMex, params); // create a client HttpClient client = new HttpClient(connections); // don't forget to wire params so that EPR properties are passed around client.getParams().setDefaults(params); // configure the client (proxy, security, etc) HttpClientHelper.configure(client.getHostConfiguration(), client.getState(), method.getURI(), params); // this callable encapsulates the http method execution and the process of the response final Callable executionCallable; // execute it boolean isTwoWay = odeMex.getMessageExchangePattern() == MessageExchange.MessageExchangePattern.REQUEST_RESPONSE; if (isTwoWay) { // two way executionCallable = new HttpExternalService.TwoWayCallable(client, method, odeMex.getMessageExchangeId(), odeMex.getOperation()); scheduler.registerSynchronizer(new Scheduler.Synchronizer() { public void afterCompletion(boolean success) { // If the TX is rolled back, then we don't send the request. if (!success) return; // The invocation must happen in a separate thread executorService.submit(executionCallable); } public void beforeCompletion() { } }); odeMex.replyAsync(); } else { // one way, just execute and forget executionCallable = new HttpExternalService.OneWayCallable(client, method, odeMex.getMessageExchangeId(), odeMex.getOperation()); executorService.submit(executionCallable); odeMex.replyOneWayOk(); } } catch (UnsupportedEncodingException e) { String errmsg = "The HTTP encoding returned isn't supported " + odeMex; log.error(errmsg, e); odeMex.replyWithFailure(MessageExchange.FailureType.FORMAT_ERROR, errmsg, null); } catch (URIException e) { String errmsg = "Error sending message to " + getClass().getSimpleName() + " for ODE mex " + odeMex; log.error(errmsg, e); odeMex.replyWithFailure(MessageExchange.FailureType.FORMAT_ERROR, errmsg, null); } catch (Exception e) { String errmsg = "Unknown HTTP call error for ODE mex " + odeMex; log.error(errmsg, e); odeMex.replyWithFailure(MessageExchange.FailureType.OTHER, errmsg, null); } } private class OneWayCallable implements Callable<Void> { HttpMethod method; String mexId; Operation operation; HttpClient client; public OneWayCallable(HttpClient client, HttpMethod method, String mexId, Operation operation) { this.method = method; this.mexId = mexId; this.operation = operation; this.client = client; } public Void call() throws Exception { try { // simply execute the http method if (log.isDebugEnabled()) log.debug("Executing http request : " + method.getName() + " " + method.getURI()); final int statusCode = client.executeMethod(method); // invoke getResponseBody to force the loading of the body // Actually the processResponse may happen in a separate thread and // as a result the connection might be closed before the body processing (see the finally clause below). byte[] responseBody = method.getResponseBody(); // ... and process the response
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -