Java Code Examples for org.apache.http.client.utils.URLEncodedUtils#parse()

The following examples show how to use org.apache.http.client.utils.URLEncodedUtils#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestPredicateImpl.java    From bobcat with Apache License 2.0 6 votes vote down vote up
@Override
public boolean accepts(HttpRequest request) {
  boolean result = false;
  URI uri = URI.create(request.getUri());
  String path = uri.getPath();
  if (path != null && path.startsWith(urlPrefix)) {
    String query = uri.getQuery();
    if (expectedParams.isEmpty() && StringUtils.isEmpty(query)) {
      result = true;
    } else if (StringUtils.isNotEmpty(query)) {
      List<NameValuePair> params = URLEncodedUtils.parse(query, Charsets.UTF_8);
      result = hasAllExpectedParams(expectedParams, params);
    }
  }
  return result;
}
 
Example 2
Source File: APKExpansionPolicy.java    From vpn-over-dns with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<String, String>();
    try {
        URI rawExtras = new URI("?" + extras);
        List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
        for (NameValuePair item : extraList) {
            String name = item.getName();
            int i = 0;
            while (results.containsKey(name)) {
                name = item.getName() + ++i;
            }
            results.put(name, item.getValue());
        }
    } catch (URISyntaxException e) {
        Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
    }
    return results;
}
 
Example 3
Source File: APKExpansionPolicy.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<String, String>();
    try {
        URI rawExtras = new URI("?" + extras);
        List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
        for (NameValuePair item : extraList) {
            String name = item.getName();
            int i = 0;
            while (results.containsKey(name)) {
                name = item.getName() + ++i;
            }
            results.put(name, item.getValue());
        }
    } catch (URISyntaxException e) {
        Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
    }
    return results;
}
 
Example 4
Source File: APKExpansionPolicy.java    From text_converter with GNU General Public License v3.0 6 votes vote down vote up
private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<String, String>();
    try {
        URI rawExtras = new URI("?" + extras);
        List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
        for (NameValuePair item : extraList) {
            String name = item.getName();
            int i = 0;
            while (results.containsKey(name)) {
                name = item.getName() + ++i;
            }
            results.put(name, item.getValue());
        }
    } catch (URISyntaxException e) {
        Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
    }
    return results;
}
 
Example 5
Source File: HttpUtil.java    From common-project with Apache License 2.0 6 votes vote down vote up
public static Map<String, String> paraseURI(String uri) {
    try {
        if (!uri.startsWith("?")) {
            uri = "?".concat(uri);
        }
        HashMap<String, String> map = new HashMap<String, String>();
        List<NameValuePair> parse = URLEncodedUtils.parse(new URI(uri), CHARSET_NAME);
        for (NameValuePair nameValuePair : parse) {
            map.put(nameValuePair.getName(), nameValuePair.getValue());
        }
        return map;
    } catch (Exception e) {
        logger.error("parase uri error", e);
    }
    return null;
}
 
Example 6
Source File: AwsSigner4.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private String getCanonicalQuery(URI uri) {
    String query = uri.getQuery();
    if(query == null || query.isEmpty()) {
        return "";
    }
    List<NameValuePair> params = URLEncodedUtils.parse(query, StandardCharsets.UTF_8);
    Collections.sort(params, new Comparator<NameValuePair>() {
        @Override
        public int compare(NameValuePair l, NameValuePair r) {
            return l.getName().compareToIgnoreCase(r.getName());
        }
    });
    return URLEncodedUtils.format(params, StandardCharsets.UTF_8);
}
 
Example 7
Source File: ServerManagedPolicy.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<String, String>();
    try {
        URI rawExtras = new URI("?" + extras);
        List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
        for (NameValuePair item : extraList) {
            results.put(item.getName(), item.getValue());
        }
    } catch (URISyntaxException e) {
      Log.w(TAG, "Invalid syntax error while decoding extras data from server.");
    }
    return results;
}
 
