use of org.apache.http.RequestLine in project indy by Commonjava.
the class ProxyResponseWriter method handleEvent.
@Override
public void handleEvent(final ConduitStreamSinkChannel channel) {
HttpConduitWrapper http = new HttpConduitWrapper(channel, httpRequest, contentController, cacheProvider);
if (httpRequest == null) {
if (error != null) {
logger.debug("handling error from request reader: " + error.getMessage(), error);
handleError(error, http);
} else {
logger.debug("invalid state (no error or request) from request reader. Sending 400.");
try {
http.writeStatus(ApplicationStatus.BAD_REQUEST);
} catch (final IOException e) {
logger.error("Failed to write BAD REQUEST for missing HTTP first-line to response channel.", e);
}
}
return;
}
// TODO: Can we handle this?
final String oldThreadName = Thread.currentThread().getName();
Thread.currentThread().setName("PROXY-" + httpRequest.getRequestLine().toString());
channel.getCloseSetter().set((sinkChannel) -> {
logger.debug("sink channel closing.");
Thread.currentThread().setName(oldThreadName);
});
logger.info("\n\n\n>>>>>>> Handle write\n\n\n\n\n");
if (error == null) {
try {
final UserPass proxyUserPass = UserPass.parse(ApplicationHeader.proxy_authorization, httpRequest, null);
logger.debug("Proxy UserPass: {}\nConfig secured? {}\nConfig tracking type: {}", proxyUserPass, config.isSecured(), config.getTrackingType());
if (proxyUserPass == null && (config.isSecured() || TrackingType.ALWAYS == config.getTrackingType())) {
http.writeStatus(ApplicationStatus.PROXY_AUTHENTICATION_REQUIRED);
http.writeHeader(ApplicationHeader.proxy_authenticate, String.format(PROXY_AUTHENTICATE_FORMAT, config.getProxyRealm()));
} else {
RequestLine requestLine = httpRequest.getRequestLine();
String method = requestLine.getMethod().toUpperCase();
switch(method) {
case GET_METHOD:
case HEAD_METHOD:
{
if (proxyUserPass != null) {
logger.debug("Passing BASIC authentication credentials to Keycloak bearer-token translation authenticator");
if (!proxyAuthenticator.authenticate(proxyUserPass, http)) {
break;
}
}
final URL url = new URL(requestLine.getUri());
final RemoteRepository repo = getRepository(url);
transfer(http, repo, url.getPath(), GET_METHOD.equals(method), proxyUserPass);
break;
}
case OPTIONS_METHOD:
{
http.writeStatus(ApplicationStatus.OK);
http.writeHeader(ApplicationHeader.allow, ALLOW_HEADER_VALUE);
break;
}
default:
{
http.writeStatus(ApplicationStatus.METHOD_NOT_ALLOWED);
}
}
}
logger.info("Response complete.");
} catch (final Throwable e) {
error = e;
}
}
if (error != null) {
handleError(error, http);
}
try {
http.close();
} catch (final IOException e) {
logger.error("Failed to flush/shutdown response.", e);
}
}
use of org.apache.http.RequestLine in project cdap-ingest by caskdata.
the class StreamConfigHttpRequestHandler method handle.
@Override
public void handle(HttpRequest httpRequest, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
RequestLine requestLine = httpRequest.getRequestLine();
String method = requestLine.getMethod();
int statusCode;
if (!HttpMethod.PUT.equals(method)) {
statusCode = HttpStatus.SC_NOT_IMPLEMENTED;
} else {
String uri = requestLine.getUri();
String streamName = TestUtils.getStreamNameFromUri(uri);
if (TestUtils.SUCCESS_STREAM_NAME.equals(streamName)) {
statusCode = HttpStatus.SC_BAD_REQUEST;
BasicHttpEntityEnclosingRequest request = (BasicHttpEntityEnclosingRequest) httpRequest;
HttpEntity requestEntity = request.getEntity();
if (requestEntity != null) {
JsonObject jsonContent = RestClient.toJsonObject(requestEntity);
if (jsonContent != null) {
long ttl = jsonContent.get(TTL_ATTRIBUTE_NAME).getAsLong();
if (ttl == RestTest.STREAM_TTL) {
statusCode = HttpStatus.SC_OK;
}
}
}
} else if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
statusCode = TestUtils.authorize(httpRequest);
} else {
statusCode = TestUtils.getStatusCodeByStreamName(streamName);
}
}
response.setStatusCode(statusCode);
}
use of org.apache.http.RequestLine in project cdap-ingest by caskdata.
the class StreamHttpRequestHandler method handle.
@Override
public void handle(HttpRequest httpRequest, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
RequestLine requestLine = httpRequest.getRequestLine();
String method = requestLine.getMethod();
int statusCode;
String uri = requestLine.getUri();
String streamName = TestUtils.getStreamNameFromUri(uri + "/");
if (HttpMethod.PUT.equals(method)) {
if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
statusCode = TestUtils.authorize(httpRequest);
} else {
statusCode = TestUtils.getStatusCodeByStreamName(streamName);
}
} else if (HttpMethod.POST.equals(method)) {
String fullStreamName = streamName;
streamName = streamName.replace(TestUtils.WRITER_TEST_STREAM_NAME_POSTFIX, StringUtils.EMPTY);
if (TestUtils.AUTH_STREAM_NAME.equals(streamName)) {
statusCode = TestUtils.authorize(httpRequest);
} else if (TestUtils.WITH_CUSTOM_HEADER_STREAM_NAME.endsWith(streamName)) {
Header testHeader = httpRequest.getFirstHeader(fullStreamName + "." + RestTest.TEST_HEADER_NAME);
if (testHeader != null && RestTest.TEST_HEADER_VALUE.equals(testHeader.getValue())) {
statusCode = HttpStatus.SC_OK;
} else {
statusCode = HttpStatus.SC_BAD_REQUEST;
}
} else {
statusCode = TestUtils.getStatusCodeByStreamName(streamName);
}
if (HttpStatus.SC_OK == statusCode && !TestUtils.FILE_STREAM_NAME.equals(streamName)) {
//check request content
BasicHttpEntityEnclosingRequest request = (BasicHttpEntityEnclosingRequest) httpRequest;
HttpEntity requestEntity = request.getEntity();
if (requestEntity != null) {
String content = RestClient.toString(requestEntity);
if (!RestTest.EXPECTED_WRITER_CONTENT.equals(content) && !TestUtils.ALLOW_ANY_EVENT_STREAM.equals(streamName)) {
statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
}
} else {
statusCode = HttpStatus.SC_BAD_REQUEST;
}
}
} else {
statusCode = HttpStatus.SC_NOT_IMPLEMENTED;
}
response.setStatusCode(statusCode);
}
use of org.apache.http.RequestLine in project elasticsearch by elastic.
the class FailureTrackingResponseListenerTests method mockResponse.
private static Response mockResponse() {
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
RequestLine requestLine = new BasicRequestLine("GET", "/", protocolVersion);
StatusLine statusLine = new BasicStatusLine(protocolVersion, 200, "OK");
HttpResponse httpResponse = new BasicHttpResponse(statusLine);
return new Response(requestLine, new HttpHost("localhost", 9200), httpResponse);
}
use of org.apache.http.RequestLine in project okhttp by square.
the class OkApacheClient method transformRequest.
private static Request transformRequest(HttpRequest request) {
Request.Builder builder = new Request.Builder();
RequestLine requestLine = request.getRequestLine();
String method = requestLine.getMethod();
builder.url(requestLine.getUri());
String contentType = null;
for (Header header : request.getAllHeaders()) {
String name = header.getName();
if ("Content-Type".equalsIgnoreCase(name)) {
contentType = header.getValue();
} else {
builder.header(name, header.getValue());
}
}
RequestBody body = null;
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
// Wrap the entity in a custom Body which takes care of the content, length, and type.
body = new HttpEntityBody(entity, contentType);
Header encoding = entity.getContentEncoding();
if (encoding != null) {
builder.header(encoding.getName(), encoding.getValue());
}
} else {
body = Util.EMPTY_REQUEST;
}
}
builder.method(method, body);
return builder.build();
}
Aggregations