Java Code Examples for org.apache.http.NameValuePair#getName()

The following examples show how to use org.apache.http.NameValuePair#getName() . These examples are extracted from open source projects. 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 Project: hop   File: HttpPost.java    License: Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
String getRequestBodyParamsAsStr( NameValuePair[] pairs, String charset ) throws HopException {
  StringBuffer buf = new StringBuffer();
  try {
    for ( int i = 0; i < pairs.length; ++i ) {
      NameValuePair pair = pairs[ i ];
      if ( pair.getName() != null ) {
        if ( i > 0 ) {
          buf.append( "&" );
        }

        buf.append( URLEncoder.encode( pair.getName(), !StringUtil.isEmpty( charset ) ? charset : DEFAULT_ENCODING ) );
        buf.append( "=" );
        if ( pair.getValue() != null ) {
          buf.append( URLEncoder.encode( pair.getValue(), !StringUtil.isEmpty( charset ) ? charset : DEFAULT_ENCODING ) );
        }
      }
    }
    return buf.toString();
  } catch ( UnsupportedEncodingException e ) {
    throw new HopException( e.getMessage(), e.getCause() );
  }
}
 
Example 2
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
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
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 Project: cosmic   File: UriUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean cifsCredentialsPresent(final URI uri) {
    final List<NameValuePair> args = URLEncodedUtils.parse(uri, "UTF-8");
    boolean foundUser = false;
    boolean foundPswd = false;
    for (final NameValuePair nvp : args) {
        final String name = nvp.getName();
        if (name.equals("user")) {
            foundUser = true;
            s_logger.debug("foundUser is" + foundUser);
        } else if (name.equals("password")) {
            foundPswd = true;
            s_logger.debug("foundPswd is" + foundPswd);
        }
    }
    return (foundUser && foundPswd);
}
 
Example 6
Source Project: patreon-java   File: PatreonAPI.java    License: Apache License 2.0 6 votes vote down vote up
public String getNextCursorFromDocument(JSONAPIDocument document) {
  Links links = document.getLinks();
  if (links == null) {
    return null;
  }
  Link nextLink = links.getNext();
  if (nextLink == null) {
    return null;
  }
  String nextLinkString = nextLink.toString();
  try {
    List<NameValuePair> queryParameters = URLEncodedUtils.parse(new URI(nextLinkString), "utf8");
    for (NameValuePair pair : queryParameters) {
      String name = pair.getName();
      if (name.equals("page[cursor]")) {
        return pair.getValue();
      }
    }
  } catch (URISyntaxException e) {
    LOG.error(e.getMessage());
  }
  return null;
}
 
Example 7
Source Project: travelguide   File: APKExpansionPolicy.java    License: Apache License 2.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 8
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 9
Source Project: Klyph   File: APKExpansionPolicy.java    License: MIT 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 10
Source Project: Bats   File: RecoverableRpcProxy.java    License: Apache License 2.0 5 votes vote down vote up
private long connect(long timeMillis) throws IOException
{
  String uriStr = fsRecoveryHandler.readConnectUri();
  if (!uriStr.equals(lastConnectURI)) {
    LOG.debug("Got new RPC connect address {}", uriStr);
    lastConnectURI = uriStr;
    if (umbilical != null) {
      RPC.stopProxy(umbilical);
    }

    retryTimeoutMillis = Long.getLong(RETRY_TIMEOUT, RETRY_TIMEOUT_DEFAULT);
    retryDelayMillis = Long.getLong(RETRY_DELAY, RETRY_DELAY_DEFAULT);
    rpcTimeout = Integer.getInteger(RPC_TIMEOUT, RPC_TIMEOUT_DEFAULT);

    URI heartbeatUri = URI.create(uriStr);

    String queryStr = heartbeatUri.getQuery();
    if (queryStr != null) {
      List<NameValuePair> queryList = URLEncodedUtils.parse(queryStr, Charset.defaultCharset());
      if (queryList != null) {
        for (NameValuePair pair : queryList) {
          String value = pair.getValue();
          String key = pair.getName();
          if (QP_rpcTimeout.equals(key)) {
            this.rpcTimeout = Integer.parseInt(value);
          } else if (QP_retryTimeoutMillis.equals(key)) {
            this.retryTimeoutMillis = Long.parseLong(value);
          } else if (QP_retryDelayMillis.equals(key)) {
            this.retryDelayMillis = Long.parseLong(value);
          }
        }
      }
    }
    InetSocketAddress address = NetUtils.createSocketAddrForHost(heartbeatUri.getHost(), heartbeatUri.getPort());
    umbilical = RPC.getProxy(StreamingContainerUmbilicalProtocol.class, StreamingContainerUmbilicalProtocol.versionID, address, currentUser, conf, defaultSocketFactory, rpcTimeout);
    // reset timeout
    return System.currentTimeMillis() + retryTimeoutMillis;
  }
  return timeMillis;
}
 
