@Override protected HttpMessage createInvalidMessage() { return new HttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/bad-request", config); } }
/** * Sets the given parameter * * @param param the param to set */ public void setParam(MethodParam param) { params[param.getPosition()] = param; }
private Object processAnnotations(HttpMethod method, HttpRequest request, MethodParam param, MethodParam[] params, ResourcePath path, ResourcePath.Component[] components, ChannelHandlerContext ctx) { if (param.isCookieParam()) { return extractCookieParam(param, request); } else if (param.isHeaderParam()) { return extractHeaderParam(param, request); } else if (param.isFormParam()) { return extractFormParam(param, request); } else if (param.isQueryParam()) { return extractQueryParam(param, request); } else if (param.isPathParam()) { return extractPathParam(param, path); } else if (param.isSessionParam()) { return request.getSubject() == null ? null : request.getSubject().getSession().getAttribute(param.getName()); } return null; }
protected Object extractQueryParam(MethodParam param, HttpRequest request) { //query string param can be a list or string, if neither set to null if (List.class.isAssignableFrom(param.getParameterType())) { return request.getQueryParams().get(param.getName()); } else if (String.class.isAssignableFrom(param.getParameterType())) { return request.getQueryParams().getFirst(param.getName()); } else { if (ReflectionUtil.isNumeric(param.getParameterType())) { //if param is a number then try to handle with NumberType.parseType return extractNumberParam(param, request.getQueryParams().getFirst(param.getName())); } else { return null; } } }
public void transcribe(HttpRequest request) { //apply any transcription for (Transcription transcription : transcriptions) { if (transcription.matches(request.getUri())) { if (transcription.isReplaceWholeRequest()) { request.setUri(transcription.getReplacementPath()); } else { String newPath; if (transcription.isReplaceFirstOccurrence()) { newPath = transcription.replaceFirstMatch(request.getUri()); } else { newPath = transcription.replaceAllMatches(request.getUri()); } request.setUri(newPath); } break; } } }
protected Object extractFormParam(MethodParam param, HttpRequest request) { Object obj = request.getFormParam().get(param.getName()); if (obj != null && param.getParameterType().isAssignableFrom(obj.getClass())) { return obj; } else { if (ReflectionUtil.isNumeric(param.getParameterType())) { //if param is a number then try to handle with NumberType.parseType return extractNumberParam(param, (String) request.getFormParam().get(param.getName())); } else { return null; } } }
/** * Initializes a response with 200 status and sets the connection header to whatever the client * requested. If no connection header is found in the client request then it is set to CLOSE * * @param message */ public HttpResponse(final HttpRequest message) { this(message == null ? HttpVersion.HTTP_1_1 : message.getProtocolVersion(), HttpStatus.OK); if (message != null) { String conn = message.headers().get(HttpHeaders.Names.CONNECTION); if (conn == null) { conn = HttpHeaders.Values.CLOSE; } headers().set(HttpHeaders.Names.CONNECTION, conn); } }
@Override public Response.ResponseBuilder status(int status) { response.setStatus(HttpResponseStatus.valueOf(status)); return this; }
@Override public WebSocketHandler setupPipeline(ChannelPipeline p, ChannelHandlerContext ctx) { //WebSocketHandler is stateful so must do an instance per request/channel WebSocketHandler h = new WebSocketHandler(config); p.addLast("ws-decoder", new HttpRequestDecoder(config)); p.addLast("ws-aggregator", new HttpObjectAggregator(65536)); p.addLast("ws-encoder", new HttpResponseEncoder()); p.addLast("ws-chunkedWriter", new ChunkedWriteHandler()); p.addLast("ws-handler", h); return h; } }
/** * Sets a cookie with path as "/" * * @param name * @param value */ public void setCookie(final String name, final String value) { HttpCookie cookie = new HttpCookie(name, value); cookie.setPath("/"); setCookie(cookie); }
private void writeResponseFromStream(ResolvedFile response, HttpResponse res, HttpRequest request, MediaType mediaType, HttpMethod method, ChannelHandlerContext ctx) { res.setManagedWriter(new StaticFileWriter(ctx, res, response, request, formats, conf)); }
@Override public Response.ResponseBuilder header(String name, Object value) { response.headers().set(name, value); return this; }
@Override public int getStatusCode() { return response.getStatus().code(); }
public int compare(final Transcription o1, final Transcription o2) { if (o1.getCreatedAt() < o2.getCreatedAt()) { return -1; } if (o1.getCreatedAt() > o2.getCreatedAt()) { return 1; } return 0; } });
/** * @return the URI used to match this template e.g. /404-not-found * if this a path is not set then the {@link #getCode()} is returned. * If this is not an error template then {@link #getCode()} is always 0 */ public String getPath() { return path == null ? String.valueOf(getCode()) : path; }
@Override public String getValidationMessage(MethodParam param) { return String.format("%s is required", param.getName()); }
@Override public String getReasonPhrase() { return response.getStatus().reasonPhrase(); } };