use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class JAXRSMultipartTest method testUploadFileWithSemicolonName.
@Test
public void testUploadFileWithSemicolonName() throws Exception {
String address = "http://localhost:" + PORT + "/bookstore/books/file/semicolon";
WebClient client = WebClient.create(address);
client.type("multipart/form-data").accept("text/plain");
ContentDisposition cd = new ContentDisposition("attachment;name=\"a\";filename=\"a;txt\"");
MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
headers.putSingle("Content-Disposition", cd.toString());
Attachment att = new Attachment(new ByteArrayInputStream("file name with semicolon".getBytes()), headers);
MultipartBody body = new MultipartBody(att);
String partContent = client.post(body, String.class);
assertEquals("file name with semicolon, filename:" + "a;txt", partContent);
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class JAXRSInInterceptor method processRequest.
private void processRequest(Message message, Exchange exchange) throws IOException {
ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(message);
RequestPreprocessor rp = providerFactory.getRequestPreprocessor();
if (rp != null) {
rp.preprocess(message, new UriInfoImpl(message, null));
}
// Global pre-match request filters
if (JAXRSUtils.runContainerRequestFilters(providerFactory, message, true, null)) {
return;
}
// HTTP method
String httpMethod = HttpUtils.getProtocolHeader(message, Message.HTTP_REQUEST_METHOD, HttpMethod.POST, true);
// Path to match
String rawPath = HttpUtils.getPathToMatch(message, true);
Map<String, List<String>> protocolHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
// Content-Type
String requestContentType = null;
List<String> ctHeaderValues = protocolHeaders.get(Message.CONTENT_TYPE);
if (ctHeaderValues != null && !ctHeaderValues.isEmpty()) {
requestContentType = ctHeaderValues.get(0);
message.put(Message.CONTENT_TYPE, requestContentType);
}
if (requestContentType == null) {
requestContentType = (String) message.get(Message.CONTENT_TYPE);
if (requestContentType == null) {
requestContentType = MediaType.WILDCARD;
}
}
// Accept
String acceptTypes = null;
List<String> acceptHeaderValues = protocolHeaders.get(Message.ACCEPT_CONTENT_TYPE);
if (acceptHeaderValues != null && !acceptHeaderValues.isEmpty()) {
acceptTypes = acceptHeaderValues.get(0);
message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
}
if (acceptTypes == null) {
acceptTypes = HttpUtils.getProtocolHeader(message, Message.ACCEPT_CONTENT_TYPE, null);
if (acceptTypes == null) {
acceptTypes = "*/*";
message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
}
}
List<MediaType> acceptContentTypes = null;
try {
acceptContentTypes = JAXRSUtils.sortMediaTypes(acceptTypes, JAXRSUtils.MEDIA_TYPE_Q_PARAM);
} catch (IllegalArgumentException ex) {
throw ExceptionUtils.toNotAcceptableException(null, null);
}
exchange.put(Message.ACCEPT_CONTENT_TYPE, acceptContentTypes);
// 1. Matching target resource class
List<ClassResourceInfo> resources = JAXRSUtils.getRootResources(message);
Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources = JAXRSUtils.selectResourceClass(resources, rawPath, message);
if (matchedResources == null) {
org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("NO_ROOT_EXC", BUNDLE, message.get(Message.REQUEST_URI), rawPath);
Level logLevel = JAXRSUtils.getExceptionLogLevel(message, NotFoundException.class);
LOG.log(logLevel == null ? Level.FINE : logLevel, errorMsg.toString());
Response resp = JAXRSUtils.createResponse(resources, message, errorMsg.toString(), Response.Status.NOT_FOUND.getStatusCode(), false);
throw ExceptionUtils.toNotFoundException(null, resp);
}
MultivaluedMap<String, String> matchedValues = new MetadataMap<String, String>();
OperationResourceInfo ori = null;
try {
ori = JAXRSUtils.findTargetMethod(matchedResources, message, httpMethod, matchedValues, requestContentType, acceptContentTypes, true, true);
setExchangeProperties(message, exchange, ori, matchedValues, resources.size());
} catch (WebApplicationException ex) {
if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), httpMethod)) {
Response response = JAXRSUtils.createResponse(resources, null, null, 200, true);
exchange.put(Response.class, response);
return;
}
throw ex;
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Request path is: " + rawPath);
LOG.fine("Request HTTP method is: " + httpMethod);
LOG.fine("Request contentType is: " + requestContentType);
LOG.fine("Accept contentType is: " + acceptTypes);
LOG.fine("Found operation: " + ori.getMethodToInvoke().getName());
}
// Global and name-bound post-match request filters
if (!ori.isSubResourceLocator() && JAXRSUtils.runContainerRequestFilters(providerFactory, message, false, ori.getNameBindings())) {
return;
}
// Process parameters
List<Object> params = JAXRSUtils.processParameters(ori, matchedValues, message);
message.setContent(List.class, params);
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class OAuthUtils method addParametersIfNeeded.
public static void addParametersIfNeeded(MessageContext mc, HttpServletRequest request, OAuthMessage oAuthMessage) throws IOException {
List<Entry<String, String>> params = oAuthMessage.getParameters();
String enc = oAuthMessage.getBodyEncoding();
enc = enc == null ? StandardCharsets.UTF_8.name() : enc;
if (params.isEmpty() && MediaType.APPLICATION_FORM_URLENCODED_TYPE.isCompatible(MediaType.valueOf(oAuthMessage.getBodyType()))) {
InputStream stream = mc != null ? mc.getContent(InputStream.class) : oAuthMessage.getBodyAsStream();
String body = FormUtils.readBody(stream, enc);
MultivaluedMap<String, String> map = new MetadataMap<String, String>();
FormUtils.populateMapFromString(map, PhaseInterceptorChain.getCurrentMessage(), body, enc, true, request);
for (String key : map.keySet()) {
oAuthMessage.addParameter(key, map.getFirst(key));
}
} else {
// This path will most likely work only for the AuthorizationRequestService
// when processing a user confirmation with only 3 parameters expected
String ct = request.getContentType();
if (ct != null && MediaType.APPLICATION_FORM_URLENCODED.equals(ct)) {
Map<String, List<String>> map = new HashMap<>();
for (Entry<String, String> param : params) {
map.put(param.getKey(), Collections.singletonList(param.getValue()));
}
FormUtils.logRequestParametersIfNeeded(map, enc);
}
}
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class AccessTokenIntrospectionClient method validateAccessToken.
public AccessTokenValidation validateAccessToken(MessageContext mc, String authScheme, String authSchemeData, MultivaluedMap<String, String> extraProps) throws OAuthServiceException {
WebClient client = WebClient.fromClient(tokenValidatorClient, true);
MultivaluedMap<String, String> props = new MetadataMap<String, String>();
props.putSingle(OAuthConstants.TOKEN_ID, authSchemeData);
try {
TokenIntrospection response = client.post(props, TokenIntrospection.class);
return convertIntrospectionToValidation(response);
} catch (WebApplicationException ex) {
throw new OAuthServiceException(ex);
}
}
use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.
the class OAuthJSONProviderTest method doReadClientAccessToken.
@SuppressWarnings({ "unchecked", "rawtypes" })
public ClientAccessToken doReadClientAccessToken(String response, String expectedTokenType, Map<String, String> expectedParams) throws Exception {
OAuthJSONProvider provider = new OAuthJSONProvider();
ClientAccessToken token = (ClientAccessToken) provider.readFrom((Class) ClientAccessToken.class, ClientAccessToken.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(response.getBytes()));
assertEquals("1234", token.getTokenKey());
assertTrue(expectedTokenType.equalsIgnoreCase(token.getTokenType()));
assertEquals("5678", token.getRefreshToken());
assertEquals(12345, token.getExpiresIn());
assertEquals("read", token.getApprovedScope());
Map<String, String> extraParams = token.getParameters();
if (expectedParams != null) {
assertEquals(expectedParams, extraParams);
}
assertEquals("http://abc", extraParams.get("my_parameter"));
return token;
}
Aggregations