use of com.sun.net.httpserver.HttpExchange in project connect-java-library by urbanairship.
the class StreamConnectionTest method testCloseKillsConsume.
@Test
public void testCloseKillsConsume() throws Exception {
Answer httpAnswer = new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
exchange.sendResponseHeaders(200, 0L);
// hoping this is enough to force flushing data down the wire
exchange.getResponseBody().write(randomAlphabetic(10000).getBytes(UTF_8));
exchange.getResponseBody().write("\n".getBytes(UTF_8));
exchange.getResponseBody().flush();
return null;
}
};
doAnswer(httpAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
final CountDownLatch latch = new CountDownLatch(1);
Answer consumerAnswer = new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
latch.countDown();
return null;
}
};
doAnswer(consumerAnswer).when(consumer).accept(anyString());
stream = new StreamConnection(descriptor(), http, connectionRetryStrategy, consumer, url);
ExecutorService thread = Executors.newSingleThreadExecutor();
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
stream.read(Optional.<StartPosition>absent());
return Boolean.TRUE;
}
};
try {
Future<Boolean> future = thread.submit(callable);
// Wait till we get something from the server
latch.await(1, TimeUnit.MINUTES);
// Now kill the stream
stream.close();
// Consume future should return without issue now
try {
future.get(1, TimeUnit.SECONDS);
} catch (TimeoutException e) {
fail();
}
} finally {
thread.shutdownNow();
}
}
use of com.sun.net.httpserver.HttpExchange in project connect-java-library by urbanairship.
the class StreamConnectionTest method testExceptionDuringConsume.
@Test
public void testExceptionDuringConsume() throws Exception {
Answer httpAnswer = new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
exchange.sendResponseHeaders(200, 0L);
exchange.getResponseBody().write(randomAlphabetic(10).getBytes(UTF_8));
exchange.getResponseBody().write("\n".getBytes(UTF_8));
exchange.getResponseBody().flush();
return null;
}
};
doAnswer(httpAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
doThrow(new RuntimeException("boom")).when(consumer).accept(anyString());
stream = new StreamConnection(descriptor(), http, connectionRetryStrategy, consumer, url);
expectedException.expect(RuntimeException.class);
stream.read(Optional.<StartPosition>absent());
stream.wait(100);
}
use of com.sun.net.httpserver.HttpExchange in project azure-tools-for-java by Microsoft.
the class JobViewDummyHttpServer method initlize.
public static synchronized void initlize() {
if (isEnabled) {
return;
}
try {
server = HttpServer.create(new InetSocketAddress(PORT), 10);
server.createContext("/clusters/", new HttpHandler() {
@Override
public void handle(final HttpExchange httpExchange) throws IOException {
requestDetail = RequestDetail.getRequestDetail(httpExchange.getRequestURI());
if (requestDetail == null) {
return;
}
IClusterDetail clusterDetail = requestDetail.getClusterDetail();
final String queryUrl = requestDetail.getQueryUrl();
if (requestDetail.getApiType() == HttpRequestType.YarnHistory) {
TaskExecutor.submit(new YarnHistoryTask(clusterDetail, queryUrl, new HttpFutureCallback(httpExchange) {
@Override
public void onSuccess(String str) {
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
try {
// work around of get job result
// TODO: get job result by REST API
Document doc = Jsoup.parse(str);
Elements contentElements = doc.getElementsByClass("content");
if (contentElements.size() == 1) {
Elements elements = contentElements.get(0).getElementsByTag("pre");
if (elements.size() == 1) {
str = elements.get(0).html();
}
}
httpExchange.sendResponseHeaders(200, str.length());
OutputStream stream = httpExchange.getResponseBody();
stream.write(str.getBytes());
stream.close();
} catch (IOException e) {
int a = 1;
// LOGGER.error("Get job history error", e);
}
}
}));
} else if (requestDetail.getApiType() == HttpRequestType.LivyBatchesRest) {
TaskExecutor.submit(new LivyTask(clusterDetail, queryUrl, new HttpFutureCallback(httpExchange) {
@Override
public void onSuccess(String str) {
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
try {
String applicationId = requestDetail.getProperty("applicationId");
if (applicationId != null) {
str = JobUtils.getJobInformation(str, applicationId);
}
httpExchange.sendResponseHeaders(200, str.length());
OutputStream stream = httpExchange.getResponseBody();
stream.write(str.getBytes());
stream.close();
} catch (IOException e) {
// LOGGER.error("Get job history error", e);
}
}
}));
} else if (requestDetail.getApiType() == HttpRequestType.MultiTask) {
TaskExecutor.submit(new MultiRestTask(clusterDetail, requestDetail.getQueryUrls(), new MultiHttpFutureCallback(httpExchange) {
public void onSuccess(List<String> strs) {
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
try {
String str = tasksDetailsConvert(strs);
httpExchange.sendResponseHeaders(200, str.length());
OutputStream stream = httpExchange.getResponseBody();
stream.write(str.getBytes());
stream.close();
} catch (IOException e) {
// LOGGER.error("Get job history error", e);
}
}
}));
} else {
TaskExecutor.submit(new RestTask(clusterDetail, queryUrl, new HttpFutureCallback(httpExchange) {
@Override
public void onSuccess(@NotNull String str) {
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
try {
httpExchange.sendResponseHeaders(200, str.length());
OutputStream stream = httpExchange.getResponseBody();
stream.write(str.getBytes());
stream.close();
} catch (IOException e) {
// LOGGER.error("Get job history error", e);
}
}
}));
}
}
});
executorService = Executors.newFixedThreadPool(NO_OF_THREADS);
server.setExecutor(executorService);
server.start();
isEnabled = true;
} catch (IOException e) {
// LOGGER.error("Get job history error", e);
// DefaultLoader.getUIHelper().showError(e.getClass().getName(), e.getMessage());
}
}
use of com.sun.net.httpserver.HttpExchange in project ceylon by eclipse.
the class CeylonDocToolTests method externalLinksToRemoteRepoWithoutModuleNamePattern.
@Test
public void externalLinksToRemoteRepoWithoutModuleNamePattern() throws Exception {
Assume.assumeTrue(CompilerTests.allowNetworkTests());
HttpServer stubServer = HttpServer.create(new InetSocketAddress(0), 1);
stubServer.createContext("/repo", new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
if (httpExchange.getRequestMethod().equals("HEAD")) {
httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1);
} else {
httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_IMPLEMENTED, -1);
}
httpExchange.close();
}
});
stubServer.start();
try {
String repoUrl = "http://localhost:" + stubServer.getAddress().getPort() + "/repo";
externalLinks(repoUrl, "file://not-existing-dir", "https://not-existing-url", repoUrl);
} finally {
stubServer.stop(0);
}
}
use of com.sun.net.httpserver.HttpExchange in project crate by crate.
the class AzureHttpHandler method handle.
@Override
public void handle(final HttpExchange exchange) throws IOException {
final String request = exchange.getRequestMethod() + " " + exchange.getRequestURI().toString();
if (request.startsWith("GET") || request.startsWith("HEAD") || request.startsWith("DELETE")) {
int read = exchange.getRequestBody().read();
assert read == -1 : "Request body should have been empty but saw [" + read + "]";
}
try {
if (Regex.simpleMatch("PUT /" + container + "/*blockid=*", request)) {
// Put Block (https://docs.microsoft.com/en-us/rest/api/storageservices/put-block)
final Map<String, String> params = decodeQueryString(exchange.getRequestURI().toString());
final String blockId = params.get("blockid");
blobs.put(blockId, Streams.readFully(exchange.getRequestBody()));
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
} else if (Regex.simpleMatch("PUT /" + container + "/*comp=blocklist*", request)) {
// Put Block List (https://docs.microsoft.com/en-us/rest/api/storageservices/put-block-list)
final String blockList = Streams.copyToString(new InputStreamReader(exchange.getRequestBody(), StandardCharsets.UTF_8));
final List<String> blockIds = Arrays.stream(blockList.split("<Latest>")).filter(line -> line.contains("</Latest>")).map(line -> line.substring(0, line.indexOf("</Latest>"))).collect(Collectors.toList());
final ByteArrayOutputStream blob = new ByteArrayOutputStream();
for (String blockId : blockIds) {
BytesReference block = blobs.remove(blockId);
assert block != null;
block.writeTo(blob);
}
blobs.put(exchange.getRequestURI().getPath(), new BytesArray(blob.toByteArray()));
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
} else if (Regex.simpleMatch("PUT /" + container + "/*", request)) {
// PUT Blob (see https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob)
final String ifNoneMatch = exchange.getRequestHeaders().getFirst("If-None-Match");
if ("*".equals(ifNoneMatch)) {
if (blobs.putIfAbsent(exchange.getRequestURI().getPath(), Streams.readFully(exchange.getRequestBody())) != null) {
sendError(exchange, RestStatus.CONFLICT);
return;
}
} else {
blobs.put(exchange.getRequestURI().getPath(), Streams.readFully(exchange.getRequestBody()));
}
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
} else if (Regex.simpleMatch("HEAD /" + container + "/*", request)) {
// Get Blob Properties (see https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-properties)
final BytesReference blob = blobs.get(exchange.getRequestURI().getPath());
if (blob == null) {
sendError(exchange, RestStatus.NOT_FOUND);
return;
}
exchange.getResponseHeaders().add("x-ms-blob-content-length", String.valueOf(blob.length()));
exchange.getResponseHeaders().add("x-ms-blob-type", "blockblob");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
} else if (Regex.simpleMatch("GET /" + container + "/*", request)) {
// GET Object (https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html)
final BytesReference blob = blobs.get(exchange.getRequestURI().getPath());
if (blob == null) {
sendError(exchange, RestStatus.NOT_FOUND);
return;
}
// see Constants.HeaderConstants.STORAGE_RANGE_HEADER
final String range = exchange.getRequestHeaders().getFirst("x-ms-range");
final Matcher matcher = Pattern.compile("^bytes=([0-9]+)-([0-9]+)$").matcher(range);
if (matcher.matches() == false) {
throw new AssertionError("Range header does not match expected format: " + range);
}
final int start = Integer.parseInt(matcher.group(1));
final int length = Integer.parseInt(matcher.group(2)) - start + 1;
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
exchange.getResponseHeaders().add("x-ms-blob-content-length", String.valueOf(length));
exchange.getResponseHeaders().add("x-ms-blob-type", "blockblob");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), length);
exchange.getResponseBody().write(blob.toBytesRef().bytes, start, length);
} else if (Regex.simpleMatch("DELETE /" + container + "/*", request)) {
// Delete Blob (https://docs.microsoft.com/en-us/rest/api/storageservices/delete-blob)
blobs.entrySet().removeIf(blob -> blob.getKey().startsWith(exchange.getRequestURI().getPath()));
exchange.sendResponseHeaders(RestStatus.ACCEPTED.getStatus(), -1);
} else if (Regex.simpleMatch("GET /" + container + "?restype=container&comp=list*", request)) {
// List Blobs (https://docs.microsoft.com/en-us/rest/api/storageservices/list-blobs)
final Map<String, String> params = decodeQueryString(exchange.getRequestURI().toString());
final StringBuilder list = new StringBuilder();
list.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
list.append("<EnumerationResults>");
final String prefix = params.get("prefix");
final Set<String> blobPrefixes = new HashSet<>();
final String delimiter = params.get("delimiter");
if (delimiter != null) {
list.append("<Delimiter>").append(delimiter).append("</Delimiter>");
}
list.append("<Blobs>");
for (Map.Entry<String, BytesReference> blob : blobs.entrySet()) {
if (prefix != null && blob.getKey().startsWith("/" + container + "/" + prefix) == false) {
continue;
}
String blobPath = blob.getKey().replace("/" + container + "/", "");
if (delimiter != null) {
int fromIndex = (prefix != null ? prefix.length() : 0);
int delimiterPosition = blobPath.indexOf(delimiter, fromIndex);
if (delimiterPosition > 0) {
blobPrefixes.add(blobPath.substring(0, delimiterPosition) + delimiter);
continue;
}
}
list.append("<Blob><Name>").append(blobPath).append("</Name>");
list.append("<Properties><Content-Length>").append(blob.getValue().length()).append("</Content-Length>");
list.append("<BlobType>BlockBlob</BlobType></Properties></Blob>");
}
if (blobPrefixes.isEmpty() == false) {
blobPrefixes.forEach(p -> list.append("<BlobPrefix><Name>").append(p).append("</Name></BlobPrefix>"));
}
list.append("</Blobs>");
list.append("</EnumerationResults>");
byte[] response = list.toString().getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/xml");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
exchange.getResponseBody().write(response);
} else {
sendError(exchange, RestStatus.BAD_REQUEST);
}
} finally {
exchange.close();
}
}
Aggregations