Example 8
Source File: TwitterCallbackPath.java    From PYX-Reloaded with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    exchange.startBlocking();
    if (exchange.isInIoThread()) {
        exchange.dispatch(this);
        return;
    }

    try {
        String token = Utils.extractParam(exchange, "oauth_token");
        String verifier = Utils.extractParam(exchange, "oauth_verifier");

        if (token == null || verifier == null)
            throw new IllegalArgumentException("Missing token or verifier!");

        Cookie tokensCookie = exchange.getRequestCookies().get("PYX-Twitter-Token");
        if (tokensCookie == null)
            throw new IllegalArgumentException("Missing 'PYX-Twitter-Token' cookie!");

        List<NameValuePair> tokens = URLEncodedUtils.parse(tokensCookie.getValue(), Charset.forName("UTF-8"));
        if (!Objects.equals(token, Utils.get(tokens, "oauth_token")))
            throw new IllegalStateException("Missing token in cookie or tokens don't match!");

        String secret = Utils.get(tokens, "oauth_token_secret");
        if (secret == null)
            throw new IllegalArgumentException("Missing token secret in cookie!");

        OAuth1AccessToken accessToken = helper.accessToken(new OAuth1RequestToken(token, secret), verifier);
        CookieImpl cookie = new CookieImpl("PYX-Twitter-Token", accessToken.getRawResponse());
        cookie.setMaxAge(COOKIE_MAX_AGE);
        exchange.setResponseCookie(cookie);
        exchange.getResponseHeaders().add(Headers.LOCATION, REDIRECT_LOCATION);
        exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
    } catch (Throwable ex) {
        logger.error("Failed processing the request: " + exchange, ex);
        throw ex;
    }
}
 
Example 9
Source File: ComponentContainer.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
/**
 * Get list of expected query parameters.
 * 
 * @param pageUrl page URL annotation
 * @param expectUri expected landing page URI
 * @return list of expected query parameters
 */
private static List<NameValuePair> getExpectedParams(final PageUrl pageUrl, final URI expectUri) {
    List<NameValuePair> expectParams = new ArrayList<>();
    String[] params = pageUrl.params();
    if (params.length > 0) {
        for (String param : params) {
            String name = null;
            String value = null;
            String[] nameValueBits = param.split("=");
            switch (nameValueBits.length) {
                case NAME_WITH_VALUE:
                    value = nameValueBits[1].trim();
                    
                case PARAM_NAME_ONLY:
                    name = nameValueBits[0].trim();
                    expectParams.add(new BasicNameValuePair(name, value));
                    break;
                    
                default:
                    throw new IllegalArgumentException("Format of PageUrl parameter '" + param
                        + "' does not conform to template [name] or [name]=[pattern]");
            }
        }
    } else if (expectUri != null) {
        expectParams = URLEncodedUtils.parse(expectUri, "UTF-8");
    }
    return expectParams;
}
 
Example 10
Source File: AzkabanWorkflowClient.java    From dr-elephant with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the workflow execution id given the azkaban workflow url
 * @param azkabanWorkflowUrl The url of the azkaban workflow
 * @throws MalformedURLException
 * @throws URISyntaxException
 */
private void setExecutionId(String azkabanWorkflowUrl)
    throws MalformedURLException, URISyntaxException {
  List<NameValuePair> params = URLEncodedUtils.parse(new URI(azkabanWorkflowUrl), "UTF-8");
  for (NameValuePair param : params) {
    if (param.getName() == "execid") {
      this._executionId = param.getValue();
    }
  }
}
 
