Java Code Examples for fi.iki.elonen.NanoHTTPD#newFixedLengthResponse()

The following examples show how to use fi.iki.elonen.NanoHTTPD#newFixedLengthResponse() . 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: ElementController.java    From UIAutomatorWD with MIT License 6 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    String elementId = urlParams.get("elementId");
    try {
        Element el = Elements.getGlobal().getElement(elementId);
        final Rect rect = el.element.getVisibleBounds();
        JSONObject res = new JSONObject();
        res.put("x", rect.left);
        res.put("y", rect.top);
        res.put("height", rect.height());
        res.put("width", rect.width());
        return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(res, sessionId).toString());
    } catch (final Exception e) {
        return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.UnknownError, sessionId).toString());
    }
}
 
Example 2
Source File: RestService.java    From kieker with Apache License 2.0 6 votes vote down vote up
private Response processJsonRequest(final byte[] buffer) {
	try {
		final ObjectMapper mapper = new ObjectMapper();
		mapper.readTree(buffer);
		final JsonNode object = mapper.readTree(buffer);
		if (object.getNodeType() == JsonNodeType.ARRAY) {
			return this.processJsonArray((ArrayNode) object);
		} else {
			return NanoHTTPD.newFixedLengthResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "JSON array expected");
		}
	} catch (final IOException e) {
		try {
			LOGGER.error("Parsing error for JSON message: {}", new String(buffer, "UTF-8"));
			return NanoHTTPD.newFixedLengthResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "Malformed JSON data");
		} catch (final UnsupportedEncodingException e1) {
			return NanoHTTPD.newFixedLengthResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "Malformed JSON data, is not UTF-8");
		}
	}
}
 
Example 3
Source File: HttpHandler.java    From gocd with Apache License 2.0 5 votes vote down vote up
NanoHTTPD.Response process() {
    if (isPassed()) {
        return NanoHTTPD.newFixedLengthResponse(Status.OK, "text/plain; charset=utf-8", "OK!");
    } else {
        return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, "text/plain; charset=utf-8", "Bad!");
    }
}
 
Example 4
Source File: RestService.java    From kieker with Apache License 2.0 5 votes vote down vote up
/**
 * Handle record put request.
 *
 * @param session
 *            http session
 * @return returns a response object
 */
private Response handlePutRequest(final IHTTPSession session) {
	try {
		final Integer size = Integer.valueOf(session.getHeaders().get("content-length"));

		if (size != null) {
			final byte[] buffer = new byte[size];
			final InputStream stream = session.getInputStream();
			if (stream.read(buffer, 0, size) == size) {

				final String contentType = session.getHeaders().get("content-type");

				if ("application/json".equals(contentType)) {
					return this.processJsonRequest(buffer);
				} else {
					return NanoHTTPD.newFixedLengthResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "JSON data expected");
				}
			} else {
				return NanoHTTPD.newFixedLengthResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "Corrupted message");
			}
		} else {
			return NanoHTTPD.newFixedLengthResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "JSON data expected");
		}
	} catch (final IOException e) {
		return NanoHTTPD.newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "IO exception");
	}
}
 
Example 5
Source File: WebServer.java    From exists-maven-plugin with Apache License 2.0 5 votes vote down vote up
private Response generateResponse(IHTTPSession session) {
  String uri = session.getUri();
  String type = getType(uri);
  switch (session.getMethod()) {
    case HEAD: {
      if (!storage.containsKey(uri)) {
        return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND, type, null);
      }
      return NanoHTTPD.newFixedLengthResponse(Status.OK, type, null);
    }
    case GET: {
      byte[] file = storage.get(uri);
      if (file == null) {
        return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND, type, uri);
      }
      return newFixedLengthResponse(Status.OK, type, new ByteArrayInputStream(file), (long)file.length);
    }
    case PUT: {
      try {
        storage.put(uri, getInput(session));
        return NanoHTTPD.newFixedLengthResponse(Status.OK, "", uri);
      }
      catch (IOException e) {
        return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", e.getMessage());
      }
    }
    default:
      return NanoHTTPD.newFixedLengthResponse(Status.NOT_IMPLEMENTED, "text/plain", session.getMethod() + " not supported");
  }
}
 
