Merge branch 'temp-jetty9' of https://git-wip-us.apache.org/repos/asf/camel into temp-jetty9
Conflicts: components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelHttpClient.java components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteAliasTest.java Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cf0618d9 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cf0618d9 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cf0618d9 Branch: refs/heads/temp-jetty9 Commit: cf0618d9b989dfed40bd5ce3ec4bd737c893c7e8 Parents: 95b16c5 0e076c0 Author: Christian Schneider <ch...@die-schneider.net> Authored: Fri Dec 12 18:23:23 2014 +0100 Committer: Christian Schneider <ch...@die-schneider.net> Committed: Fri Dec 12 18:23:23 2014 +0100 ---------------------------------------------------------------------- .../org/apache/camel/component/jetty/JettyContentExchange.java | 2 ++ 1 file changed, 2 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cf0618d9/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java ---------------------------------------------------------------------- diff --cc components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java index 046caa8,212fde9..ac6fbfb --- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java +++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java @@@ -1,46 -1,254 +1,48 @@@ -/** - * 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.camel.component.jetty; + import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; + import java.net.MalformedURLException; import java.util.Collection; import java.util.Map; -import java.util.TreeMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import org.apache.camel.AsyncCallback; -import org.apache.camel.CamelExchangeException; -import org.apache.camel.Exchange; -import org.apache.camel.ExchangeTimedOutException; import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.client.api.Request; -import org.eclipse.jetty.client.api.Response; -import org.eclipse.jetty.client.api.Result; -import org.eclipse.jetty.client.util.BufferingResponseListener; -import org.eclipse.jetty.client.util.BytesContentProvider; -import org.eclipse.jetty.client.util.InputStreamContentProvider; -import org.eclipse.jetty.client.util.StringContentProvider; -import org.eclipse.jetty.http.HttpFields; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Jetty specific exchange which keeps track of the the request and response. - * - * @version - */ -public class JettyContentExchange { - - private static final Logger LOG = LoggerFactory.getLogger(JettyContentExchange.class); - - private volatile Exchange exchange; - private volatile AsyncCallback callback; - private volatile JettyHttpBinding jettyBinding; - private volatile HttpClient client; - private final CountDownLatch done = new CountDownLatch(1); - private Request request; - private Response response; - private byte[] responseContent; - - private String requestContentType; - - private boolean supportRedirect; - - public JettyContentExchange(Exchange exchange, JettyHttpBinding jettyBinding, - final HttpClient client) { - super(); // keep headers by default - this.exchange = exchange; - this.jettyBinding = jettyBinding; - this.client = client; - } - - public void setCallback(AsyncCallback callback) { - this.callback = callback; - } - - protected void onRequestComplete() { - LOG.trace("onRequestComplete"); - closeRequestContentSource(); - } - - protected void onResponseComplete(Result result, byte[] content, String contentType) { - LOG.trace("onResponseComplete"); - done.countDown(); - this.response = result.getResponse(); - this.responseContent = content; - if (callback == null) { - // this is only for the async callback - return; - } - try { - jettyBinding.populateResponse(exchange, this); - } catch (Exception e) { - exchange.setException(e); - } finally { - callback.done(false); - } - } - - protected void onExpire() { - LOG.trace("onExpire"); - - // need to close the request input stream - closeRequestContentSource(); - doTaskCompleted(new ExchangeTimedOutException(exchange, client.getConnectTimeout())); - } - - protected void onException(Throwable ex) { - LOG.trace("onException {}", ex); - - // need to close the request input stream - closeRequestContentSource(); - doTaskCompleted(ex); - } - - protected void onConnectionFailed(Throwable ex) { - LOG.trace("onConnectionFailed {}", ex); - - // need to close the request input stream - closeRequestContentSource(); - doTaskCompleted(ex); - } - - public byte[] getBody() { - // must return the content as raw bytes - return getResponseContentBytes(); - } - - public String getUrl() { - try { - return this.request.getURI().toURL().toExternalForm(); - } catch (MalformedURLException e) { - throw new IllegalStateException(e.getMessage(), e); - } - } - - protected void closeRequestContentSource() { - tryClose(this.request.getContent()); - } - - private void tryClose(Object obj) { - if (obj instanceof Closeable) { - try { - ((Closeable)obj).close(); - } catch (IOException e) { - // Ignore - } - } - } - - protected void doTaskCompleted(Throwable ex) { - if (ex instanceof TimeoutException) { - exchange.setException(new ExchangeTimedOutException(exchange, request.getTimeout())); - } else { - exchange.setException(new CamelExchangeException("JettyClient failed cause by: " + ex.getMessage(), exchange, ex)); - } - done.countDown(); - - if (callback != null) { - // now invoke callback to indicate we are done async - callback.done(false); - } - } - - public void setRequestContentType(String contentType) { - this.requestContentType = contentType; - } - - public int getResponseStatus() { - return this.response.getStatus(); - } - - public void setMethod(String method) { - this.request.method(method); - } - - public void setTimeout(long timeout) { - this.request.timeout(timeout, TimeUnit.MILLISECONDS); - } - - public void setURL(String url) { - this.request = client.newRequest(url); - } - - public void setRequestContent(byte[] byteArray) { - this.request.content(new BytesContentProvider(byteArray), this.requestContentType); - } - - public void setRequestContent(String data, String charset) throws UnsupportedEncodingException { - StringContentProvider cp = charset != null ? new StringContentProvider(data, charset) : new StringContentProvider(data); - this.request.content(cp, this.requestContentType); - } - public void setRequestContent(InputStream ins) { - this.request.content(new InputStreamContentProvider(ins), this.requestContentType); - } - - public void addRequestHeader(String key, String s) { - this.request.header(key, s); - } - - public void send(HttpClient client) throws IOException { - org.eclipse.jetty.client.api.Request.Listener listener = new Request.Listener.Adapter() { - - @Override - public void onSuccess(Request request) { - onRequestComplete(); - } - - @Override - public void onFailure(Request request, Throwable failure) { - onConnectionFailed(failure); - } - - }; - BufferingResponseListener responseListener = new BufferingResponseListener() { - - @Override - public void onComplete(Result result) { - if (result.isFailed()) { - doTaskCompleted(result.getFailure()); - } else { - onResponseComplete(result, getContent(), getMediaType()); - } - } - }; - request.followRedirects(supportRedirect).listener(listener).send(responseListener); - } - - protected void setResponse(Response response) { - this.response = response; - } - - public byte[] getResponseContentBytes() { - return responseContent; - } - - public Map<String, Collection<String>> getResponseHeaders() { - final HttpFields f = response.getHeaders(); - Map<String, Collection<String>> ret = new TreeMap<String, Collection<String>>(String.CASE_INSENSITIVE_ORDER); - for (String n : f.getFieldNamesCollection()) { - ret.put(n, f.getValuesList(n)); - } - return ret; - } - - public void setSupportRedirect(boolean supportRedirect) { - this.supportRedirect = supportRedirect; - } + +public interface JettyContentExchange { + + void setCallback(AsyncCallback callback); + + byte[] getBody(); + + String getUrl(); + + void setRequestContentType(String contentType); + + int getResponseStatus(); + + void setMethod(String method); + + void setTimeout(long timeout); + + void setURL(String url); + + void setRequestContent(byte[] byteArray); + + void setRequestContent(String data, String charset) throws UnsupportedEncodingException; + + void setRequestContent(InputStream ins); + + void addRequestHeader(String key, String s); + + void send(HttpClient client) throws IOException; + + byte[] getResponseContentBytes(); + + Map<String, Collection<String>> getResponseHeaders(); + + void setSupportRedirect(boolean supportRedirect); }