@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Map<String, Object> readFrom(Class<Map<String, Object>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { FormEncodingProvider delegate = new FormEncodingProvider(); MultivaluedMap<String, String> multi = (MultivaluedMap<String, String>) delegate.readFrom(MultivaluedMap.class, null, null, mediaType, httpHeaders, entityStream); Map<String, Object> map = Maps.newHashMapWithExpectedSize(multi.keySet().size()); for (String key : multi.keySet()) { List<String> value = multi.get(key); if (value.size() > 1) { map.put(key, Lists.newArrayList(value)); } else if (value.size() == 1) { map.put(key, Iterables.getOnlyElement(value)); } else { map.put(key, null); } } return map; } }
public static void writeForm(FormEncodingProvider<Form> provider, Form form, OutputStream os) throws Exception { provider.writeTo(form, Form.class, Form.class, new Annotation[]{}, MediaType.APPLICATION_FORM_URLENCODED_TYPE, new MetadataMap<String, Object>(), os); }
public T readFrom( Class<T> clazz, Type genericType, Annotation[] annotations, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException { if (is == null) { return null; } try { if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) { MultipartBody body = AttachmentUtils.getMultipartBody(mc); if (MultipartBody.class.isAssignableFrom(clazz)) { return clazz.cast(body); } else if (Attachment.class.isAssignableFrom(clazz)) { return clazz.cast(body.getRootAttachment()); } } MultivaluedMap<String, String> params = createMap(clazz); populateMap(params, annotations, is, mt, !keepEncoded(annotations)); validateMap(params); persistParamsOnMessage(params); return getFormObject(clazz, params); } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw ExceptionUtils.toBadRequestException(e, null); } }
@Override public Object readFrom(Class clazz, Type genericType, Annotation[] annotations, MediaType mt, MultivaluedMap headers, InputStream is) throws IOException { Predicate isContentTyoe = h -> CONTENT_TYPE.equalsIgnoreCase(h.toString()); Optional<String> element = headers.keySet() .stream() .filter(isContentTyoe) .findFirst(); if (!element.isPresent() || hasinValidValue(element.get(), MetadataMap.class.cast(headers))) { throw new WebApplicationException(new IllegalArgumentException("The Content-Type header is missing on this request you should define: " + CONTENT_TYPE), status(BAD_REQUEST).build()); } return super.readFrom(clazz, genericType, annotations, mt, headers, is); }
protected String getTokenFromFormData(Message message) { String method = (String)message.get(Message.HTTP_REQUEST_METHOD); String type = (String)message.get(Message.CONTENT_TYPE); if (type != null && MediaType.APPLICATION_FORM_URLENCODED.startsWith(type) && method != null && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT))) { try { FormEncodingProvider<Form> provider = new FormEncodingProvider<>(true); Form form = FormUtils.readForm(provider, message); MultivaluedMap<String, String> formData = form.asMap(); String token = formData.getFirst(OAuthConstants.ACCESS_TOKEN); if (token != null) { FormUtils.restoreForm(provider, form, message); return token; } } catch (Exception ex) { // the exception will be thrown below } } AuthorizationUtils.throwAuthorizationFailure(supportedSchemes, realm); return null; }
@Test public void testGetBookSAMLTokenInForm() throws Exception { String address = "https://localhost:" + PORT + "/samlform/bookstore/books"; FormEncodingProvider<Form> formProvider = new FormEncodingProvider<>(); formProvider.setExpectedEncoded(true); WebClient wc = createWebClient(address, new SamlFormOutInterceptor(), formProvider); wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML); try { Book book = wc.post(new Form(new MetadataMap<String, String>()).param("name", "CXF").param("id", "125"), Book.class); assertEquals(125L, book.getId()); } catch (WebApplicationException ex) { fail(ex.getMessage()); } catch (ProcessingException ex) { if (ex.getCause() != null && ex.getCause().getMessage() != null) { fail(ex.getCause().getMessage()); } else { fail(ex.getMessage()); } } }
FormEncodingProvider<?> provider = new FormEncodingProvider<Object>(); provider.setConsumeMediaTypes(Arrays.asList("text/html"));
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) { return isSupported(type, genericType, annotations, mt); }
@SuppressWarnings("unchecked") public void writeTo(T obj, Class<?> c, Type t, Annotation[] anns, MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException { MultivaluedMap<String, String> map = (MultivaluedMap<String, String>)(obj instanceof Form ? ((Form)obj).asMap() : obj); boolean encoded = keepEncoded(anns); String enc = HttpUtils.getSetEncoding(mt, headers, StandardCharsets.UTF_8.name()); FormUtils.writeMapToOutputStream(map, os, enc, encoded); }
public static Form readForm(FormEncodingProvider<Form> provider, Message message) throws Exception { return provider.readFrom(Form.class, Form.class, new Annotation[]{}, MediaType.APPLICATION_FORM_URLENCODED_TYPE, new MetadataMap<String, String>(), message.getContent(InputStream.class)); }
protected String getTokenFromFormData(Message message) { String method = (String)message.get(Message.HTTP_REQUEST_METHOD); String type = (String)message.get(Message.CONTENT_TYPE); if (type != null && MediaType.APPLICATION_FORM_URLENCODED.startsWith(type) && method != null && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT))) { try { FormEncodingProvider<Form> provider = new FormEncodingProvider<>(true); Form form = FormUtils.readForm(provider, message); MultivaluedMap<String, String> formData = form.asMap(); String token = formData.getFirst(OAuthConstants.ACCESS_TOKEN); if (token != null) { FormUtils.restoreForm(provider, form, message); return token; } } catch (Exception ex) { // the exception will be thrown below } } AuthorizationUtils.throwAuthorizationFailure(supportedSchemes, realm); return null; }
@Test public void testGetBookPreviousSAMLTokenInForm() throws Exception { String address = "https://localhost:" + PORT + "/samlform/bookstore/books"; FormEncodingProvider<Form> formProvider = new FormEncodingProvider<>(); formProvider.setExpectedEncoded(true); WebClient wc = createWebClientForExistingToken(address, new SamlFormOutInterceptor(), formProvider); wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_XML); try { Book book = wc.post(new Form(new MetadataMap<String, String>()).param("name", "CXF").param("id", "125"), Book.class); assertEquals(125L, book.getId()); } catch (WebApplicationException ex) { fail(ex.getMessage()); } catch (ProcessingException ex) { if (ex.getCause() != null && ex.getCause().getMessage() != null) { fail(ex.getCause().getMessage()); } else { fail(ex.getMessage()); } } }
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) { return isSupported(type, genericType, annotations, mt); }
@SuppressWarnings("unchecked") public void writeTo(T obj, Class<?> c, Type t, Annotation[] anns, MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException { MultivaluedMap<String, String> map = (MultivaluedMap<String, String>)(obj instanceof Form ? ((Form)obj).getData() : obj); boolean encoded = keepEncoded(anns); String enc = HttpUtils.getSetEncoding(mt, headers, "UTF-8"); FormUtils.writeMapToOutputStream(map, os, enc, encoded); }
public T readFrom( Class<T> clazz, Type genericType, Annotation[] annotations, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException { if (is == null) { return null; } try { if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) { MultipartBody body = AttachmentUtils.getMultipartBody(mc); if (MultipartBody.class.isAssignableFrom(clazz)) { return clazz.cast(body); } else if (Attachment.class.isAssignableFrom(clazz)) { return clazz.cast(body.getRootAttachment()); } } MultivaluedMap<String, String> params = createMap(clazz); populateMap(params, annotations, is, mt, !keepEncoded(annotations)); validateMap(params); persistParamsOnMessage(params); return getFormObject(clazz, params); } catch (WebApplicationException e) { throw e; } catch (Exception e) { throw ExceptionUtils.toBadRequestException(e, null); } }
public static Form readForm(FormEncodingProvider<Form> provider, Message message) throws Exception { return provider.readFrom(Form.class, Form.class, new Annotation[]{}, MediaType.APPLICATION_FORM_URLENCODED_TYPE, new MetadataMap<String, String>(), message.getContent(InputStream.class)); }
protected static void initFactory(ProviderFactory factory) { // ensure to not load providers not available in a module environment if not needed factory.setProviders(false, false, new BinaryDataProvider<Object>(), new SourceProvider<Object>(), tryCreateInstance("org.apache.cxf.jaxrs.provider.DataSourceProvider"), new FormEncodingProvider<Object>(), new StringTextProvider(), new PrimitiveTextProvider<Object>(), tryCreateInstance(JAXB_PROVIDER_NAME), tryCreateInstance("org.apache.cxf.jaxrs.provider.JAXBElementTypedProvider"), tryCreateInstance("org.apache.cxf.jaxrs.provider.MultipartProvider")); Object prop = factory.getBus().getProperty("skip.default.json.provider.registration"); if (!PropertyUtils.isTrue(prop)) { factory.setProviders(false, false, createProvider(JSON_PROVIDER_NAME, factory.getBus())); } }
public static void writeForm(FormEncodingProvider<Form> provider, Form form, OutputStream os) throws Exception { provider.writeTo(form, Form.class, Form.class, new Annotation[]{}, MediaType.APPLICATION_FORM_URLENCODED_TYPE, new MetadataMap<String, Object>(), os); }
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) { return isSupported(type, genericType, annotations, mt); }
public OOBAuthorizationResponse readFrom( Class<OOBAuthorizationResponse> clazz, Type genericType, Annotation[] annotations, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException { Form form = formProvider.readFrom(Form.class, Form.class, annotations, mt, headers, is); MultivaluedMap<String, String> data = form.asMap(); OOBAuthorizationResponse resp = new OOBAuthorizationResponse(); resp.setRequestToken(data.getFirst(OAuth.OAUTH_TOKEN)); resp.setVerifier(data.getFirst(OAuth.OAUTH_VERIFIER)); resp.setState(data.getFirst(OAuthConstants.X_OAUTH_STATE)); return resp; }