List of usage examples for org.apache.http.message BasicHeaderValueParser parseElements
public HeaderElement[] parseElements(CharArrayBuffer charArrayBuffer, ParserCursor parserCursor)
From source file:com.google.android.feeds.ContentHandlerUtils.java
/** * Returns the character set of the content provided by the given * {@link URLConnection}.//from www. ja v a2 s. c om * * @throws IOException if the character set cannot be determined. */ public static String getCharSet(URLConnection connection) throws IOException { String contentType = connection.getContentType(); if (contentType != null) { HeaderValueParser parser = new BasicHeaderValueParser(); HeaderElement[] values = BasicHeaderValueParser.parseElements(contentType, parser); if (values.length > 0) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { return param.getValue(); } } } if (connection instanceof HttpURLConnection) { return HTTP.DEFAULT_CONTENT_CHARSET; } else { throw new IOException("Unabled to determine character encoding"); } }
From source file:org.expath.servlex.functions.ParseHeaderValueCallTest.java
@Test public void reproduceTheAlgorithm() { String value = "attachment; filename=\"fname.ext\""; HeaderValueParser parser = new BasicHeaderValueParser(); HeaderElement[] elems = BasicHeaderValueParser.parseElements(value, parser); for (HeaderElement e : elems) { System.err.println("element: " + e.getName()); if (e.getValue() != null) { System.err.println(" : " + e.getValue()); }//from w w w . java 2 s . c o m for (NameValuePair p : e.getParameters()) { System.err.println(" param: " + p.getName()); if (p.getValue() != null) { System.err.println(" : " + p.getValue()); } } } }
From source file:edu.cornell.mannlib.vitro.webapp.utils.http.ContentTypeUtil.java
/** * The order of items in the Accept header is not important. We rely on the * specificity of the match and the "q" factor, in that order. * //from w w w .j av a 2s . co m * Since q ranges between 1.0 and 0.001, we add a specificity offset of 2, 3 * or 4. That way, matches with equal specificity are decided by q factor. */ public static Set<AcceptableType> parseAcceptHeader(String acceptHeader) throws AcceptHeaderParsingException { if (acceptHeader == null || acceptHeader.trim().isEmpty()) { return Collections.singleton(new AcceptableType("*/*", "1.0")); } HeaderElement[] elements = BasicHeaderValueParser.parseElements(acceptHeader, null); Set<AcceptableType> acceptableTypes = new HashSet<>(); for (HeaderElement he : elements) { String name = he.getName(); NameValuePair qPair = he.getParameterByName("q"); String qString = (qPair == null) ? "1.0" : qPair.getValue(); acceptableTypes.add(new AcceptableType(name, qString)); } return acceptableTypes; }
From source file:org.ovirt.engine.api.common.util.DetailHelper.java
/** * Determines what details to include or exclude from the {@code detail} parameter of the {@code Accept} header and * from the {@code detail} matrix or query parameters. * * @param headers the object that gives access to the HTTP headers of the request, may be {@code null} in which case * it will be completely ignored/* w ww.ja v a 2 s . com*/ * @param uri the object that gives access to the URI information, may be {@code null} in which case it will be * completely ignored * @return the set containing the extracted information, may be empty, but never {@code null} */ public static Set<String> getDetails(HttpHeaders headers, UriInfo uri) { // We will collect the detail specifications obtained from different places into this list, for later // processing: List<String> allSpecs = new ArrayList<>(0); // Try to extract the specification of what to include/exclude from the accept header: if (headers != null) { List<String> headerValues = headers.getRequestHeader(ACCEPT); if (CollectionUtils.isNotEmpty(headerValues)) { for (String headerValue : headerValues) { HeaderElement[] headerElements = BasicHeaderValueParser.parseElements(headerValue, null); if (ArrayUtils.isNotEmpty(headerElements)) { for (HeaderElement headerElement : headerElements) { for (NameValuePair parameter : headerElement.getParameters()) { if (StringUtils.equalsIgnoreCase(parameter.getName(), DETAIL)) { String spec = parameter.getValue(); if (StringUtils.isNotEmpty(spec)) { allSpecs.add(parameter.getValue()); } } } } } } } } // Try also from the matrix parameters: if (uri != null) { List<PathSegment> segments = uri.getPathSegments(); if (CollectionUtils.isNotEmpty(segments)) { PathSegment last = segments.get(segments.size() - 1); if (last != null) { MultivaluedMap<String, String> parameters = last.getMatrixParameters(); if (MapUtils.isNotEmpty(parameters)) { List<String> specs = parameters.get(DETAIL); if (CollectionUtils.isNotEmpty(specs)) { allSpecs.addAll(specs); } } } } } // Try also from the query parameters: if (uri != null) { MultivaluedMap<String, String> parameters = uri.getQueryParameters(); if (MapUtils.isNotEmpty(parameters)) { List<String> specs = parameters.get(DETAIL); if (CollectionUtils.isNotEmpty(specs)) { allSpecs.addAll(specs); } } } // Process all the obtained detail specifications: return parseDetails(allSpecs); }
From source file:org.expath.servlex.tools.ContentType.java
private void init(String value) throws TechnicalException { LOG.debug("Content type - original : " + value); myOriginal = value;//from www . j a va 2 s.c om HeaderValueParser parser = new BasicHeaderValueParser(); HeaderElement[] elems = BasicHeaderValueParser.parseElements(value, parser); if (elems.length != 1) { String msg = "Content-Type does not have exactly one element, it has "; throw new TechnicalException(msg + elems.length + " (" + value + ")"); } String type = elems[0].getName(); int slash = type.indexOf('/'); if (slash <= 0) { String msg = "Content-Type does not contain any slash character (" + value + ")"; throw new TechnicalException(msg); } myMainType = type.substring(0, slash); mySubType = type.substring(slash + 1); NameValuePair param = elems[0].getParameterByName("charset"); if (param != null) { myCharset = param.getValue(); } LOG.debug("Content type - main type: " + myMainType); LOG.debug("Content type - sub type : " + mySubType); LOG.debug("Content type - charset : " + myCharset); }
From source file:com.gistlabs.mechanize.util.apache.ContentType.java
/** * Parses textual representation of <code>Content-Type</code> value. * * @param s text/*www . j a v a 2 s.c o m*/ * @return content type * @throws ParseException if the given text does not represent a valid * <code>Content-Type</code> value. */ public static ContentType parse(final String s) throws ParseException, UnsupportedCharsetException { if (s == null) { throw new IllegalArgumentException("Content type may not be null"); } HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null); if (elements.length > 0) { return create(elements[0]); } else { throw new ParseException("Invalid content type: " + s); } }
From source file:org.ovirt.engine.api.common.security.CSRFProtectionFilter.java
/** * Checks if the headers contained in the given request indicate that the user wants to enable protection. This * means checking if the {@code Prefer} header exists and has at least one {@code csrf-protection} element. For * example:/*from w ww. j a v a2s . c o m*/ * * <pre> * GET /ovirt-engine/api HTTP/1.1 * Host: ovirt.example.com * Prefer: persistent-auth, csrf-protection * </pre> * * @param request the HTTP request to check * @return {@code true} if the request contains headers that indicate that protection should be enabled, * {@code false} otherwise */ private boolean isProtectionRequested(HttpServletRequest request) { Enumeration<String> headerValues = request.getHeaders(PREFER_HEADER); while (headerValues.hasMoreElements()) { String headerValue = headerValues.nextElement(); HeaderElement[] headerElements = BasicHeaderValueParser.parseElements(headerValue, null); for (HeaderElement headerElement : headerElements) { String elementName = headerElement.getName(); if (PREFER_ELEMENT.equalsIgnoreCase(elementName)) { return true; } } } return false; }
From source file:securitydigest.TestDigestScheme.java
private static Map<String, String> parseAuthResponse(final Header authResponse) { String s = authResponse.getValue(); if (!s.startsWith("Digest ")) { return null; }/* ww w .ja v a2s .c o m*/ HeaderElement[] elements = BasicHeaderValueParser.parseElements(s.substring(7), null); Map<String, String> map = new HashMap(elements.length); for (int i = 0; i < elements.length; i++) { HeaderElement element = elements[i]; map.put(element.getName(), element.getValue()); } return map; }