public void update(Request request, Response response) { String id = request.getHeader(Constants.Url.SAMPLE_ID, "No resource ID supplied"); SampleOidEntity entity = request.getBodyAs(SampleOidEntity.class, "Resource details not provided"); entity.setId(Identifiers.MONGOID.parse(id)); service.update(entity); response.setResponseNoContent(); }
private String getRouteName(Request request) { if (request.getResolvedRoute() == null) return StringUtils.EMPTY_STRING; String name = request.getResolvedRoute().getName(); if (name == null || name.trim().isEmpty()) { name = request.getResolvedRoute().getPattern(); } return name; }
@Override public void process(Request request) { if (request.isFlagged(Flags.Auth.PUBLIC_ROUTE) || request.isFlagged(Flags.Auth.NO_AUTHENTICATION)) return; String token = getToken(request); try { Map<String, Object> decoded = jwtVerifier.verify(token); request.putAttachment(AUTH0_JWT, decoded); } catch (Exception e) { throw new UnauthorizedException("Token validation failed"); } }
@Override protected void onComplete(Request request, Response response) { activeRequestsCounter.dec(); Long duration = computeDurationMillis(START_TIMES_BY_CORRELATION_ID.remove(request.getCorrelationId())); if (duration != null && duration.longValue() > 0) { allTimesTimer.update(duration, TimeUnit.MILLISECONDS); String name = getRouteName(request); if (name == null || name.isEmpty()) return; ROUTE_TIMERS.get(name).update(duration, TimeUnit.MILLISECONDS); } Counter responseCounter = COUNTERS_BY_RESPONSE.get(response.getResponseStatus().getCode()); if (responseCounter == null) { responseCounter = metrics.counter(getResponseCounterName(response.getResponseStatus())); COUNTERS_BY_RESPONSE.putIfAbsent(response.getResponseStatus().getCode(), responseCounter); } responseCounter.inc(); publish(request, response, duration); }
public ApiDeclarations read(Request request, Response response) { String path = request.getHeader("path"); ApiDeclarations api = apisByPath.get("/" + path); if (api == null) throw new NotFoundException(path); return api; }
public SampleOidEntity read(Request request, Response response) { String id = request.getHeader(Constants.Url.SAMPLE_ID, "No resource ID supplied"); SampleOidEntity entity = service.read(Identifiers.MONGOID.parse(id)); // enrich the resource with links, etc. here... HyperExpress.bind(Constants.Url.SAMPLE_ID, Identifiers.MONGOID.format(entity.getId())); return entity; }
/** Set the 'X-Response-Time' header to the response time in milliseconds. */ @Override public void process(Request request, Response response) { Long duration = computeDurationMillis(START_TIMES_BY_CORRELATION_ID.get(request.getCorrelationId())); if (duration != null && duration.longValue() > 0) { response.addHeader("X-Response-Time", String.valueOf(duration)); } }
public SampleOidEntity create(Request request, Response response) { SampleOidEntity entity = request.getBodyAs(SampleOidEntity.class, "Resource details not provided"); SampleOidEntity saved = service.create(entity); // Construct the response for create... response.setResponseCreated(); // Bind the resource with link URL tokens, etc. here... TokenResolver resolver = HyperExpress.bind(Constants.Url.SAMPLE_ID, Identifiers.MONGOID.format(saved.getId())); // Include the Location header... String locationPattern = request.getNamedUrl(HttpMethod.GET, Constants.Routes.SINGLE_OID_SAMPLE); response.addLocationHeader(LOCATION_BUILDER.build(locationPattern, resolver)); // Return the newly-created resource... return saved; }
private String getToken(Request request) throws UnauthorizedException { String authorization = request.getHeader(HttpHeaders.Names.AUTHORIZATION); if (authorization == null) { throw new UnauthorizedException("No Authorization header was found"); } Matcher matcher = PATTERN.matcher(authorization); if (matcher.find()) { return matcher.group(1); } else { throw new UnauthorizedException("Format is Authorization: Bearer [token]"); } }
public void delete(Request request, Response response) { String id = request.getHeader(Constants.Url.SAMPLE_ID, "No resource ID supplied"); service.delete(Identifiers.MONGOID.parse(id)); response.setResponseNoContent(); }
public void read(Request request, Response response) { System.out.println( "message: " + request.getHeader("m") + ", " + "stacktrace: " + request.getHeader("s")); }
@Override protected void onReceived(Request request, Response response) { activeRequestsCounter.inc(); START_TIMES_BY_CORRELATION_ID.put(request.getCorrelationId(), System.nanoTime()); }