001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.http.endpoints;
018:
019: import java.io.IOException;
020:
021: import javax.jbi.messaging.ExchangeStatus;
022: import javax.jbi.messaging.MessageExchange;
023: import javax.jbi.messaging.NormalizedMessage;
024: import javax.jbi.servicedesc.ServiceEndpoint;
025: import javax.xml.namespace.QName;
026:
027: import org.apache.servicemix.JbiConstants;
028: import org.apache.servicemix.common.DefaultComponent;
029: import org.apache.servicemix.common.ServiceUnit;
030: import org.apache.servicemix.common.endpoints.ProviderEndpoint;
031: import org.apache.servicemix.http.HttpComponent;
032: import org.apache.servicemix.http.HttpEndpointType;
033: import org.apache.servicemix.http.jetty.SmxHttpExchange;
034:
035: /**
036: *
037: * @author gnodet
038: * @since 3.2
039: * @org.apache.xbean.XBean element="provider"
040: */
041: public class HttpProviderEndpoint extends ProviderEndpoint implements
042: HttpEndpointType {
043:
044: //private SslParameters ssl;
045: //private BasicAuthCredentials basicAuthentication;
046: private HttpProviderMarshaler marshaler;
047: private String locationURI;
048:
049: public HttpProviderEndpoint() {
050: super ();
051: }
052:
053: public HttpProviderEndpoint(DefaultComponent component,
054: ServiceEndpoint endpoint) {
055: super (component, endpoint);
056: }
057:
058: public HttpProviderEndpoint(ServiceUnit serviceUnit, QName service,
059: String endpoint) {
060: super (serviceUnit, service, endpoint);
061: }
062:
063: public String getLocationURI() {
064: return locationURI;
065: }
066:
067: public void setLocationURI(String locationURI) {
068: this .locationURI = locationURI;
069: }
070:
071: public void start() throws Exception {
072: if (marshaler == null) {
073: DefaultHttpProviderMarshaler m = new DefaultHttpProviderMarshaler();
074: m.setLocationURI(locationURI);
075: marshaler = m;
076: }
077: super .start();
078: }
079:
080: /*
081: public BasicAuthCredentials getBasicAuthentication() {
082: return basicAuthentication;
083: }
084:
085: public void setBasicAuthentication(BasicAuthCredentials basicAuthentication) {
086: this.basicAuthentication = basicAuthentication;
087: }
088:
089: public SslParameters getSsl() {
090: return ssl;
091: }
092:
093: public void setSsl(SslParameters ssl) {
094: this.ssl = ssl;
095: }
096: */
097:
098: /**
099: * @return the marshaler
100: */
101: public HttpProviderMarshaler getMarshaler() {
102: return marshaler;
103: }
104:
105: /**
106: * @param marshaler the marshaler to set
107: */
108: public void setMarshaler(HttpProviderMarshaler marshaler) {
109: this .marshaler = marshaler;
110: }
111:
112: public void process(MessageExchange exchange) throws Exception {
113: if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
114: NormalizedMessage nm = exchange.getMessage("in");
115: if (nm == null) {
116: throw new IllegalStateException(
117: "Exchange has no input message");
118: }
119: SmxHttpExchange httpEx = new Exchange(exchange);
120: marshaler.createRequest(exchange, nm, httpEx);
121: getConnectionPool().send(httpEx);
122: }
123: }
124:
125: protected void handle(SmxHttpExchange httpExchange,
126: MessageExchange exchange) throws IOException {
127: try {
128: marshaler.handleResponse(exchange, httpExchange);
129: boolean txSync = exchange.getStatus() == ExchangeStatus.ACTIVE
130: && exchange.isTransacted()
131: && Boolean.TRUE.equals(exchange
132: .getProperty(JbiConstants.SEND_SYNC));
133: if (txSync) {
134: sendSync(exchange);
135: } else {
136: send(exchange);
137: }
138: } catch (Exception e) {
139: throw (IOException) new IOException(e.getMessage())
140: .initCause(e);
141: }
142: }
143:
144: protected org.mortbay.jetty.client.HttpClient getConnectionPool() {
145: HttpComponent comp = (HttpComponent) getServiceUnit()
146: .getComponent();
147: return comp.getConnectionPool();
148: }
149:
150: protected class Exchange extends SmxHttpExchange {
151: MessageExchange jbiExchange;
152:
153: public Exchange(MessageExchange jbiExchange) {
154: this .jbiExchange = jbiExchange;
155: }
156:
157: protected void onResponseComplete() throws IOException {
158: handle(this, jbiExchange);
159: }
160: }
161:
162: }
|