Example 11
Source File: FormsResource.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResultListDataRepresentation getForms(HttpServletRequest request) {

  // need to parse the filterText parameter ourselves, due to encoding issues with the default parsing.
  String filter = null;
  List<NameValuePair> params = URLEncodedUtils.parse(request.getQueryString(), Charset.forName("UTF-8"));
  if (params != null) {
    for (NameValuePair nameValuePair : params) {
      if ("filter".equalsIgnoreCase(nameValuePair.getName())) {
        filter = nameValuePair.getValue();
      }
    }
  }
  String validFilter = makeValidFilterText(filter);

  List<Model> models = null;
  if (validFilter != null) {
    models = modelRepository.findModelsByModelType(AbstractModel.MODEL_TYPE_FORM, validFilter);

  } else {
    models = modelRepository.findModelsByModelType(AbstractModel.MODEL_TYPE_FORM);
  }

  List<FormRepresentation> reps = new ArrayList<FormRepresentation>();

  for (Model model : models) {
    reps.add(new FormRepresentation(model));
  }

  Collections.sort(reps, new NameComparator());

  ResultListDataRepresentation result = new ResultListDataRepresentation(reps);
  result.setTotal(Long.valueOf(models.size()));
  return result;
}
 
Example 12
Source File: DecisionTablesResource.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public ResultListDataRepresentation getDecisionTables(HttpServletRequest request) {
  // need to parse the filterText parameter ourselves, due to encoding issues with the default parsing.
  String filter = null;
  List<NameValuePair> params = URLEncodedUtils.parse(request.getQueryString(), Charset.forName("UTF-8"));
  if (params != null) {
    for (NameValuePair nameValuePair : params) {
      if ("filter".equalsIgnoreCase(nameValuePair.getName())) {
        filter = nameValuePair.getValue();
      }
    }
  }
  return decisionTableService.getDecisionTables(filter);
}
 
Example 13
Source File: DelegationTokenAuthenticationFilter.java    From big-c with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static String getDoAs(HttpServletRequest request) {
  List<NameValuePair> list = URLEncodedUtils.parse(request.getQueryString(),
      UTF8_CHARSET);
  if (list != null) {
    for (NameValuePair nv : list) {
      if (DelegationTokenAuthenticatedURL.DO_AS.
          equalsIgnoreCase(nv.getName())) {
        return nv.getValue();
      }
    }
  }
  return null;
}
 
Example 14
Source File: WebsocketSessionContext.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the query string into a map of params.
 * @param queryString
 */
protected static Map<String, String> parseQueryString(String queryString) {
    Map<String, String> parsed = new HashMap<>();
    List<NameValuePair> list = URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8);
    for (NameValuePair nameValuePair : list) {
        parsed.put(nameValuePair.getName(), nameValuePair.getValue());
    }
    return parsed;
}
 
Example 15
Source File: DelegationTokenAuthenticationFilter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static String getDoAs(HttpServletRequest request) {
  List<NameValuePair> list = URLEncodedUtils.parse(request.getQueryString(),
      UTF8_CHARSET);
  if (list != null) {
    for (NameValuePair nv : list) {
      if (DelegationTokenAuthenticatedURL.DO_AS.
          equalsIgnoreCase(nv.getName())) {
        return nv.getValue();
      }
    }
  }
  return null;
}
 
Example 16
Source File: HttpServerUtilities.java    From Repeat with Apache License 2.0 5 votes vote down vote up
public static Map<String, String> parseGetParameters(String url) {
	try {
		List<NameValuePair> paramList = URLEncodedUtils.parse(new URI(url), StandardCharsets.UTF_8);
		Map<String, String>  params = new HashMap<>();
		for (NameValuePair param : paramList) {
			params.put(param.getName(), param.getValue());
		}
		return params;
	} catch (URISyntaxException e) {
		LOGGER.log(Level.WARNING, "Exception when parsing URL.", e);
		return null;
	}
}
 
Example 17
Source File: ComponentContainer.java    From Selenium-Foundation with Apache License 2.0 4 votes vote down vote up
/**
 * <b>INTERNAL</b>: Verify actual landing page against elements of the {@link PageUrl} annotation of the specified
 * page object.
 * 
 * @param pageObj page object whose landing page is to be verified
 * @param pageClass class of the specified page object
 * @param pageUrl {@link PageUrl} annotation for the indicate page class
 * @param targetUri configured target URI
 */
