default <T> T getAttribute(String attributeName, Class<T> expectedClass) { Object o = getAttribute(attributeName); if (o == null) { return null; } else if (expectedClass.isAssignableFrom(o.getClass())) { return expectedClass.cast(o); } else { throw new IllegalArgumentException("Object " + o + " is not instance of expected class " + expectedClass); } }
public Builder requestAttribute(String attributeName, Object attributeValue) { if (obj.requestContext != null) { throw new IllegalStateException("requestContext has been set already!"); } if (this.requestAttributes == null) { this.requestAttributes = new DefaultRequestAttributes(); } this.requestAttributes.setAttribute(attributeName, attributeValue); return this; }
private MediaType accept(RequestContext ctx) { try { MediaType mediaType = (MediaType) ctx.requestAttributes().getAttribute(HttpHeaders.Names.ACCEPT); if (mediaType != null) { return mediaType; } } catch (NullPointerException e) { log.trace("Unable to retrieve Accept header from Request. Defaulting to 'application/json'."); } return MediaType.JSON; }
private RequestContext createPolicyReq() { RequestAttributes attribs = new DefaultRequestAttributes(); attribs.setAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, ctxToAuthorize); attribs.setAttribute(AuthzConstants.ATTR_REQUEST_RESOURCE_STATE, reqStateToAuthorize); attribs.setAttribute(AuthzConstants.ATTR_RESPONSE_RESOURCE_STATE, respStateToAuthorize); return new RequestContext.Builder().requestAttributes(attribs).build(); }
private String getBearerToken(RequestContext requestContext) { String auth = requestContext.requestAttributes().getAttribute(HttpHeaders.Names.AUTHORIZATION, String.class); if (auth != null) { String[] a = auth.split(" "); if (a.length == 2 && a[0].equalsIgnoreCase(AUTH_TYPE)) { return a[1]; } } return null; }
@Override public Map<String, ?> properties(RequestContext ctx) throws Exception { // determine table names List<String> tables = catalog().tableIds(); // store to ctx attributes to pass on to readMembers ctx.requestAttributes().setAttribute(TABLE_NAMES, tables); List<Resource> links = new LinkedList<>(); MapResource batch = new MapResource(); batch.put("rel", "batch"); batch.put(LiveOak.HREF, uri() + "/" + BATCH_ENDPOINT); links.add(batch); Map<String, Object> result = new HashMap<>(); result.put("links", links); // here only set num of tables as size result.put("count", tables.size()); // maybe some other things to do with db as a whole result.put("type", "database"); return result; }
private MediaType contentType(RequestContext ctx) { try { MediaType mediaType = (MediaType) ctx.requestAttributes().getAttribute(HttpHeaders.Names.CONTENT_TYPE); if (mediaType != null) { return mediaType; } } catch (NullPointerException e) { log.trace("Unable to retrieve Content-Type header from Request. Defaulting to 'application/json'."); } return MediaType.JSON; }
attribs.setAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, reqContext); attribs.setAttribute(AuthzConstants.ATTR_RESPONSE_RESOURCE_STATE, origResourceResponse.state());
@Override public Collection<Resource> members(RequestContext ctx) throws Exception { List<String> tables = (List<String>) ctx.requestAttributes().getAttribute(TABLE_NAMES); return tables.stream() .map(table -> new PgSqlTableResource(this, table)) .collect(Collectors.toList()); }
.securityContext(securityContext); RequestAttributes attribs = new DefaultRequestAttributes(); attribs.setAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, reqToAuthorize); attribs.setAttribute(AuthzConstants.ATTR_RESPONSE_RESOURCE_STATE, childState); RequestContext authzRequest = new RequestContext.Builder().requestAttributes(attribs).build();
RequestContext reqCtxToAuthorize = ctx.requestAttributes() != null ? ctx.requestAttributes().getAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, RequestContext.class) : null; ResourceState reqResourceState = ctx.requestAttributes() != null ? ctx.requestAttributes().getAttribute(AuthzConstants.ATTR_REQUEST_RESOURCE_STATE, ResourceState.class) : null; ResourceState respResourceState = ctx.requestAttributes() != null ? ctx.requestAttributes().getAttribute(AuthzConstants.ATTR_RESPONSE_RESOURCE_STATE, ResourceState.class) : null; if (reqCtxToAuthorize == null) { if (log.isTraceEnabled()) {
attribs.setAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, req.requestContext()); attribs.setAttribute(AuthzConstants.ATTR_REQUEST_RESOURCE_STATE, req.state()); RequestContext authzRequest = new RequestContext.Builder().requestAttributes(attribs).build();
@Override public void readProperties(RequestContext ctx, PropertySink sink) throws Exception { AuthzDecision decision = null; try { if (policy != null) { RequestContext reqCtxToAuthorize = ctx.requestAttributes() != null ? ctx.requestAttributes().getAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, RequestContext.class) : null; if (reqCtxToAuthorize == null) { if (log.isTraceEnabled()) { log.trace("Request is null. Rejecting"); } decision = AuthzDecision.REJECT; } else { decision = policy.isAuthorized(reqCtxToAuthorize); } } } catch (Throwable t) { log.error("Error during authz check", t); decision = AuthzDecision.REJECT; } if (decision == null) { decision = AuthzDecision.IGNORE; } sink.accept(AuthzConstants.ATTR_AUTHZ_POLICY_RESULT, decision.toString()); sink.complete(); }
@Override public void readProperties(RequestContext ctx, PropertySink sink) throws Exception { try { RequestContext ctxToAuthorize = ctx.requestAttributes().getAttribute(AuthzConstants.ATTR_REQUEST_CONTEXT, RequestContext.class); ResourceState reqStateToAuthorize = ctx.requestAttributes().getAttribute(AuthzConstants.ATTR_REQUEST_RESOURCE_STATE, ResourceState.class); ResourceState respStateToAuthorize = ctx.requestAttributes().getAttribute(AuthzConstants.ATTR_RESPONSE_RESOURCE_STATE, ResourceState.class); if (ctxToAuthorize == null) { if (log.isTraceEnabled()) {
HttpRequest original = (HttpRequest) request.requestContext().requestAttributes().getAttribute("HTTP_REQUEST");