use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.
the class SelectMethodCandidatesTest method doTestProducesResource.
private void doTestProducesResource(Class<?> resourceClass, String path, String acceptContentTypes, String expectedResponseType, String expectedMethodName) throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(resourceClass);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentType = "*/*";
Message m = new MessageImpl();
m.put(Message.CONTENT_TYPE, contentType);
Exchange ex = new ExchangeImpl();
ex.setInMessage(m);
m.setExchange(ex);
Endpoint e = mockEndpoint();
ex.put(Endpoint.class, e);
MetadataMap<String, String> values = new MetadataMap<>();
Map<ClassResourceInfo, MultivaluedMap<String, String>> mResources = JAXRSUtils.selectResourceClass(resources, path, m);
OperationResourceInfo ori = JAXRSUtils.findTargetMethod(mResources, m, "GET", values, contentType, sortMediaTypes(acceptContentTypes));
assertNotNull(ori);
assertEquals(expectedMethodName, ori.getMethodToInvoke().getName());
assertEquals(expectedResponseType, m.getExchange().get(Message.CONTENT_TYPE));
}
use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.
the class JwtRequestCodeFilter method process.
@Override
public MultivaluedMap<String, String> process(MultivaluedMap<String, String> params, UserSubject endUser, Client client) {
String requestToken = params.getFirst(REQUEST_PARAM);
String requestUri = params.getFirst(REQUEST_URI_PARAM);
if (requestToken == null) {
if (isRequestUriValid(client, requestUri)) {
requestToken = WebClient.create(requestUri).accept(REQUEST_URI_CONTENT_TYPE).get(String.class);
}
} else if (requestUri != null) {
LOG.warning("It is not valid to specify both a request and request_uri value");
throw new SecurityException();
}
if (requestToken != null) {
JweDecryptionProvider theDecryptor = super.getInitializedDecryptionProvider(client.getClientSecret());
JwsSignatureVerifier theSigVerifier = getInitializedSigVerifier(client);
JwtToken jwt = getJwtToken(requestToken, theDecryptor, theSigVerifier);
JwtClaims claims = jwt.getClaims();
// Check issuer
String iss = issuer != null ? issuer : client.getClientId();
if (!iss.equals(claims.getIssuer())) {
throw new SecurityException();
}
// Check client_id - if present it must match the client_id specified in the request
if (claims.getClaim(OAuthConstants.CLIENT_ID) != null && !claims.getStringProperty(OAuthConstants.CLIENT_ID).equals(client.getClientId())) {
throw new SecurityException();
}
// Check response_type - if present it must match the response_type specified in the request
String tokenResponseType = (String) claims.getClaim(OAuthConstants.RESPONSE_TYPE);
if (tokenResponseType != null && !tokenResponseType.equals(params.getFirst(OAuthConstants.RESPONSE_TYPE))) {
throw new SecurityException();
}
MultivaluedMap<String, String> newParams = new MetadataMap<>(params);
Map<String, Object> claimsMap = claims.asMap();
for (Map.Entry<String, Object> entry : claimsMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Map) {
Map<String, Object> map = CastUtils.cast((Map<?, ?>) value);
value = jsonHandler.toJson(map);
} else if (value instanceof List) {
List<Object> list = CastUtils.cast((List<?>) value);
value = jsonHandler.toJson(list);
}
newParams.putSingle(key, value.toString());
}
return newParams;
}
return params;
}
use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.
the class AbstractClient method retryInvoke.
@SuppressWarnings("unchecked")
protected Object[] retryInvoke(BindingOperationInfo oi, Object[] params, Map<String, Object> context, Exchange exchange) throws Exception {
try {
Object body = params.length == 0 ? null : params[0];
Map<String, Object> reqContext = CastUtils.cast((Map<?, ?>) context.get(REQUEST_CONTEXT));
MultivaluedMap<String, String> headers = (MultivaluedMap<String, String>) reqContext.get(Message.PROTOCOL_HEADERS);
URI newRequestURI = calculateNewRequestURI(reqContext);
// TODO: if failover conduit selector fails to find a failover target
// then it will revert to the previous endpoint; that is not very likely
// but possible - thus ideally we need to resert base and current URI only
// if we get the same ConduitInitiatior endpoint instance before and after
// retryInvoke.
Object response = retryInvoke(newRequestURI, headers, body, exchange, context);
exchange.put(List.class, getContentsList(response));
return new Object[] { response };
} catch (Exception ex) {
exchange.put(Exception.class, ex);
} catch (Throwable t) {
exchange.put(Exception.class, new Exception(t));
}
return null;
}
use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.
the class CrossOriginResourceSharingFilter method getResourceMethod.
private Method getResourceMethod(Message m, String httpMethod) {
String requestUri = HttpUtils.getPathToMatch(m, true);
List<ClassResourceInfo> resources = JAXRSUtils.getRootResources(m);
Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources = JAXRSUtils.selectResourceClass(resources, requestUri, m);
if (matchedResources == null) {
return null;
}
MultivaluedMap<String, String> values = new MetadataMap<>();
OperationResourceInfo ori = findPreflightMethod(matchedResources, requestUri, httpMethod, values, m);
return ori == null ? null : ori.getAnnotatedMethod();
}
use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.
the class SseInterceptor method handleMessage.
public void handleMessage(Message message) {
// Not an SSE invocation, skipping it in favor of normal processing
if (message.get(SseEventSink.class) == null) {
return;
}
if (!isRequestor(message) && message.get(SseInterceptor.class) == null) {
message.put(SseInterceptor.class, this);
final Exchange exchange = message.getExchange();
OperationResourceInfo ori = (OperationResourceInfo) exchange.get(OperationResourceInfo.class.getName());
if (ori != null) {
Response.ResponseBuilder builder = Response.ok();
HttpServletResponse servletResponse = null;
final ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(message);
final Object response = message.get(AbstractHTTPDestination.HTTP_RESPONSE);
if (response instanceof HttpServletResponse) {
servletResponse = (HttpServletResponse) response;
builder = Response.status(servletResponse.getStatus());
for (final String header : servletResponse.getHeaderNames()) {
final Collection<String> headers = servletResponse.getHeaders(header);
addHeader(builder, header, headers);
}
}
// Run the filters
try {
final ResponseImpl responseImpl = (ResponseImpl) builder.build();
final Message outMessage = getOutMessage(message);
JAXRSUtils.runContainerResponseFilters(providerFactory, responseImpl, outMessage, ori, ori.getAnnotatedMethod());
if (servletResponse != null) {
servletResponse.setStatus(responseImpl.getStatus());
final Map<String, List<String>> userHeaders = CastUtils.cast((Map<?, ?>) outMessage.get(Message.PROTOCOL_HEADERS));
if (userHeaders != null) {
for (Map.Entry<String, List<String>> entry : userHeaders.entrySet()) {
setHeader(servletResponse, entry);
}
}
final MultivaluedMap<String, String> headers = responseImpl.getStringHeaders();
if (headers != null) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
setHeader(servletResponse, entry);
}
}
}
} catch (Throwable ex) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, ex.getMessage(), ex);
}
}
}
}
}
Aggregations