List of usage examples for org.apache.commons.httpclient URI URI
public URI(URI base, URI relative) throws URIException
From source file:org.structr.web.Importer.java
private void copyURLToFile(final String uri, final java.io.File fileOnDisk) throws IOException { final HttpClient client = getHttpClient(); final HttpMethod get = new GetMethod(); get.setURI(new URI(uri, false)); get.addRequestHeader("User-Agent", "curl/7.35.0"); logger.log(Level.INFO, "Downloading from {0}", uri); final int statusCode = client.executeMethod(get); if (statusCode == 200) { try (final InputStream is = get.getResponseBodyAsStream()) { try (final OutputStream os = new FileOutputStream(fileOnDisk)) { IOUtils.copy(is, os);//from ww w.j av a2s. com } } } else { System.out.println("response body: " + new String(get.getResponseBody(), "utf-8")); logger.log(Level.WARNING, "Unable to create file {0}: status code was {1}", new Object[] { uri, statusCode }); } }
From source file:org.tsaap.lti.tp.ResourceLink.java
/** * Send a service request to the tool consumer. * * @param type message type/*from w w w .ja v a2 s . c o m*/ * @param url URL to send request to * @param params map of parameter values to be passed * @return <code>true</code> if the request successfully obtained a response */ private boolean doService(String type, String url, Map<String, String> params) { this.extResponse = null; if ((url != null) && (url.length() > 0)) { // Add standard parameters params.put("oauth_consumer_key", this.consumer.getKey()); params.put("lti_version", ToolProvider.LTI_VERSION); params.put("lti_message_type", type); HashSet<Map.Entry<String, String>> httpParams = new HashSet<Map.Entry<String, String>>(); httpParams.addAll(params.entrySet()); // Check for query parameters which need to be included in the signature Map<String, String> queryParams = new HashMap<String, String>(); String urlNoQuery = url; try { URI uri = new URI(url, false); String query = uri.getQuery(); if (query != null) { urlNoQuery = urlNoQuery.substring(0, urlNoQuery.length() - query.length() - 1); String[] queryItems = query.split("&"); for (int i = 0; i < queryItems.length; i++) { String[] queryItem = queryItems[i].split("=", 2); if (queryItem.length > 1) { queryParams.put(queryItem[0], queryItem[1]); } else { queryParams.put(queryItem[0], ""); } } httpParams.addAll(queryParams.entrySet()); } } catch (URIException e) { } // Add OAuth signature List<Map.Entry<String, String>> reqParams = new ArrayList<Map.Entry<String, String>>(); OAuthMessage oAuthMessage = new OAuthMessage("POST", urlNoQuery, httpParams); OAuthConsumer oAuthConsumer = new OAuthConsumer("about:blank", this.consumer.getKey(), this.consumer.getSecret(), null); OAuthAccessor oAuthAccessor = new OAuthAccessor(oAuthConsumer); try { oAuthMessage.addRequiredParameters(oAuthAccessor); reqParams.addAll(oAuthMessage.getParameters()); } catch (OAuthException e) { } catch (URISyntaxException e) { } catch (IOException e) { } // Remove parameters being passed on the query string for (Iterator<Map.Entry<String, String>> iter = queryParams.entrySet().iterator(); iter.hasNext();) { Map.Entry<String, String> entry = iter.next(); reqParams.remove(entry); } // Connect to tool consumer this.extResponse = this.doPostRequest(url, Utils.getHTTPParams(reqParams), null, null); // Parse XML response if (this.extResponse != null) { this.extDoc = Utils.getXMLDoc(this.extResponse); boolean ok = this.extDoc != null; if (ok) { Element el = Utils.getXmlChild(this.extDoc.getRootElement(), "statusinfo"); ok = el != null; if (ok) { String responseCode = Utils.getXmlChildValue(el, "codemajor"); ok = responseCode != null; if (ok) { ok = responseCode.equals("Success"); } } } if (!ok) { this.extResponse = null; } } } return this.extResponse != null; }
From source file:org.tsaap.lti.tp.ResourceLink.java
/** * Send a service request to the tool consumer. * * @param type Message type//from w w w . j a v a2 s. c o m * @param url URL to send request to * @param xml XML of message request * @return <code>true</code> if the request successfully obtained a response */ private boolean doLTI11Service(String type, String url, String xml) { this.extResponse = null; if ((url != null) && (url.length() > 0)) { String messageId = UUID.randomUUID().toString(); StringBuilder xmlRequest = new StringBuilder(); xmlRequest.append("<?xml version = \"1.0\" encoding = \"UTF-8\"?>\n"); xmlRequest.append( "<imsx_POXEnvelopeRequest xmlns = \"http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0\">\n"); xmlRequest.append(" <imsx_POXHeader>\n"); xmlRequest.append(" <imsx_POXRequestHeaderInfo>\n"); xmlRequest.append(" <imsx_version>V1.0</imsx_version>\n"); xmlRequest.append(" <imsx_messageIdentifier>").append(messageId) .append("</imsx_messageIdentifier>\n"); xmlRequest.append(" </imsx_POXRequestHeaderInfo>\n"); xmlRequest.append(" </imsx_POXHeader>\n"); xmlRequest.append(" <imsx_POXBody>\n"); xmlRequest.append(" <").append(type).append("Request>\n"); xmlRequest.append(xml); xmlRequest.append(" </").append(type).append("Request>\n"); xmlRequest.append(" </imsx_POXBody>\n"); xmlRequest.append("</imsx_POXEnvelopeRequest>\n"); // Calculate body hash String hash = Base64.encodeBase64String(DigestUtils.sha1(xmlRequest.toString())); Map<String, String> params = new HashMap<String, String>(); params.put("oauth_body_hash", hash); HashSet<Map.Entry<String, String>> httpParams = new HashSet<Map.Entry<String, String>>(); httpParams.addAll(params.entrySet()); // Check for query parameters which need to be included in the signature Map<String, String> queryParams = new HashMap<String, String>(); String urlNoQuery = url; try { URI uri = new URI(url, false); String query = uri.getQuery(); if (query != null) { urlNoQuery = urlNoQuery.substring(0, urlNoQuery.length() - query.length() - 1); String[] queryItems = query.split("&"); for (int i = 0; i < queryItems.length; i++) { String[] queryItem = queryItems[i].split("=", 2); if (queryItem.length > 1) { queryParams.put(queryItem[0], queryItem[1]); } else { queryParams.put(queryItem[0], ""); } } httpParams.addAll(queryParams.entrySet()); } } catch (URIException e) { } // Add OAuth signature Map<String, String> header = new HashMap<String, String>(); OAuthMessage oAuthMessage = new OAuthMessage("POST", urlNoQuery, httpParams); OAuthConsumer oAuthConsumer = new OAuthConsumer("about:blank", this.consumer.getKey(), this.consumer.getSecret(), null); OAuthAccessor oAuthAccessor = new OAuthAccessor(oAuthConsumer); try { oAuthMessage.addRequiredParameters(oAuthAccessor); header.put("Authorization", oAuthMessage.getAuthorizationHeader(null)); header.put("Content-Type", "application/xml"); } catch (OAuthException e) { } catch (URISyntaxException e) { } catch (IOException e) { } StringRequestEntity entity = new StringRequestEntity(xmlRequest.toString()); // Connect to tool consumer this.extResponse = this.doPostRequest(url, Utils.getHTTPParams(params), header, entity); // Parse XML response if (this.extResponse != null) { this.extDoc = Utils.getXMLDoc(this.extResponse); boolean ok = this.extDoc != null; if (ok) { Element el = Utils.getXmlChild(this.extDoc.getRootElement(), "imsx_statusInfo"); ok = el != null; if (ok) { String responseCode = Utils.getXmlChildValue(el, "imsx_codeMajor"); ok = responseCode != null; if (ok) { ok = responseCode.equals("success"); } } } if (!ok) { this.extResponse = null; } } } return (this.extResponse != null); }
From source file:org.tsaap.lti.tp.ResourceLink.java
/** * Performs an HTTP POST request./*from ww w. j a v a 2s .c o m*/ * * @param url URL to send request to * @param params map of parameter values to be passed * @param header values to include in the request header * @return response returned from request, null if an error occurred */ private String doPostRequest(String url, NameValuePair[] params, Map<String, String> header, StringRequestEntity entity) { String fileContent = null; HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT); PostMethod httpPost = new PostMethod(url); httpPost.setFollowRedirects(false); httpPost.addParameters(params); try { if (entity != null) { httpPost.setRequestEntity(entity); } if (header != null) { String name; for (Iterator<String> iter = header.keySet().iterator(); iter.hasNext();) { name = iter.next(); httpPost.addRequestHeader(name, header.get(name)); } } httpPost.setURI(new URI(url, false)); int resp = client.executeMethod(httpPost); if (resp < 400) { fileContent = httpPost.getResponseBodyAsString(); } } catch (IOException e) { fileContent = null; } httpPost.releaseConnection(); return fileContent; }
From source file:org.waveprotocol.box.server.waveserver.SolrSearchProviderImpl.java
private JsonArray sendSearchRequest(String solrQuery, Function<InputStreamReader, JsonArray> function) throws IOException { JsonArray docsJson;/*from ww w.ja va 2 s.c om*/ GetMethod getMethod = new GetMethod(); HttpClient httpClient = new HttpClient(); try { getMethod.setURI(new URI(solrQuery, false)); int statusCode = httpClient.executeMethod(getMethod); docsJson = function.apply(new InputStreamReader(getMethod.getResponseBodyAsStream())); if (statusCode != HttpStatus.SC_OK) { LOG.warning("Failed to execute query: " + solrQuery); throw new IOException("Search request status is not OK: " + statusCode); } } finally { getMethod.releaseConnection(); } return docsJson; }
From source file:org.waveprotocol.box.server.waveserver.SolrWaveIndexerImpl.java
private void sendRequestToDeleteSolrIndex() { GetMethod getMethod = new GetMethod(); try {//w ww.ja v a2s.c o m getMethod.setURI(new URI(solrBaseUrl + "/update?wt=json" + "&stream.body=<delete><query>" + SolrSearchProviderImpl.Q + "</query></delete>", false)); HttpClient httpClient = new HttpClient(); int statusCode = httpClient.executeMethod(getMethod); if (statusCode == HttpStatus.SC_OK) { getMethod.setURI(new URI(solrBaseUrl + "/update?wt=json" + "&stream.body=<commit/>", false)); httpClient = new HttpClient(); statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOG.warning("failed to clean solr index"); } } else { LOG.warning("failed to clean solr index"); } } catch (Exception e) { LOG.warning("failed to clean solr index", e); } finally { getMethod.releaseConnection(); } }
From source file:org.zaizi.alfresco.redlink.service.search.solr.SensefySolrQueryHTTPClient.java
public ResultSet executeQuery(SearchParameters searchParameters)// , String language) { if (repositoryState.isBootstrapping()) { throw new AlfrescoRuntimeException( "SOLR queries can not be executed while the repository is bootstrapping"); }//from ww w . j av a2 s . c o m try { if (searchParameters.getStores().size() == 0) { throw new AlfrescoRuntimeException("No store for query"); } URLCodec encoder = new URLCodec(); StringBuilder url = new StringBuilder(); url.append(sensefySearchEndpoint); // Send the query url.append("?query="); url.append(encoder.encode(searchParameters.getQuery(), "UTF-8")); // url.append("?wt=").append(encoder.encode("json", "UTF-8")); url.append("&fields=").append(encoder.encode("DBID,score", "UTF-8")); // Emulate old limiting behaviour and metadata final LimitBy limitBy; int maxResults = -1; if (searchParameters.getMaxItems() >= 0) { maxResults = searchParameters.getMaxItems(); limitBy = LimitBy.FINAL_SIZE; } else if (searchParameters.getLimitBy() == LimitBy.FINAL_SIZE && searchParameters.getLimit() >= 0) { maxResults = searchParameters.getLimit(); limitBy = LimitBy.FINAL_SIZE; } else { maxResults = searchParameters.getMaxPermissionChecks(); if (maxResults < 0) { maxResults = maximumResultsFromUnlimitedQuery; } limitBy = LimitBy.NUMBER_OF_PERMISSION_EVALUATIONS; } url.append("&rows=").append(String.valueOf(maxResults)); // url.append("&df=").append(encoder.encode(searchParameters.getDefaultFieldName(), "UTF-8")); url.append("&start=").append(encoder.encode("" + searchParameters.getSkipCount(), "UTF-8")); // Locale locale = I18NUtil.getLocale(); // if (searchParameters.getLocales().size() > 0) // { // locale = searchParameters.getLocales().get(0); // } // url.append("&locale="); // url.append(encoder.encode(locale.toString(), "UTF-8")); StringBuffer sortBuffer = new StringBuffer(); for (SortDefinition sortDefinition : searchParameters.getSortDefinitions()) { if (sortBuffer.length() == 0) { sortBuffer.append("&sort="); } else { sortBuffer.append(encoder.encode(", ", "UTF-8")); } sortBuffer.append(encoder.encode(sortDefinition.getField(), "UTF-8")) .append(encoder.encode(" ", "UTF-8")); if (sortDefinition.isAscending()) { sortBuffer.append(encoder.encode("asc", "UTF-8")); } else { sortBuffer.append(encoder.encode("desc", "UTF-8")); } } url.append(sortBuffer); if (searchParameters.getFieldFacets().size() > 0) { for (FieldFacet facet : searchParameters.getFieldFacets()) { url.append("&facet=").append(encoder.encode(facet.getField(), "UTF-8")); } } // end of field factes // add current username doing the request for permissions url.append("&userName=").append(encoder.encode(AuthenticationUtil.getRunAsUser(), "UTF-8")); GetMethod get = new GetMethod(url.toString()); try { httpClient.executeMethod(get); if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { Header locationHeader = get.getResponseHeader("location"); if (locationHeader != null) { String redirectLocation = locationHeader.getValue(); get.setURI(new URI(redirectLocation, true)); httpClient.executeMethod(get); } } if (get.getStatusCode() != HttpServletResponse.SC_OK) { throw new LuceneQueryParserException( "Request failed " + get.getStatusCode() + " " + url.toString()); } Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream())); // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler JSONObject json = new JSONObject(new JSONTokener(reader)); SensefySolrJSONResultSet results = new SensefySolrJSONResultSet(json, searchParameters, nodeService, nodeDAO, limitBy, maxResults); if (s_logger.isDebugEnabled()) { s_logger.debug("Sent :" + url); s_logger.debug("Got: " + results.getNumberFound() + " in " + results.getQueryTime() + " ms"); } return results; } finally { get.releaseConnection(); } } catch (UnsupportedEncodingException e) { throw new LuceneQueryParserException("", e); } catch (HttpException e) { throw new LuceneQueryParserException("", e); } catch (IOException e) { throw new LuceneQueryParserException("", e); } catch (JSONException e) { throw new LuceneQueryParserException("", e); } }
From source file:org.zaproxy.clientapi.core.WavsepStatic.java
public static void genericTest(String relativeUrl, Alert[] ignoreAlerts, Alert[] requireAlerts) throws Exception { String url = wavsepHost + relativeUrl; ClientApi client = initClientApi();// w w w . j a va 2 s . c o m client.accessUrl(url); Thread.sleep(sleepInMs); String nodeName = url; if (nodeName.indexOf("?") > 0) { String query = new URI(nodeName, true).getQuery(); if (query == null) { query = ""; } nodeName = nodeName.substring(0, nodeName.indexOf("?")) + getQueryParamString(getParamNameSet(query)); } // Dont actually seem to need to spider the URLs... //client.spiderUrl(nodeName); //Thread.sleep(sleepInMs); client.ascan.scan("", nodeName, "true", "false"); Thread.sleep(sleepInMs); List<Alert> ignoreAlertsList = new ArrayList<>(ignoreAlerts.length); ignoreAlertsList.addAll(Arrays.asList(setAlertsUrl(ignoreAlerts, url))); List<Alert> requireAlertsList = new ArrayList<>(requireAlerts.length); requireAlertsList.addAll(Arrays.asList(setAlertsUrl(requireAlerts, url))); client.checkAlerts(ignoreAlertsList, requireAlertsList); }
From source file:org.zaproxy.zap.extension.accessControl.ContextAccessRulesManager.java
/** * Import a rule from a serialized representation. The rule should have been exported via the * {@link #exportSerializedRules()} method. * * @param serializedRule the serialized rule */// w w w. j a v a 2 s . c om protected void importSerializedRule(String serializedRule) { try { String[] values = serializedRule.split(Character.toString(SERIALIZATION_SEPARATOR), 4); int userId = Integer.parseInt(values[0]); AccessRule rule = AccessRule.valueOf(values[1]); String nodeName = new String(Base64.decodeBase64(values[2])); URI uri = new URI(values[3], true); SiteTreeNode node = new SiteTreeNode(nodeName, uri); getUserRules(userId).put(node, rule); if (log.isDebugEnabled()) { log.debug(String.format( "Imported access control rule (context, userId, node, rule): (%d, %d, %s, %s) ", context.getIndex(), userId, uri.toString(), rule)); } } catch (Exception ex) { log.error("Unable to import serialized rule for context " + context.getIndex() + ":" + serializedRule, ex); } }
From source file:org.zaproxy.zap.extension.ascan.ActiveScanAPI.java
private static URI getTargetUrl(String url) throws ApiException { try {/*from w w w . ja v a 2 s . c o m*/ URI targetUrl = new URI(url, false); String scheme = targetUrl.getScheme(); if (scheme == null || (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https"))) { throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_URL); } return targetUrl; } catch (URIException e) { throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_URL); } }