use of org.forgerock.json.resource.http.HttpContext in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method setUp.
@BeforeMethod
public void setUp() throws ResourceException {
callerSubject = new Subject();
// to mock the HTTP method, we need the following contexts
Context httpContext = new HttpContext(json(object(field(HttpContext.ATTR_HEADERS, Collections.singletonMap("method", Arrays.asList("PUT"))), field(HttpContext.ATTR_PARAMETERS, Collections.emptyMap()))), null);
Context securityContext = new SecurityContext(httpContext, null, null);
Context subjectContext = new SSOTokenContext(mock(Debug.class), null, securityContext) {
@Override
public Subject getCallerSubject() {
return callerSubject;
}
};
RealmContext realmContext = new RealmContext(subjectContext);
realmContext.setSubRealm("/", "/");
mockServerContext = ClientContext.newInternalClientContext(realmContext);
resourceTypeService = mock(MockResourceTypeService.class);
Debug debug = mock(Debug.class);
resourceTypesResource = new ResourceTypesResource(debug, new EntitlementsExceptionMappingHandler(EntitlementRestGuiceModule.getEntitlementsErrorHandlers()), resourceTypeService);
rawData.put("name", Collections.singleton("myResourceType"));
rawData.put("description", Collections.singleton("myResourceType"));
rawData.put("realm", Collections.singleton("/"));
rawData.put("actions", Collections.singleton("CREATE"));
rawData.put("patterns", Collections.singleton("http://example.com:80/*"));
rawData.put("creationDate", Collections.singleton(String.valueOf(new Date().getTime())));
rawData.put("lastModifiedDate", Collections.singleton(String.valueOf(new Date().getTime())));
}
use of org.forgerock.json.resource.http.HttpContext in project OpenAM by OpenRock.
the class RestUtils method getMimeHeaderValue.
/**
* Returns the value of the named header field from the request, decoding it if it is mime-encoded. If the header
* is not mime-encoded then it is returned as-is. If no such header is present, then {@code null} is returned. If
* there are multiple values for the header, then the first value is returned.
*
* @param serverContext the context of the request. Must contain a {@link HttpContext}.
* @param headerName the name of the header to get.
* @return the decoded header value, or {@code null} if no such header exists in the request.
*
* @see <a href="https://tools.ietf.org/html/rfc2047">RFC 2047: MIME Part 3: Message Header Extensions for Non-ASCII
* Text</a>
*/
public static String getMimeHeaderValue(final Context serverContext, final String headerName) {
final HttpContext httpContext = serverContext.asContext(HttpContext.class);
final String headerValue = httpContext.getHeaderAsString(headerName);
try {
return headerValue == null ? null : MimeUtility.decodeText(headerValue);
} catch (UnsupportedEncodingException ex) {
if (debug.warningEnabled()) {
debug.warning("Unable to decode mime header: " + ex);
}
return headerValue;
}
}
use of org.forgerock.json.resource.http.HttpContext in project OpenAM by OpenRock.
the class ServerContextUtils method getLocaleFromContext.
/**
* Get the Context as an HttpContext, read the accept-language from the
* header and create a Locale object from that.
*
* @param context The server context from which the language header can be read.
* @return The Local instance or null if no accept-language header was found.
*/
public static Locale getLocaleFromContext(Context context) {
if (context == null) {
return null;
}
Locale locale;
try {
final String language = context.asContext(HttpContext.class).getHeaderAsString(ACCEPT_LANGUAGE);
locale = com.sun.identity.shared.locale.Locale.getLocaleObjFromAcceptLangHeader(language);
} catch (IllegalArgumentException iae) {
locale = null;
}
return locale;
}
use of org.forgerock.json.resource.http.HttpContext in project OpenAM by OpenRock.
the class LocaleContext method getLocale.
/**
* The {@link Locale} corresponding to the incoming request.
* @return The client's preferred locale.
*/
public Locale getLocale() {
if (locale == null) {
final HttpContext httpContext = asContext(HttpContext.class);
ISLocaleContext localeContext = new ISLocaleContext();
localeContext.setLocale(httpContext);
locale = localeContext.getLocale();
}
return locale;
}
use of org.forgerock.json.resource.http.HttpContext in project OpenAM by OpenRock.
the class CrestAuditor method setClientFromHttpContextHeaderIfExists.
private void setClientFromHttpContextHeaderIfExists(AMAccessAuditEventBuilder builder, Context context) {
if (context.containsContext(HttpContext.class)) {
HttpContext httpContext = context.asContext(HttpContext.class);
List<String> xForwardedFor = httpContext.getHeader(SystemPropertiesManager.get(Constants.CLIENT_IP_ADDR_HEADER));
if (xForwardedFor != null && xForwardedFor.size() > 0) {
builder.client(xForwardedFor.get(0));
}
}
}
Aggregations