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;
}
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;
}
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());
}
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)));
}
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)));
}
Aggregations