Example 11
protected String parseCifsMountOptions(final URI uri) {
    final List<NameValuePair> args = URLEncodedUtils.parse(uri, "UTF-8");
    boolean foundUser = false;
    boolean foundPswd = false;
    final StringBuilder extraOpts = new StringBuilder();
    for (final NameValuePair nvp : args) {
        final String name = nvp.getName();
        if (name.equals("user")) {
            foundUser = true;
            s_logger.debug("foundUser is" + foundUser);
        } else if (name.equals("password")) {
            foundPswd = true;
            s_logger.debug("password is present in uri");
        }

        extraOpts.append(name + "=" + nvp.getValue() + ",");
    }

    if (s_logger.isDebugEnabled()) {
        s_logger.error("extraOpts now " + extraOpts);
    }

    if (!foundUser || !foundPswd) {
        final String errMsg =
                "Missing user and password from URI. Make sure they" + "are in the query string and separated by '&'.  E.g. "
                        + "cifs://example.com/some_share?user=foo&password=bar";
        s_logger.error(errMsg);
        throw new CloudRuntimeException(errMsg);
    }
    return extraOpts.toString();
}
 
Example 12
/**
 * 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 13
private long connect(long timeMillis) throws IOException
{
  String uriStr = fsRecoveryHandler.readConnectUri();
  if (!uriStr.equals(lastConnectURI)) {
    LOG.debug("Got new RPC connect address {}", uriStr);
    lastConnectURI = uriStr;
    if (umbilical != null) {
      RPC.stopProxy(umbilical);
    }

    retryTimeoutMillis = Long.getLong(RETRY_TIMEOUT, RETRY_TIMEOUT_DEFAULT);
    retryDelayMillis = Long.getLong(RETRY_DELAY, RETRY_DELAY_DEFAULT);
    rpcTimeout = Integer.getInteger(RPC_TIMEOUT, RPC_TIMEOUT_DEFAULT);

    URI heartbeatUri = URI.create(uriStr);

    String queryStr = heartbeatUri.getQuery();
    if (queryStr != null) {
      List<NameValuePair> queryList = URLEncodedUtils.parse(queryStr, Charset.defaultCharset());
      if (queryList != null) {
        for (NameValuePair pair : queryList) {
          String value = pair.getValue();
          String key = pair.getName();
          if (QP_rpcTimeout.equals(key)) {
            this.rpcTimeout = Integer.parseInt(value);
          } else if (QP_retryTimeoutMillis.equals(key)) {
            this.retryTimeoutMillis = Long.parseLong(value);
          } else if (QP_retryDelayMillis.equals(key)) {
            this.retryDelayMillis = Long.parseLong(value);
          }
        }
      }
    }
    InetSocketAddress address = NetUtils.createSocketAddrForHost(heartbeatUri.getHost(), heartbeatUri.getPort());
    umbilical = RPC.getProxy(StreamingContainerUmbilicalProtocol.class, StreamingContainerUmbilicalProtocol.versionID, address, currentUser, conf, defaultSocketFactory, rpcTimeout);
    // reset timeout
    return System.currentTimeMillis() + retryTimeoutMillis;
  }
  return timeMillis;
}
 
Example 14
public String parametersAsQueryString() {
    String s = "";
    for (Iterator<NameValuePair> i = this.params.iterator(); i.hasNext(); ) {
        NameValuePair nv = i.next();
        s += "&" + nv.getName() + "=" + nv.getValue();
    }
    if (s.length() > 0) {
        return "?" + s.substring(1);
    }
    return s;
}
 
Example 15
@Override
public Map<String, Object> createProperties() {
	Map<String, Object> properties = new HashMap<String, Object>();
	if (node != null)
	{
		int docid = this.docid;//node.getWebURL().getDocid();
		properties.put(NodeKeys.ID, docid);

		String url = this.url;//node.getWebURL().getURL();
		if (url != null)
			properties.put(NodeKeys.URL, url);

		String domain = this.domain;//node.get()
		if (domain != null)
			properties.put(NodeKeys.DOMAIN, domain);

		String path = this.path;//node.getWebURL().getPath();
		if (path != null)
			properties.put(NodeKeys.PATH, path);

		String parentUrl = this.parent;//node.getWebURL().getParentUrl();
		if (parentUrl != null)
			properties.put(NodeKeys.PARENT, parentUrl);

		if (data != null){
			int links = data.getOutgoingUrls().size();
			properties.put(NodeKeys.LINKS, links);

			int forms = data.getForms().size();
			properties.put(NodeKeys.FORMS, forms);
		}

		List<NameValuePair> p = this.params;//node.get()
		if (p != null){
			String str="";
			for (NameValuePair pair : p)
			{
				str+=pair.getName()+"="+pair.getValue()+",";
			}

			properties.put(NodeKeys.PARAMS, str);
		}

		if (CrawlConfig.getRole() != null)
			properties.put(NodeKeys.ROLE, CrawlConfig.getRole());


	}

	return properties;
}
 
Example 16
@Override
public void handle(HttpExchange exchange)
                                         throws IOException {
    try {
        if (!isAccessAllowed(exchange)) {
            sendErrResponse(exchange, 403,
                "Forbidden");
            return;
        }

        // 解析参数
        Set<Long> success = Collections.emptySet();
        long newStep = controller.getStep();
        int newSlotCount = controller.getSlotCount();

        try {
            for (NameValuePair nvp : parseParams(exchange)) {
                String name = nvp.getName();
                String value = nvp.getValue();
                if ("step".equalsIgnoreCase(name)) {
                    newStep = Long.parseLong(value);
                } else if ("slotCount"
                    .equalsIgnoreCase(name)) {
                    newSlotCount = Integer
                        .parseInt(value);
                } else if ("success"
                    .equalsIgnoreCase(name)) {
                    success = parseCursors(value);
                }
            }
        } catch (NumberFormatException nfe) {
            sendErrResponse(exchange, 400,
                nfe.getMessage());
            return;
        }

        Object data = controller
            .getNextData(success);

        JSONObject bodyEntity = new JSONObject();
        // 这里返回newStep给用户 表明我们已经接受了用户修改的step
        bodyEntity.put("step", newStep);
        bodyEntity.put("slotCount", newSlotCount);
        bodyEntity.put("data", data);
        sendResponse(exchange, bodyEntity);

        controller.update(newStep, newSlotCount);

        // if (oldRate != newStep || oldSlotCount != newSlotCount) {
        // }
    } catch (Throwable e) {
        logger.warn("pull metrics failed."
                    + e.getMessage());
    } finally {
        exchange.close();
    }
}
 
Example 17
Source Project: sndml3   File: Parameters.java    License: MIT License 4 votes vote down vote up
public Parameters(List<NameValuePair> list) {
	super();
	for (NameValuePair nvp : list) {
		super.put(nvp.getName(), nvp.getValue());
	}
}
 
Example 18
Source Project: sndml3   File: Parameters.java    License: MIT License 4 votes vote down vote up
public void add(NameValuePair nvp) {
	super.put(nvp.getName(), nvp.getValue());
}
 
Example 19
private String processCustomLayoutUrlFormat(URL url, String shortSessionId) {
	String finalUrl = url.getProtocol() + "://" + url.getAuthority();
	if (!url.getPath().isEmpty()) {
		finalUrl += url.getPath();
	}
	finalUrl = finalUrl.endsWith("/") ? finalUrl.substring(0, finalUrl.length() - 1) : finalUrl;
	if (url.getQuery() != null) {
		URI uri;
		try {
			uri = url.toURI();
			finalUrl += "?";
		} catch (URISyntaxException e) {
			String error = "\"customLayout\" property has URL format and query params (" + url.toString()
					+ "), but does not comply with RFC2396 URI format";
			log.error(error);
			throw new OpenViduException(Code.RECORDING_PATH_NOT_VALID, error);
		}
		List<NameValuePair> params = URLEncodedUtils.parse(uri, Charset.forName("UTF-8"));
		Iterator<NameValuePair> it = params.iterator();
		boolean hasSessionId = false;
		boolean hasSecret = false;
		while (it.hasNext()) {
			NameValuePair param = it.next();
			finalUrl += param.getName() + "=" + param.getValue();
			if (it.hasNext()) {
				finalUrl += "&";
			}
			if (!hasSessionId) {
				hasSessionId = param.getName().equals("sessionId");
			}
			if (!hasSecret) {
				hasSecret = param.getName().equals("secret");
			}
		}
		if (!hasSessionId) {
			finalUrl += "&sessionId=" + shortSessionId;
		}
		if (!hasSecret) {
			finalUrl += "&secret=" + openviduConfig.getOpenViduSecret();
		}
	}

	if (url.getRef() != null) {
		finalUrl += "#" + url.getRef();
	}

	return finalUrl;
}
 
Example 20
Source Project: r2m-plugin-android   File: UrlParser.java    License: Apache License 2.0 4 votes vote down vote up
public static ParsedUrl parseUrl(String url) {
    List<PathPart> pathParts = new ArrayList<PathPart>();
    List<Query> queries = new ArrayList<Query>();
    ParsedUrl parsedUrl;
    String base;

    try {
        URL aURL = new URL(url);
        base = aURL.getAuthority();
        String protocol = aURL.getProtocol();
        parsedUrl = new ParsedUrl();
        parsedUrl.setPathWithEndingSlash(aURL.getPath().endsWith("/"));
        parsedUrl.setBaseUrl(protocol + "://" + base);
        List<NameValuePair> pairs = URLEncodedUtils.parse(aURL.getQuery(),
                Charset.defaultCharset());
        for (NameValuePair pair : pairs) {
            Query query = new Query(pair.getName(), pair.getValue());
            queries.add(query);
        }
        parsedUrl.setQueries(queries);

        String[] pathStrings = aURL.getPath().split("/");
        for (String pathPart : pathStrings) {
          Matcher m = PATH_PARAM_PATTERN.matcher(pathPart);
          if (m.find()) {
            String paramDef = m.group(1);
            String[] paramParts = paramDef.split(":");
            if (paramParts.length > 1) {
              pathParts.add(new PathPart(paramParts[1].trim(), paramParts[0].trim()));
            } else {
              pathParts.add(new PathPart(paramParts[0].trim()));
            }
          } else {
            if(!pathPart.isEmpty()) {
              pathParts.add(new PathPart(pathPart));
            }
          }
        }
        parsedUrl.setPathParts(pathParts);
    } catch (Exception ex) {
        Logger.error(UrlParser.class, R2MMessages.getMessage("CANNOT_PARSE_URL", url));
        return null;
    }
    return parsedUrl;
}