spark.utils.IOUtils Java Examples

The following examples show how to use spark.utils.IOUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ByteMapper.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Decompress a string encoded as UTF-8 bytes to a
 * Java String.
 *
 * @param compressed string as compressed bytes
 * @return decompressed String
 * @throws IOException if an error occurs during decompression
 */
protected String decompressUTF8(byte[] compressed) throws IOException {
    try (ByteArrayInputStream bis = new ByteArrayInputStream(compressed)) {
        try (GZIPInputStream gzip = new GZIPInputStream(bis)) {
            byte[] decompressed = IOUtils.toByteArray(gzip);
            return new String(decompressed, StandardCharsets.UTF_8);
        }
    }
}
 
Example #2
Source File: ResolvedContentRoute.java    From cineast with MIT License 5 votes vote down vote up
@Override
public Object handle(Request request, Response response) throws Exception {

  Map<String, String> params = request.params();

  String id;

  if (params != null && params.containsKey(":id")) {
    id = params.get(":id");
  } else {
    response.status(400);
    response.raw().getWriter().write("Bad request");
    response.raw().getWriter().flush();
    return 404;
  }

  ResolutionResult rresult = this.resolver.resolve(id);

  if (rresult == null) {
    response.status(400);
    response.raw().getWriter().write("Bad request");
    response.raw().getWriter().flush();
    return 404;
  }

  response.type(rresult.mimeType);

  try (InputStream inputStream = rresult.stream;
      OutputStream wrappedOutputStream =
          GzipUtils.checkAndWrap(request.raw(), response.raw(), false)) {
    IOUtils.copy(inputStream, wrappedOutputStream);
    wrappedOutputStream.flush();
    response.raw().getOutputStream().close();
  }

  return 200;
}
 
Example #3
Source File: UserControllerTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static String executeRequest(String method, String path) throws IOException {
  URL url = new URL("http://localhost:8080" + path);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(method);
  connection.setDoOutput(true);
  connection.connect();
  return IOUtils.toString(connection.getInputStream());
}
 
Example #4
Source File: AppTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static TestResponse executeRequest(String method, String path) throws IOException {
  URL url = new URL("http://localhost:8080" + path);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(method);
  connection.setDoOutput(true);
  connection.connect();
  String body = IOUtils.toString(connection.getInputStream());
  return new TestResponse(connection.getResponseCode(), body);
}
 
Example #5
Source File: AppTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static TestResponse executeRequest(String method, String path) throws IOException {
  URL url = new URL("http://localhost:8080" + path);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(method);
  connection.setDoOutput(true);
  connection.connect();
  String body = IOUtils.toString(connection.getInputStream());
  return new TestResponse(connection.getResponseCode(), body);
}
 
Example #6
Source File: AppTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static TestResponse executeRequest(String method, String path) throws IOException {
  URL url = new URL("http://localhost:8080" + path);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(method);
  connection.setDoOutput(true);
  connection.connect();
  String body = IOUtils.toString(connection.getInputStream());
  return new TestResponse(connection.getResponseCode(), body);
}
 
Example #7
Source File: UserControllerIntegrationTest.java    From blog-examples with Apache License 2.0 5 votes vote down vote up
private TestResponse request(String method, String path) {
	try {
		URL url = new URL("http://localhost:4567" + path);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod(method);
		connection.setDoOutput(true);
		connection.connect();
		String body = IOUtils.toString(connection.getInputStream());
		return new TestResponse(connection.getResponseCode(), body);
	} catch (IOException e) {
		e.printStackTrace();
		fail("Sending request failed: " + e.getMessage());
		return null;
	}
}