protected static final void verifyLandingPage(final Page pageObj, Class<?> pageClass, PageUrl pageUrl, URI targetUri) {
    String actual;
    String expect;
    
    URI actualUri = URI.create(pageObj.getCurrentUrl());
    String expectUrl = getPageUrl(pageUrl, targetUri);
    URI expectUri = (expectUrl != null) ? URI.create(expectUrl) : null;
    if (expectUri != null) {
        actual = actualUri.getScheme();
        expect = expectUri.getScheme();
        if ( ! StringUtils.equals(actual, expect)) {
            throw new LandingPageMismatchException(pageClass, "scheme", actual, expect);
        }
        
        actual = actualUri.getHost();
        expect = expectUri.getHost();
        if ( ! StringUtils.equals(actual, expect)) {
            throw new LandingPageMismatchException(pageClass, "host", actual, expect);
        }
        
        actual = actualUri.getUserInfo();
        expect = expectUri.getUserInfo();
        if ( ! StringUtils.equals(actual, expect)) {
            throw new LandingPageMismatchException(pageClass, "user info", actual, expect);
        }
        
        actual = Integer.toString(actualUri.getPort());
        expect = Integer.toString(expectUri.getPort());
        if ( ! StringUtils.equals(actual, expect)) {
            throw new LandingPageMismatchException(pageClass, "port", actual, expect);
        }
    }
    
    String pattern = pageUrl.pattern();
    if (!PLACEHOLDER.equals(pattern)) {
        actual = actualUri.getPath();
        String target = targetUri.getPath();
        if (StringUtils.isNotBlank(target)) {
            int actualLen = actual.length();
            int targetLen = target.length();
            
            if ((actualLen > targetLen) && (actual.startsWith(target))) {
                actual = actual.substring(targetLen);
            } else {
                throw new LandingPageMismatchException(pageClass, "base path", actual, target);
            }
        }
        
        if ( ! actual.matches(pattern)) {
            throw new LandingPageMismatchException(pageClass, pageObj.getCurrentUrl());
        }
    } else if (expectUri != null) {
        actual = actualUri.getPath();
        expect = expectUri.getPath();
        if ( ! StringUtils.equals(actual, expect)) {
            throw new LandingPageMismatchException(pageClass, "path", actual, expect);
        }
    }
    
    List<NameValuePair> actualParams = URLEncodedUtils.parse(actualUri, "UTF-8");
    
    for (NameValuePair expectPair : getExpectedParams(pageUrl, expectUri)) {
        if (!hasExpectedParam(actualParams, expectPair)) {
            throw new LandingPageMismatchException(
                            pageClass, "query parameter", actualUri.getQuery(), expectPair.toString());
        }
    }
}
 