Example 6
Source File: RouterNanoHTTPD.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
public Response process(Map<String, String> urlParams, IHTTPSession session) {
    String error = "General error!";
    if (handlerObject != null || handler != null) {
        try {
            Object object = ((handlerObject != null) ? handlerObject : handler.newInstance());
            if (object instanceof UriResponder) {
                UriResponder responder = (UriResponder) object;
                switch (this.method) {
                    case Methods.GET:
                        return responder.get(this, urlParams, session);
                    case Methods.POST:
                        return responder.post(this, urlParams, session);
                    case Methods.PUT:
                        return responder.put(this, urlParams, session);
                    case Methods.DELETE:
                        return responder.delete(this, urlParams, session);
                    default:
                        return responder.other(session.getMethod().toString(), this, urlParams, session);
                }
            } else {
                return NanoHTTPD.newFixedLengthResponse(Status.OK, "text/plain", //
                        new StringBuilder("Return: ")//
                                .append(handler.getCanonicalName())//
                                .append(".toString() -> ")//
                                .append(object)//
                                .toString());
            }
        } catch (Exception e) {
            error = "Error: " + e.getClass().getName() + " : " + e.getMessage();
            LOG.log(Level.SEVERE, error, e);
        }
    }
    return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", error);
}
 
Example 7
Source File: WebServer.java    From exists-maven-plugin with Apache License 2.0 5 votes vote down vote up
private Response generateResponse(IHTTPSession session) {
  String uri = session.getUri();
  if (uri.startsWith("/auth")) {
  String authorization = session.getHeaders().get("authorization");
  if (authorization == null || !authorization.equals("Basic dXNlcjE6cGFzc3dvcmQxMjM=")) {
    Response response = NanoHTTPD.newFixedLengthResponse(Status.UNAUTHORIZED, "", null);
    response.addHeader("WWW-Authenticate", "Basic realm=\"Authentication needed\"");
    return response;
  }
  uri = uri.substring("/auth".length());
  }
  String type = getType(uri);
  switch (session.getMethod()) {
    case HEAD: {
      if (!storage.containsKey(uri)) {
        return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND, type, null);
      }
      return NanoHTTPD.newFixedLengthResponse(Status.OK, type, null);
    }
    case GET: {
      byte[] file = storage.get(uri);
      if (file == null) {
        return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND, type, uri);
      }
      return newFixedLengthResponse(Status.OK, type, new ByteArrayInputStream(file), (long)file.length);
    }
    case PUT: {
      try {
        storage.put(uri, getInput(session));
        return NanoHTTPD.newFixedLengthResponse(Status.OK, "", uri);
      }
      catch (IOException e) {
        return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", e.getMessage());
      }
    }
    default:
      return NanoHTTPD.newFixedLengthResponse(Status.NOT_IMPLEMENTED, "text/plain", session.getMethod() + " not supported");
  }
}
 
Example 8
Source File: StatusController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 9
Source File: ZigBeeRpcServer.java    From zigbee4java with Apache License 2.0 4 votes vote down vote up
@Override
public Response serve(IHTTPSession session) {
    final NanoHttpdJsonRpcServerResponse response = jsonRpcServer.handle(session);
    return NanoHTTPD.newFixedLengthResponse(response.getStatus(), "application/json-rpc", response.getMessage());
}
 
Example 10
Source File: UrlController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 11
Source File: AlertController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 12
Source File: TimeoutsController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 13
Source File: WindowController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 14
Source File: ExecuteController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 15
Source File: UrlController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 16
Source File: RouterNanoHTTPD.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
public Response get(UriResource uriResource, Map<String, String> urlParams, IHTTPSession session) {
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), "");
}
 
Example 17
Source File: StaticController.java    From r2cloud with Apache License 2.0 4 votes vote down vote up
private static Response new503ErrorResponse() {
	return NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "unable to find");
}
 
Example 18
Source File: UrlController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");

    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 19
Source File: ContextController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
 
Example 20
Source File: ContextController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
    String sessionId = urlParams.get("sessionId");
    return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}