com.github.aistomin.http.PostRequest Maven / Gradle / Ivy
/**
* Copyright (c) 2016, Istomin Andrei
*
* 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 com.github.aistomin.http;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.ServiceUnavailableRetryStrategy;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.protocol.HttpContext;
/**
* HTTP POST Request.
*
* @author Andrei Istomin ([email protected])
* @version $Id$
* @since 0.1
*/
public final class PostRequest implements HttpRequest {
/**
* Retries count on HTTP error.
*/
public static final Integer RETRY_COUNT = 20;
/**
* Retries interval in milliseconds on HTTP error.
*/
public static final Integer RETRY_INTERVAL = 100;
/**
* Request's URL.
*/
private final transient String resource;
/**
* Request's headers.
*/
private final transient Map heads;
/**
* Ctor.
*
* @param url Request's URL.
* @param headers Request's headers.
*/
public PostRequest(final String url, final Map headers) {
this.resource = url;
this.heads = headers;
}
@Override
public String execute() throws Exception {
final Request request = Request.Post(this.resource);
for (final Map.Entry item : this.heads.entrySet()) {
request.addHeader(item.getKey(), item.getValue());
}
final HttpClientBuilder builder = HttpClientBuilder.create();
builder.setRedirectStrategy(new LaxRedirectStrategy());
builder.setServiceUnavailableRetryStrategy(
new ServiceUnavailableRetryStrategy() {
public boolean retryRequest(
final HttpResponse response, final int count,
final HttpContext context
) {
return count <= PostRequest.RETRY_COUNT;
}
public long getRetryInterval() {
return PostRequest.RETRY_INTERVAL;
}
}
);
return Executor.newInstance(builder.build()).execute(request)
.returnContent().asString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy