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.camel.component.ahc;
018
019 import java.net.URI;
020
021 import com.ning.http.client.AsyncHttpClient;
022 import com.ning.http.client.AsyncHttpClientConfig;
023 import org.apache.camel.Consumer;
024 import org.apache.camel.Processor;
025 import org.apache.camel.Producer;
026 import org.apache.camel.impl.DefaultEndpoint;
027 import org.apache.camel.spi.HeaderFilterStrategy;
028 import org.apache.camel.spi.HeaderFilterStrategyAware;
029 import org.apache.camel.util.IOHelper;
030 import org.apache.camel.util.ObjectHelper;
031
032 /**
033 *
034 */
035 public class AhcEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware {
036
037 private AsyncHttpClient client;
038 private AsyncHttpClientConfig clientConfig;
039 private HeaderFilterStrategy headerFilterStrategy = new HttpHeaderFilterStrategy();
040 private AhcBinding binding;
041 private URI httpUri;
042 private boolean bridgeEndpoint;
043 private boolean throwExceptionOnFailure = true;
044 private boolean transferException;
045
046 public AhcEndpoint(String endpointUri, AhcComponent component, URI httpUri) {
047 super(endpointUri, component);
048 this.httpUri = httpUri;
049 }
050
051 @Override
052 public AhcComponent getComponent() {
053 return (AhcComponent) super.getComponent();
054 }
055
056 @Override
057 public Producer createProducer() throws Exception {
058 ObjectHelper.notNull(client, "AsyncHttpClient", this);
059 ObjectHelper.notNull(httpUri, "HttpUri", this);
060 ObjectHelper.notNull(binding, "AhcBinding", this);
061 return new AhcProducer(this);
062 }
063
064 @Override
065 public Consumer createConsumer(Processor processor) throws Exception {
066 throw new UnsupportedOperationException("This component does not support consuming from this endpoint");
067 }
068
069 @Override
070 public boolean isLenientProperties() {
071 // true to allow dynamic URI options to be configured and passed to external system for eg. the HttpProducer
072 return true;
073 }
074
075 @Override
076 public boolean isSingleton() {
077 return true;
078 }
079
080 public AsyncHttpClient getClient() {
081 return client;
082 }
083
084 public void setClient(AsyncHttpClient client) {
085 this.client = client;
086 }
087
088 public AsyncHttpClientConfig getClientConfig() {
089 return clientConfig;
090 }
091
092 public void setClientConfig(AsyncHttpClientConfig clientConfig) {
093 this.clientConfig = clientConfig;
094 }
095
096 public URI getHttpUri() {
097 return httpUri;
098 }
099
100 public void setHttpUri(URI httpUri) {
101 this.httpUri = httpUri;
102 }
103
104 public AhcBinding getBinding() {
105 return binding;
106 }
107
108 public void setBinding(AhcBinding binding) {
109 this.binding = binding;
110 }
111
112 public HeaderFilterStrategy getHeaderFilterStrategy() {
113 return headerFilterStrategy;
114 }
115
116 public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
117 this.headerFilterStrategy = headerFilterStrategy;
118 }
119
120 public boolean isBridgeEndpoint() {
121 return bridgeEndpoint;
122 }
123
124 public void setBridgeEndpoint(boolean bridgeEndpoint) {
125 this.bridgeEndpoint = bridgeEndpoint;
126 }
127
128 public boolean isThrowExceptionOnFailure() {
129 return throwExceptionOnFailure;
130 }
131
132 public void setThrowExceptionOnFailure(boolean throwExceptionOnFailure) {
133 this.throwExceptionOnFailure = throwExceptionOnFailure;
134 }
135
136 public boolean isTransferException() {
137 return transferException;
138 }
139
140 public void setTransferException(boolean transferException) {
141 this.transferException = transferException;
142 }
143
144 @Override
145 protected void doStart() throws Exception {
146 super.doStart();
147 if (client == null) {
148 if (clientConfig != null) {
149 client = new AsyncHttpClient(clientConfig);
150 } else {
151 client = new AsyncHttpClient();
152 }
153 }
154 }
155
156 @Override
157 protected void doStop() throws Exception {
158 super.doStop();
159 // ensure client is closed when stopping
160 if (client != null && !client.isClosed()) {
161 client.close();
162 }
163 client = null;
164 }
165
166 }