use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class StompFrame method encodeHeader.
public static Ascii encodeHeader(String value) {
if (value == null)
return null;
try {
byte[] data = value.getBytes("UTF-8");
Buffer rc = new Buffer(data.length);
for (byte d : data) {
switch(d) {
case ESCAPE_BYTE:
rc.appendBuffer(ESCAPE_ESCAPE_SEQ.toBuffer());
break;
case COLON_BYTE:
rc.appendBuffer(COLON_ESCAPE_SEQ.toBuffer());
break;
case NEWLINE_BYTE:
rc.appendBuffer(COLON_ESCAPE_SEQ.toBuffer());
break;
default:
rc.appendByte(d);
}
}
return ascii(rc);
} catch (UnsupportedEncodingException e) {
// not expected.
throw new RuntimeException(e);
}
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class StompProtocolDecoder method read_text_body.
private Action<StompFrame> read_text_body(final StompFrame frame) {
return new Action<StompFrame>() {
public StompFrame apply() throws IOException {
Buffer content = readUntil((byte) 0);
if (content != null) {
nextDecodeAction = read_action;
frame.content(chomp(content));
return frame;
} else {
return null;
}
}
};
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class StompProtocolDecoder method read_headers.
private Action<StompFrame> read_headers(final StompFrame frame) {
final Ascii[] contentLengthValue = new Ascii[1];
final ArrayList<StompFrame.HeaderEntry> headers = new ArrayList<StompFrame.HeaderEntry>(10);
return new Action<StompFrame>() {
public StompFrame apply() throws IOException {
Buffer line = readUntil((byte) '\n', protocol.maxHeaderLength, "The maximum header length was exceeded");
while (line != null) {
line = chomp(line);
if (line.length() > 0) {
if (protocol.maxHeaders != -1 && headers.size() > protocol.maxHeaders) {
throw new IOException("The maximum number of headers was exceeded");
}
try {
int seperatorIndex = indexOf(line, COLON_BYTE);
if (seperatorIndex < 0) {
throw new IOException("Header line missing separator [" + Ascii.ascii(line) + "]");
}
Buffer name = line.getBuffer(0, seperatorIndex);
if (trim) {
name = trim(name);
}
Buffer value = line.getBuffer(seperatorIndex + 1, line.length());
if (trim) {
value = trim(value);
}
StompFrame.HeaderEntry entry = new StompFrame.HeaderEntry(Ascii.ascii(name), Ascii.ascii(value));
if (entry.key.equals(CONTENT_LENGTH)) {
contentLengthValue[0] = entry.value;
}
headers.add(entry);
} catch (Exception e) {
throw new IOException("Unable to parser header line [" + line + "]");
}
} else {
frame.setHeaders(headers);
Ascii contentLength = contentLengthValue[0];
if (contentLength != null) {
// Bless the client, he's telling us how much data to read in.
int length = 0;
try {
length = Integer.parseInt(contentLength.toString());
} catch (NumberFormatException e) {
throw new IOException("Specified content-length is not a valid integer");
}
if (protocol.maxDataLength != -1 && length > protocol.maxDataLength) {
throw new IOException("The maximum data length was exceeded");
}
nextDecodeAction = read_binary_body(frame, length);
} else {
nextDecodeAction = read_text_body(frame);
}
return nextDecodeAction.apply();
}
line = readUntil((byte) '\n', protocol.maxHeaderLength, "The maximum header length was exceeded");
}
return null;
}
};
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class HttpGatewayHandler method doRouteRequest.
protected void doRouteRequest(Map<String, MappedServices> mappingRules, final HttpServerRequest request) {
String uri = request.uri();
String uri2 = uri;
if (addMissingTrailingSlashes) {
uri2 = normalizeUri(uri);
}
HttpClient client = null;
String remaining = null;
String prefix = null;
String proxyServiceUrl = null;
String reverseServiceUrl = null;
MappedServices mappedServices = null;
URL clientURL = null;
Set<Map.Entry<String, MappedServices>> entries = mappingRules.entrySet();
for (Map.Entry<String, MappedServices> entry : entries) {
String path = entry.getKey();
mappedServices = entry.getValue();
String pathPrefix = path;
boolean uriMatches = uri.startsWith(pathPrefix);
boolean uri2Matches = uri2 != null && uri2.startsWith(pathPrefix);
if (uriMatches || uri2Matches) {
int pathPrefixLength = pathPrefix.length();
if (uri2Matches && pathPrefixLength < uri2.length()) {
remaining = uri2.substring(pathPrefixLength);
} else if (pathPrefixLength < uri.length()) {
remaining = uri.substring(pathPrefixLength);
} else {
remaining = null;
}
// now lets pick a service for this path
proxyServiceUrl = mappedServices.chooseService(request);
if (proxyServiceUrl != null) {
// lets create a client for this request...
try {
clientURL = new URL(proxyServiceUrl);
client = createClient(clientURL);
prefix = clientURL.getPath();
reverseServiceUrl = request.absoluteURI().resolve(pathPrefix).toString();
if (reverseServiceUrl.endsWith("/")) {
reverseServiceUrl = reverseServiceUrl.substring(0, reverseServiceUrl.length() - 1);
}
break;
} catch (MalformedURLException e) {
LOG.warn("Failed to parse URL: " + proxyServiceUrl + ". " + e, e);
}
}
}
}
if (client != null) {
String servicePath = prefix != null ? prefix : "";
// we should usually end the prefix path with a slash for web apps at least
if (servicePath.length() > 0 && !servicePath.endsWith("/")) {
servicePath += "/";
}
if (remaining != null) {
servicePath += remaining;
}
client.exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable throwable) {
if (throwable instanceof ConnectTimeoutException) {
request.response().setStatusCode(504);
request.response().end();
} else {
LOG.error("Unhandled exception", throwable);
}
}
});
client.setConnectTimeout(connectionTimeout);
LOG.info("Proxying request {} to service path: {} on service: {} reverseServiceUrl: {}", uri, servicePath, proxyServiceUrl, reverseServiceUrl);
final HttpClient finalClient = client;
Handler<HttpClientResponse> responseHandler = new Handler<HttpClientResponse>() {
public void handle(HttpClientResponse clientResponse) {
LOG.debug("Proxying response: {}", clientResponse.statusCode());
request.response().setStatusCode(clientResponse.statusCode());
request.response().headers().set(clientResponse.headers());
applyChunkedEncoding(request.response());
clientResponse.dataHandler(new Handler<Buffer>() {
public void handle(Buffer data) {
LOG.debug("Proxying response body: {}", data);
request.response().write(data);
}
});
clientResponse.endHandler(new VoidHandler() {
public void handle() {
request.response().end();
finalClient.close();
}
});
}
};
if (mappedServices != null) {
ProxyMappingDetails proxyMappingDetails = new ProxyMappingDetails(proxyServiceUrl, reverseServiceUrl, servicePath);
responseHandler = mappedServices.wrapResponseHandlerInPolicies(request, responseHandler, proxyMappingDetails);
}
final HttpClientRequest clientRequest = client.request(request.method(), servicePath, responseHandler);
clientRequest.headers().set(request.headers());
clientRequest.setChunked(true);
if (requestTimeout != DEFAULT_REQUEST_TIMEOUT) {
clientRequest.exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable throwable) {
if (throwable instanceof TimeoutException) {
request.response().setStatusCode(504);
request.response().end();
} else {
LOG.error("Unhandled exception", throwable);
}
}
});
clientRequest.setTimeout(requestTimeout);
}
request.dataHandler(new Handler<Buffer>() {
public void handle(Buffer data) {
LOG.debug("Proxying request body: {}", data);
clientRequest.write(data);
}
});
request.endHandler(new VoidHandler() {
public void handle() {
LOG.debug("end of the request");
clientRequest.end();
}
});
} else {
// lets return a 404
LOG.info("Could not find matching proxy path for {} from paths: {}", uri, mappingRules.keySet());
request.response().setStatusCode(404);
request.response().end();
}
}
use of org.vertx.java.core.buffer.Buffer in project fabric8 by jboss-fuse.
the class HttpGatewayHandler method doReturnIndex.
protected void doReturnIndex(HttpServerRequest request, Map<String, MappedServices> mappingRules) throws IOException {
String json = mappingRulesToJson(mappingRules);
HttpServerResponse response = request.response();
response.headers().set(CONTENT_TYPE, "application/json");
if ("HEAD".equals(request.method())) {
// For HEAD method you must not return message body but must send 'Content-Length' header
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
String contentLength = String.valueOf(new Buffer(json).length());
response.putHeader(CONTENT_LENGTH, contentLength).end();
} else {
response.end(json);
}
response.setStatusCode(200);
}
Aggregations