org.camunda.connect.httpclient.impl.AbstractHttpConnector Maven / Gradle / Ivy
/* Licensed 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.camunda.connect.httpclient.impl;
import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.Map;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.camunda.connect.httpclient.HttpBaseRequest;
import org.camunda.connect.httpclient.HttpResponse;
import org.camunda.connect.impl.AbstractConnector;
public abstract class AbstractHttpConnector, R extends HttpResponse> extends AbstractConnector {
protected static HttpConnectorLogger LOG = HttpLogger.HTTP_LOGGER;
protected CloseableHttpClient httpClient;
public AbstractHttpConnector(String connectorId) {
super(connectorId);
httpClient = createClient();
}
protected CloseableHttpClient createClient() {
return HttpClients.createSystem();
}
public CloseableHttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}
@Override
public R execute(Q request) {
HttpRequestBase httpRequest = createHttpRequest(request);
HttpRequestInvocation invocation = new HttpRequestInvocation(httpRequest, request, requestInterceptors, httpClient);
try {
return createResponse((CloseableHttpResponse) invocation.proceed());
} catch (Exception e) {
throw LOG.unableToExecuteRequest(e);
}
}
protected abstract R createResponse(CloseableHttpResponse response);
@Override
public abstract Q createRequest();
/**
* creates a apache Http* representation of the request.
*
* @param request the given request
* @return {@link HttpRequestBase} an apache representation of the request
*/
protected T createHttpRequest(Q request) {
T httpRequest = createHttpRequestBase(request);
applyHeaders(httpRequest, request.getHeaders());
applyPayload(httpRequest, request);
return httpRequest;
}
@SuppressWarnings("unchecked")
protected T createHttpRequestBase(Q request) {
String url = request.getUrl();
if (url != null && !url.trim().isEmpty()) {
String method = request.getMethod();
if (HttpGet.METHOD_NAME.equals(method)) {
return (T) new HttpGet(url);
} else if (HttpPost.METHOD_NAME.equals(method)) {
return (T) new HttpPost(url);
} else if (HttpPut.METHOD_NAME.equals(method)) {
return (T) new HttpPut(url);
} else if (HttpDelete.METHOD_NAME.equals(method)) {
return (T) new HttpDelete(url);
} else if (HttpPatch.METHOD_NAME.equals(method)) {
return (T) new HttpPatch(url);
} else if (HttpHead.METHOD_NAME.equals(method)) {
return (T) new HttpHead(url);
} else if (HttpOptions.METHOD_NAME.equals(method)) {
return (T) new HttpOptions(url);
} else if (HttpTrace.METHOD_NAME.equals(method)) {
return (T) new HttpTrace(url);
} else {
throw LOG.unknownHttpMethod(method);
}
}
else {
throw LOG.requestUrlRequired();
}
}
protected void applyHeaders(T httpRequest, Map headers) {
if (headers != null) {
for (Map.Entry entry : headers.entrySet()) {
httpRequest.setHeader(entry.getKey(), entry.getValue());
LOG.setHeader(entry.getKey(), entry.getValue());
}
}
}
protected void applyPayload(T httpRequest, Q request) {
if (httpMethodSupportsPayload(httpRequest)) {
if (request.getPayload() != null) {
ByteArrayInputStream payload = new ByteArrayInputStream(request.getPayload().getBytes(Charset.forName("utf-8")));
((HttpEntityEnclosingRequestBase) httpRequest).setEntity(new InputStreamEntity(payload));
}
}
else if (request.getPayload() != null) {
LOG.payloadIgnoredForHttpMethod(request.getMethod());
}
}
protected boolean httpMethodSupportsPayload(T httpRequest) {
return httpRequest instanceof HttpEntityEnclosingRequestBase;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy