protected MultivaluedMap<String, String> toRequestState(ContainerRequestContext rc, UriInfo ui) { MultivaluedMap<String, String> requestState = new MetadataMap<>(); requestState.putAll(ui.getQueryParameters(decodeRequestParameters)); if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.isCompatible(rc.getMediaType())) { String body = FormUtils.readBody(rc.getEntityStream(), StandardCharsets.UTF_8.name()); FormUtils.populateMapFromString(requestState, JAXRSUtils.getCurrentMessage(), body, StandardCharsets.UTF_8.name(), decodeRequestParameters); } return requestState; }
public static void populateMapFromString(MultivaluedMap<String, String> params, Message m, String postBody, String enc, boolean decode, javax.servlet.http.HttpServletRequest request) { if (!StringUtils.isEmpty(postBody)) { populateMapFromString(params, m, postBody, enc, decode); } else if (request != null && MessageUtils.getContextualBoolean(m, FORM_PARAMS_FROM_HTTP_PARAMS, true)) { for (Enumeration<String> en = request.getParameterNames(); en.hasMoreElements();) { String paramName = en.nextElement(); String[] values = request.getParameterValues(paramName); params.put(HttpUtils.urlDecode(paramName), Arrays.asList(values)); } logRequestParametersIfNeeded(params, enc); } }
private Form readFormData(Message message) { try { return FormUtils.readForm(provider, message); } catch (Exception ex) { throw ExceptionUtils.toNotAuthorizedException(null, null); } }
MultipartBody body = AttachmentUtils.getMultipartBody(mc, attachmentDir, attachmentThreshold, attachmentMaxSize); FormUtils.populateMapFromMultipart(params, body, PhaseInterceptorChain.getCurrentMessage(), decode); } else { FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode); } else { FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode,
InputStream stream = mc != null ? mc.getContent(InputStream.class) : oAuthMessage.getBodyAsStream(); String body = FormUtils.readBody(stream, enc); MultivaluedMap<String, String> map = new MetadataMap<>(); FormUtils.populateMapFromString(map, PhaseInterceptorChain.getCurrentMessage(), body, enc, true, request); for (String key : map.keySet()) { map.put(param.getKey(), Collections.singletonList(param.getValue())); FormUtils.logRequestParametersIfNeeded(map, enc);
String body = FormUtils.readBody(m.getContent(InputStream.class), enc); FormUtils.populateMapFromStringOrHttpRequest(params, m, body, enc, decode); } else { if ("multipart".equalsIgnoreCase(mt.getType()) && MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mt)) { MultipartBody body = AttachmentUtils.getMultipartBody(mc); FormUtils.populateMapFromMultipart(params, body, m, decode); } else { org.apache.cxf.common.i18n.Message errorMsg =
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; }
@Override public void filter(ContainerRequestContext context) { Message message = JAXRSUtils.getCurrentMessage(); Form form = readFormData(message); MultivaluedMap<String, String> formData = form.asMap(); String assertion = formData.getFirst(SAML_ELEMENT); handleToken(message, assertion); // redirect if needed String samlRequestURI = formData.getFirst(SAML_RELAY_STATE); if (samlRequestURI != null) { // RelayState may actually represent a reference to a transient local state // containing the actual REQUEST URI client was using before being redirected // back to IDP - at the moment assume it's URI UriInfoImpl ui = new UriInfoImpl(message); if (!samlRequestURI.startsWith(ui.getBaseUri().toString())) { context.abortWith(Response.status(302).location(URI.create(samlRequestURI)).build()); return; } } formData.remove(SAML_ELEMENT); formData.remove(SAML_RELAY_STATE); // restore input stream try { FormUtils.restoreForm(provider, form, message); } catch (Exception ex) { throwFault(ex.getMessage(), ex); } }
public static void populateMapFromStringOrHttpRequest(MultivaluedMap<String, String> params, Message m, String postBody, String enc, boolean decode) { HttpServletRequest request = (HttpServletRequest)m.get(AbstractHTTPDestination.HTTP_REQUEST); populateMapFromString(params, m, postBody, enc, decode, request); }
public String toString() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { FormUtils.writeMapToOutputStream(map, bos, "UTF-8", false); return bos.toString("UTF-8"); } catch (Exception ex) { // will not happen } return ""; } }
public Form set(String name, Object value) { FormUtils.addPropertyToForm(map, name, value); return this; }
public static void populateMapFromString(MultivaluedMap<String, String> params, Message m, String postBody, String enc, boolean decode) { List<String> parts = Arrays.asList(StringUtils.split(postBody, "&")); checkNumberOfParts(m, parts.size()); for (String part : parts) { String[] keyValue = new String[2]; int index = part.indexOf("="); if (index != -1) { keyValue[0] = part.substring(0, index); keyValue[1] = index + 1 < part.length() ? part.substring(index + 1) : ""; } else { keyValue[0] = part; keyValue[1] = ""; } String name = HttpUtils.urlDecode(keyValue[0], enc); if (decode) { params.add(name, HttpUtils.urlDecode(keyValue[1], enc)); } else { params.add(name, keyValue[1]); } } }
public static MultivaluedMap<String, String> populateFormMap(MessageContext mc, boolean errorIfMissing) { MultivaluedMap<String, String> data = new MetadataMap<>(); FormUtils.populateMapFromMultipart(data, AttachmentUtils.getMultipartBody(mc), PhaseInterceptorChain.getCurrentMessage(), true); return data; }
public static void restoreForm(FormEncodingProvider<Form> provider, Form form, Message message) throws Exception { CachedOutputStream os = new CachedOutputStream(); writeForm(provider, form, os); message.setContent(InputStream.class, os.getInputStream()); }
MultipartBody body = AttachmentUtils.getMultipartBody(mc, attachmentDir, attachmentThreshold, attachmentMaxSize); FormUtils.populateMapFromMultipart(params, body, PhaseInterceptorChain.getCurrentMessage(), decode); } else { FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode); } else { FormUtils.populateMapFromString(params, PhaseInterceptorChain.getCurrentMessage(), FormUtils.readBody(is, enc), enc, decode,
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; }
@Override public void filter(ContainerRequestContext context) { Message message = JAXRSUtils.getCurrentMessage(); Form form = readFormData(message); MultivaluedMap<String, String> formData = form.asMap(); String assertion = formData.getFirst(SAML_ELEMENT); handleToken(message, assertion); // redirect if needed String samlRequestURI = formData.getFirst(SAML_RELAY_STATE); if (samlRequestURI != null) { // RelayState may actually represent a reference to a transient local state // containing the actual REQUEST URI client was using before being redirected // back to IDP - at the moment assume it's URI UriInfoImpl ui = new UriInfoImpl(message); if (!samlRequestURI.startsWith(ui.getBaseUri().toString())) { context.abortWith(Response.status(302).location(URI.create(samlRequestURI)).build()); return; } } formData.remove(SAML_ELEMENT); formData.remove(SAML_RELAY_STATE); // restore input stream try { FormUtils.restoreForm(provider, form, message); } catch (Exception ex) { throwFault(ex.getMessage(), ex); } }
public static String formToString(Form form) { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { FormUtils.writeMapToOutputStream(form.asMap(), bos, StandardCharsets.UTF_8.name(), false); return bos.toString(StandardCharsets.UTF_8.name()); } catch (Exception ex) { // will not happen } return ""; }
protected void addFormValue(MultivaluedMap<String, String> form, String name, Object pValue, Annotation[] anns) { if (pValue != null) { if (InjectionUtils.isSupportedCollectionOrArray(pValue.getClass())) { Collection<?> c = pValue.getClass().isArray() ? Arrays.asList((Object[]) pValue) : (Collection<?>) pValue; for (Iterator<?> it = c.iterator(); it.hasNext();) { FormUtils.addPropertyToForm(form, name, convertParamValue(it.next(), anns)); } } else { FormUtils.addPropertyToForm(form, name, name.isEmpty() ? pValue : convertParamValue(pValue, anns)); } } }
public static void populateMapFromString(MultivaluedMap<String, String> params, Message m, String postBody, String enc, boolean decode) { if (StringUtils.isEmpty(postBody)) { return; } String[] parts = postBody.split("&"); checkNumberOfParts(m, parts.length); for (String part : parts) { String[] keyValue = new String[2]; int index = part.indexOf("="); if (index != -1) { keyValue[0] = part.substring(0, index); keyValue[1] = index + 1 < part.length() ? part.substring(index + 1) : ""; } else { keyValue[0] = part; keyValue[1] = ""; } String name = HttpUtils.urlDecode(keyValue[0], enc); if (decode) { params.add(name, HttpUtils.urlDecode(keyValue[1], enc)); } else { params.add(name, keyValue[1]); } } }