public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) { int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; response.setStatusCode(status); StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw, true)); String logMessage = (message != null ? message + "\n" : "") + sw.toString(); logger.debug("Returning HTTP status " + status + ": " + logMessage); try { NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>" + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) { int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; response.setStatusCode(status); StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw, true)); String logMessage = (message != null ? message + "\n" : "") + sw.toString(); logger.debug("Returning HTTP status " + status + ": " + logMessage); try { NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>" + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorFileNotFound(HttpResponse response, String uri) { int status = HttpStatus.SC_NOT_FOUND; response.setStatusCode(status); String message = "File " + uri + " not found"; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>File not found</h1><p>" + message + "</p></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorFileNotFound(HttpResponse response, String uri) { int status = HttpStatus.SC_NOT_FOUND; response.setStatusCode(status); String message = "File " + uri + " not found"; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>File not found</h1><p>" + message + "</p></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorMissingQueryParameter(HttpResponse response, String param) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "Request must contain the parameter " + param; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorMissingQueryParameter(HttpResponse response, String param) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "Request must contain the parameter " + param; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue, String explanation) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid" + (explanation != null ? ": " + explanation : ""); logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue, String explanation) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid" + (explanation != null ? ": " + explanation : ""); logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
@Override protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response, Address serverAddressAtClient) throws IOException { // Individual info request String infoResponse = handleInfoRequest(absPath, queryItems, response); if (infoResponse == null) { // error condition, handleInfoRequest has set an error message return; } response.setStatusCode(HttpStatus.SC_OK); try { NStringEntity entity = new NStringEntity(infoResponse, "UTF-8"); entity.setContentType("text/plain; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
@Override protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response, Address serverAddressAtClient) throws IOException { // Individual info request String infoResponse = handleInfoRequest(absPath, queryItems, response); if (infoResponse == null) { // error condition, handleInfoRequest has set an error message return; } response.setStatusCode(HttpStatus.SC_OK); try { NStringEntity entity = new NStringEntity(infoResponse, "UTF-8"); entity.setContentType("text/plain; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) { int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; response.setStatusCode(status); StringWriter sw = new StringWriter(); exception.printStackTrace(new PrintWriter(sw, true)); String logMessage = (message != null ? message + "\n" : "") + sw.toString(); logger.debug("Returning HTTP status " + status + ": " + logMessage); try { NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>" + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorMissingQueryParameter(HttpResponse response, String param) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "Request must contain the parameter " + param; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue, String explanation) { int status = HttpStatus.SC_BAD_REQUEST; response.setStatusCode(status); String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid" + (explanation != null ? ": " + explanation : ""); logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
public static void errorFileNotFound(HttpResponse response, String uri) { int status = HttpStatus.SC_NOT_FOUND; response.setStatusCode(status); String message = "File " + uri + " not found"; logger.debug("Returning HTTP status " + status + ": " + message); try { NStringEntity entity = new NStringEntity("<html><body><h1>File not found</h1><p>" + message + "</p></body></html>", "UTF-8"); entity.setContentType("text/html; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }
/** * Creates a NStringEntity with the specified content and content type. * * @param s content to be used. Not {@code null}. * @param contentType content type to be used. May be {@code null}, in which case * {@link ContentType#TEXT_PLAIN} is assumed. * * @throws IllegalArgumentException if the string parameter is null * * @since 4.2 */ public NStringEntity(final String s, final ContentType contentType) { Args.notNull(s, "Source string"); Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } this.b = s.getBytes(charset); this.buf = ByteBuffer.wrap(this.b); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
@Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setStatusCode(HttpStatus.SC_METHOD_NOT_ALLOWED); NStringEntity body = new NStringEntity( "<html><body><h1>Method not allowed</h1></body></html>", "UTF-8"); body.setContentType("text/html; UTF-8"); response.setEntity(body); }
/** * Creates a NStringEntity with the specified content and content type. * * @param s content to be used. Not {@code null}. * @param contentType content type to be used. May be {@code null}, in which case * {@link ContentType#TEXT_PLAIN} is assumed. * * @throws IllegalArgumentException if the string parameter is null * * @since 4.2 */ public NStringEntity(final String s, final ContentType contentType) { Args.notNull(s, "Source string"); Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } this.b = s.getBytes(charset); this.buf = ByteBuffer.wrap(this.b); this.content = b; this.buffer = this.buf; if (contentType != null) { setContentType(contentType.toString()); } }
@Override protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response, Address serverAddressAtClient) throws IOException { // Individual info request String infoResponse = handleInfoRequest(absPath, queryItems, response); if (infoResponse == null) { // error condition, handleInfoRequest has set an error message return; } response.setStatusCode(HttpStatus.SC_OK); try { NStringEntity entity = new NStringEntity(infoResponse, "UTF-8"); entity.setContentType("text/plain; charset=UTF-8"); response.setEntity(entity); } catch (UnsupportedEncodingException e) { } }