Search in sources :

Example 16 with HttpContext

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())));
}
Also used : SecurityContext(org.forgerock.services.context.SecurityContext) ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) Context(org.forgerock.services.context.Context) HttpContext(org.forgerock.json.resource.http.HttpContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) RealmContext(org.forgerock.openam.rest.RealmContext) HttpContext(org.forgerock.json.resource.http.HttpContext) SecurityContext(org.forgerock.services.context.SecurityContext) Subject(javax.security.auth.Subject) Debug(com.sun.identity.shared.debug.Debug) Date(java.util.Date) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 17 with HttpContext

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;
    }
}
Also used : HttpContext(org.forgerock.json.resource.http.HttpContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 18 with HttpContext

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;
}
Also used : Locale(java.util.Locale) HttpContext(org.forgerock.json.resource.http.HttpContext)

Example 19 with HttpContext

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;
}
Also used : HttpContext(org.forgerock.json.resource.http.HttpContext) ISLocaleContext(com.sun.identity.common.ISLocaleContext)

Example 20 with HttpContext

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));
        }
    }
}
Also used : HttpContext(org.forgerock.json.resource.http.HttpContext)

Aggregations

HttpContext (org.forgerock.json.resource.http.HttpContext)26 Test (org.testng.annotations.Test)8 SSOException (com.iplanet.sso.SSOException)7 List (java.util.List)6 ResourceException (org.forgerock.json.resource.ResourceException)6 SMSException (com.sun.identity.sm.SMSException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 HashMap (java.util.HashMap)5 JsonValue (org.forgerock.json.JsonValue)5 NotFoundException (org.forgerock.json.resource.NotFoundException)5 NotSupportedException (org.forgerock.json.resource.NotSupportedException)5 SSOToken (com.iplanet.sso.SSOToken)4 ISLocaleContext (com.sun.identity.common.ISLocaleContext)4 IdRepoException (com.sun.identity.idm.IdRepoException)4 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)4 MessagingException (javax.mail.MessagingException)4 Subject (javax.security.auth.Subject)4 BadRequestException (org.forgerock.json.resource.BadRequestException)4 ConflictException (org.forgerock.json.resource.ConflictException)4 ForbiddenException (org.forgerock.json.resource.ForbiddenException)4