Search in sources :

Example 51 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project ddf by codice.

the class TestRestEndpoint method createHeaders.

private HttpHeaders createHeaders(List<String> mimeTypeList) {
    HttpHeaders headers = mock(HttpHeaders.class);
    when(headers.getRequestHeader(HttpHeaders.CONTENT_TYPE)).thenReturn(mimeTypeList);
    return headers;
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders)

Example 52 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project carbon-apimgt by wso2.

the class OAuth2Authenticator method authenticate.

/*
    * This method performs authentication and authorization
    * @param Request
    * @param Response
    * @param ServiceMethodInfo
    * throws Exception
    * */
@Override
public boolean authenticate(Request request, Response responder, ServiceMethodInfo serviceMethodInfo) throws APIMgtSecurityException {
    ErrorHandler errorHandler = null;
    boolean isTokenValid = false;
    HttpHeaders headers = request.getHeaders();
    boolean isCookieHeaderPresent = false;
    boolean isAuthorizationHeaderPresent = false;
    if (request.getHeader(RestApiConstants.COOKIE_HEADER) != null) {
        isCookieHeaderPresent = true;
    }
    if (request.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER) != null) {
        isAuthorizationHeaderPresent = true;
    }
    if (headers != null && isCookieHeaderPresent && isCookieExists(request, APIConstants.AccessTokenConstants.AM_TOKEN_MSF4J)) {
        String accessToken = null;
        String cookies = request.getHeader(RestApiConstants.COOKIE_HEADER);
        String partialTokenFromCookie = extractPartialAccessTokenFromCookie(cookies);
        if (partialTokenFromCookie != null && isAuthorizationHeaderPresent) {
            String authHeader = request.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER);
            String partialTokenFromHeader = extractAccessToken(authHeader);
            accessToken = (partialTokenFromHeader != null) ? partialTokenFromHeader + partialTokenFromCookie : partialTokenFromCookie;
        }
        isTokenValid = validateTokenAndScopes(request, serviceMethodInfo, accessToken);
        request.setProperty(LOGGED_IN_USER, getEndUserName(accessToken));
    } else if (headers != null && isAuthorizationHeaderPresent) {
        String authHeader = request.getHeader(RestApiConstants.AUTHORIZATION_HTTP_HEADER);
        String accessToken = extractAccessToken(authHeader);
        if (accessToken != null) {
            isTokenValid = validateTokenAndScopes(request, serviceMethodInfo, accessToken);
            request.setProperty(LOGGED_IN_USER, getEndUserName(accessToken));
        }
    } else {
        throw new APIMgtSecurityException("Missing Authorization header in the request.`", ExceptionCodes.MALFORMED_AUTHORIZATION_HEADER_OAUTH);
    }
    return isTokenValid;
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HttpHeaders(javax.ws.rs.core.HttpHeaders) APIMgtSecurityException(org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException)

Example 53 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class ErrorUtil method getErrorResponse.

/**
 * Transforms an exception into a {@code Response}.
 *
 * @param  exception the exception
 * @param  request the current request
 * @param  httpHeaders the current HTTP headers
 * @return the response
 */
public Response getErrorResponse(Exception exception, Request request, HttpHeaders httpHeaders) {
    Optional<APIError> apiErrorOptional = _exceptionConverterManager.convert(exception);
    if (!apiErrorOptional.isPresent()) {
        Class<? extends Exception> exceptionClass = exception.getClass();
        if (_apioLogger != null) {
            _apioLogger.warning("No exception converter found for " + exceptionClass);
        }
        if (exceptionClass.isAssignableFrom(WebApplicationException.class)) {
            WebApplicationException webApplicationException = (WebApplicationException) exception;
            return webApplicationException.getResponse();
        }
        return Response.serverError().build();
    }
    APIError apiError = apiErrorOptional.get();
    if (_apioLogger != null) {
        _apioLogger.error(apiError);
    }
    int statusCode = apiError.getStatusCode();
    Optional<ErrorMessageMapper> errorMessageMapperOptional = _errorMessageMapperManager.getErrorMessageMapperOptional(request);
    return errorMessageMapperOptional.map(errorMessageMapper -> {
        String result = ErrorWriter.writeError(errorMessageMapper, apiError, httpHeaders);
        return Response.status(statusCode).type(errorMessageMapper.getMediaType()).entity(result).build();
    }).orElseGet(() -> Response.status(statusCode).build());
}
Also used : ErrorWriter(com.liferay.apio.architect.writer.ErrorWriter) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) ApioLogger(com.liferay.apio.architect.logger.ApioLogger) ErrorMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager) Component(org.osgi.service.component.annotations.Component) APIError(com.liferay.apio.architect.error.APIError) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) OPTIONAL(org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) GREEDY(org.osgi.service.component.annotations.ReferencePolicyOption.GREEDY) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ExceptionConverterManager(com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) APIError(com.liferay.apio.architect.error.APIError)

Example 54 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class ProblemJSONErrorMessageMapperTest method testJSONLDErrorMessageMapper.

@Test
public void testJSONLDErrorMessageMapper() {
    APIError apiError = new APIError(new IllegalArgumentException(), "A title", "A description", "A type", 404);
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    String error = ErrorWriter.writeError(_errorMessageMapper, apiError, httpHeaders);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("detail", is(aJsonString(equalTo("A description")))).where("status", is(aJsonInt(equalTo(404)))).where("title", is(aJsonString(equalTo("A title")))).where("type", is(aJsonString(equalTo("A type")))).build();
    assertThat(error, is(aJsonObjectStringWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JsonMatchers.aJsonString(com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString) APIError(com.liferay.apio.architect.error.APIError) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 55 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class JSONLDFormMessageMapperTest method testJSONLDFormMessageMapper.

@Test
public void testJSONLDFormMessageMapper() {
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    JsonObject jsonObject = MockFormWriter.write(httpHeaders, _formMessageMapper);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("@context", is(aJsonArrayThat(contains(_theContext)))).where("@id", isALinkTo("localhost/f/f/s")).where("@type", is(aJsonString(equalTo("Class")))).where("description", is(aJsonString(equalTo("description")))).where("supportedProperty", is(aJsonArrayThat(contains(_theProperties)))).where("title", is(aJsonString(equalTo("title")))).build();
    assertThat(jsonObject, is(aJsonObjectWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JsonObject(com.google.gson.JsonObject) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Aggregations

HttpHeaders (javax.ws.rs.core.HttpHeaders)87 Test (org.junit.Test)57 Message (org.apache.cxf.message.Message)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 Optional (java.util.Optional)11 CatalogFramework (ddf.catalog.CatalogFramework)10 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)10 JsonObject (com.google.gson.JsonObject)9 Locale (java.util.Locale)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Response (javax.ws.rs.core.Response)8 UriInfo (javax.ws.rs.core.UriInfo)8 IOException (java.io.IOException)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 MediaType (javax.ws.rs.core.MediaType)7 RequestInfo (com.liferay.apio.architect.request.RequestInfo)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Conditions (com.liferay.apio.architect.test.util.json.Conditions)5 OutputStream (java.io.OutputStream)5 Annotation (java.lang.annotation.Annotation)5