예제 #1
0
  /**
   * @param queryString a query string of the form n1=v1&n2=v2&... to decode. May be null.
   * @param acceptAmp -> "&" if true, "&" if false
   * @return a Map of String[] indexed by name, an empty Map if the query string was null
   */
  public static Map<String, String[]> decodeQueryString(
      final CharSequence queryString, final boolean acceptAmp) {

    final Map<String, String[]> result = new TreeMap<String, String[]>();
    if (queryString != null) {
      final Matcher matcher =
          acceptAmp ? PATTERN_AMP.matcher(queryString) : PATTERN_NO_AMP.matcher(queryString);
      int matcherEnd = 0;
      while (matcher.find()) {
        matcherEnd = matcher.end();
        try {
          // Group 0 is the whole match, e.g. a=b, while group 1 is the first group
          // denoted ( with parens ) in the expression.  Hence we start with group 1.
          final String name =
              URLDecoder.decode(matcher.group(1), NetUtils.STANDARD_PARAMETER_ENCODING);
          final String value =
              URLDecoder.decode(matcher.group(2), NetUtils.STANDARD_PARAMETER_ENCODING);

          StringUtils.addValueToStringArrayMap(result, name, value);
        } catch (UnsupportedEncodingException e) {
          // Should not happen as we are using a required encoding
          throw new OXFException(e);
        }
      }
      if (queryString.length() != matcherEnd) {
        // There was garbage at the end of the query.
        throw new OXFException("Malformed URL: " + queryString);
      }
    }
    return result;
  }
예제 #2
0
 /**
  * Get the last modification date of an open URLConnection.
  *
  * <p>This handles the (broken at some point in the Java libraries) case of the file: protocol.
  *
  * @return last modified timestamp "as is"
  */
 public static long getLastModified(URLConnection urlConnection) {
   try {
     long lastModified = urlConnection.getLastModified();
     if (lastModified == 0 && "file".equals(urlConnection.getURL().getProtocol()))
       lastModified =
           new File(
                   URLDecoder.decode(
                       urlConnection.getURL().getFile(), STANDARD_PARAMETER_ENCODING))
               .lastModified();
     return lastModified;
   } catch (UnsupportedEncodingException e) {
     // Should not happen as we are using a required encoding
     throw new OXFException(e);
   }
 }
예제 #3
0
 /**
  * Get the last modification date of a URL.
  *
  * @return last modified timestamp "as is"
  */
 public static long getLastModified(URL url) throws IOException {
   if ("file".equals(url.getProtocol())) {
     // Optimize file: access. Also, this prevents throwing an exception if the file doesn't exist
     // as we try to close the stream below.
     return new File(URLDecoder.decode(url.getFile(), STANDARD_PARAMETER_ENCODING)).lastModified();
   } else {
     // Use URLConnection
     final URLConnection urlConnection = url.openConnection();
     if (urlConnection instanceof HttpURLConnection)
       ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
     try {
       return getLastModified(urlConnection);
     } finally {
       final InputStream is = urlConnection.getInputStream();
       if (is != null) is.close();
     }
   }
 }
예제 #4
0
 /**
  * Parse the query string.
  *
  * @param map parsed key-values will be stored here
  * @param qs query string
  */
 private static void parseQueryString(final Map<String, String> map, final String qs) {
   if (qs == null) return;
   for (final String nv : qs.split("&")) {
     final String[] parts = nv.split("=");
     final String key = parts[0];
     String val = null;
     if (parts.length > 1) {
       val = parts[1];
       if (val != null) {
         try {
           val = URLDecoder.decode(val, Token.UTF8);
         } catch (final UnsupportedEncodingException ex) {
           throw new RuntimeException(ex);
         }
       }
     }
     map.put(key, val);
   }
 }