Example 18
Source File: JavaProfile.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
private void parse(URL url) throws URISyntaxException {
    List<NameValuePair> values = URLEncodedUtils.parse(url.toURI(), StandardCharsets.UTF_8);
    String launchModeStr = findValueOf(values, "launchmode");
    launchMode = LaunchMode.valueOf(launchModeStr);
    for (int i = 1;; i++) {
        if (hasValueFor(values, "arg" + i)) {
            appArguments.add(findValueOf(values, "arg" + i));
        } else {
            break;
        }
    }
    if (hasValueFor(values, "command")) {
        command = findValueOf(values, "command");
    }
    if (hasValueFor(values, "executablejar")) {
        executableJar = findValueOf(values, "executablejar");
    }
    if (hasValueFor(values, "swt")) {
        startWindowTitle = findValueOf(values, "swt");
    }
    if (hasValueFor(values, "javahome")) {
        javaHome = findValueOf(values, "javahome");
    }
    if (hasValueFor(values, "vmcommand")) {
        vmCommand = findValueOf(values, "vmcommand");
    }
    for (int i = 1;; i++) {
        if (hasValueFor(values, "vmarg" + i)) {
            vmArguments.add(findValueOf(values, "vmarg" + i));
        } else {
            break;
        }
    }
    for (int i = 1;; i++) {
        if (hasValueFor(values, "cp" + i)) {
            classPathEntries.add(findValueOf(values, "cp" + i));
        } else {
            break;
        }
    }
    if (hasValueFor(values, "mainclass")) {
        mainClass = findValueOf(values, "mainclass");
    }
    for (int i = 1;; i++) {
        if (hasValueFor(values, "wsarg" + i)) {
            wsArguments.add(findValueOf(values, "wsarg" + i));
        } else {
            break;
        }
    }
    if (hasValueFor(values, "jnlp")) {
        jnlpPath = findValueOf(values, "jnlp");
    }
    jnlpNoLocalCopy = false;
    if (hasValueFor(values, "jnlpNoLocalCopy")) {
        jnlpNoLocalCopy = Boolean.parseBoolean(findValueOf(values, "jnlpNoLocalCopy"));
    }
    if (hasValueFor(values, "appleturl")) {
        appletURL = findValueOf(values, "appleturl");
    }
    if (hasValueFor(values, "nativeevents")) {
        nativeEvents = true;
    }
    if (hasValueFor(values, "launchtype")) {
        launchType = LaunchType.valueOf(findValueOf(values, "launchtype"));
    }
    if (hasValueFor(values, "keepLog")) {
        keepLog = Boolean.parseBoolean(findValueOf(values, "keepLog"));
    }
}
 
Example 19
Source File: ServerInterceptor.java    From careconnect-reference-implementation with Apache License 2.0 4 votes vote down vote up
@Override
public boolean incomingRequestPreProcessed(HttpServletRequest request, HttpServletResponse theResponse) {
    log.trace("incomingRequestPreProcessed "+request.getMethod());
    // 30/Apr/2018 Ignore for Binary endpoints
    if (request.getMethod() != null && (!request.getRequestURI().contains("Binary"))) {

        /* KGM 3/1/2018 This is now handled by CORS headers

       if (theRequest.getMethod().equals("OPTIONS"))
            throw new MethodNotAllowedException("request must use HTTP GET");
        */

        if (request.getContentType() != null ) {
           checkContentType(request.getContentType());
        }

        if (request.getHeader("Accept")  != null && !request.getHeader("Accept").equals("*/*")) {
            checkContentType(request.getHeader("Accept"));
        }

        if (request.getQueryString() != null) {


            List<NameValuePair> params = null;
            try {
                params = URLEncodedUtils.parse(new URI("http://dummy?" + request.getQueryString()), "UTF-8");
            } catch (Exception ex) {
            }

            ListIterator paramlist = params.listIterator();
            while (paramlist.hasNext()) {
                NameValuePair param = (NameValuePair) paramlist.next();
                if (param.getName().equals("_format"))
                    checkContentType(param.getValue());

            }
        }


        // May need to re-add this at a later date (probably in conjunction with a security uplift)
        /*
        KGM 3/1/2018 disabled for crucible testing

        if (request.getMethod().equals("POST") && request.getPathInfo() != null && request.getPathInfo().contains("_search"))
            throw new MethodNotAllowedException("request must use HTTP GET");
            */
    }
    return true;
}
 
Example 20
Source File: LocationUtil.java    From emodb with Apache License 2.0 4 votes vote down vote up
public static Optional<String> getZkConnectionStringOverride(URI location) {
    List<NameValuePair> params = URLEncodedUtils.parse(location, Charsets.UTF_8.name());
    for (NameValuePair pair : params) {
        if (ZK_CONNECTION_STRING_PARAM.equals(pair.getName())) {
            // Deterministically sort the hosts
            String[] hosts = pair.getValue().split(",");
            Arrays.sort(hosts);
            String zkConnectionString = Joiner.on(",").join(hosts);
            return Optional.of(zkConnectionString);
        }
    }
    return Optional.absent();
}