List of usage examples for org.apache.commons.httpclient URI URI
public URI(URI base, URI relative) throws URIException
From source file:davmail.exchange.dav.DavExchangeSession.java
protected String getEscapedUrlFromPath(String escapedPath) throws URIException { URI uri = new URI(httpClient.getHostConfiguration().getHostURL(), true); uri.setEscapedPath(escapedPath);// w w w . j av a 2 s . co m return uri.getEscapedURI(); }
From source file:davmail.exchange.dav.DavExchangeSession.java
public String encodeAndFixUrl(String url) throws URIException { String originalUrl = URIUtil.encodePath(url); if (restoreHostName && originalUrl.startsWith("http")) { String targetPath = new URI(originalUrl, true).getEscapedPath(); originalUrl = getEscapedUrlFromPath(targetPath); }/*from ww w . jav a 2 s.c o m*/ return originalUrl; }
From source file:net.xmind.signin.internal.XMindNetRequest.java
protected void prepare() { setHeaders();//from ww w . ja v a 2 s . c o m String uri = this.uri; if (!params.isEmpty()) { if (method instanceof EntityEnclosingMethod) { RequestEntity entity = generateRequestEntity(); if (entity != null) { ((EntityEnclosingMethod) method).setRequestEntity(entity); } } else { uri = generateQueryString(uri, "?"); //$NON-NLS-1$ } } StringBuffer sb = new StringBuffer(); if (useHTTPS) { sb.append("https://"); //$NON-NLS-1$ } else { sb.append("http://"); //$NON-NLS-1$ } sb.append(domain); sb.append(uri); uri = sb.toString(); if (debugging) { Activator.log(String.format("Request: method=%s, uri='%s', headers=%s", //$NON-NLS-1$ method.getClass().getSimpleName(), uri, headers)); } try { method.setURI(new URI(uri, false)); } catch (Exception e) { //should not happen } }
From source file:no.trank.openpipe.wikipedia.download.HttpDownloader.java
public void init() { if (targetFile == null || !targetFile.getParentFile().isDirectory()) { throw new IllegalArgumentException("Invalid targetFile '" + targetFile + '\''); }/*from ww w . java2s . c o m*/ if (rssUrl == null) { throw new NullPointerException("sourceUrl cannot be null"); } try { new URI(rssUrl, true); } catch (URIException e) { throw new IllegalArgumentException("rssUrl '" + rssUrl + "' must be a valid URL: " + e.getMessage()); } if (httpClient == null) { httpClient = new HttpClient(); } }
From source file:no.trank.openpipe.wikipedia.producer.HttpDownloader.java
public void init() { if (targetFile == null || !targetFile.getParentFile().isDirectory()) { throw new IllegalArgumentException("Invalid targetFile '" + targetFile + '\''); }//from www . j ava2 s .c om if (sourceUrl == null) { throw new NullPointerException("sourceUrl cannot be null"); } try { new URI(sourceUrl, true); } catch (URIException e) { throw new IllegalArgumentException( "sourceUrl '" + sourceUrl + "' must be a valid URL: " + e.getMessage()); } }
From source file:org.alfresco.httpclient.AbstractHttpClient.java
/** * Send Request to the repository//from www . ja va2 s . co m */ protected HttpMethod sendRemoteRequest(Request req) throws AuthenticationException, IOException { if (logger.isDebugEnabled()) { logger.debug(""); logger.debug("* Request: " + req.getMethod() + " " + req.getFullUri() + (req.getBody() == null ? "" : "\n" + new String(req.getBody(), "UTF-8"))); } HttpMethod method = createMethod(req); // execute method executeMethod(method); // Deal with redirect if (isRedirect(method)) { Header locationHeader = method.getResponseHeader("location"); if (locationHeader != null) { String redirectLocation = locationHeader.getValue(); method.setURI(new URI(redirectLocation, true)); httpClient.executeMethod(method); } } return method; }
From source file:org.alfresco.repo.search.impl.solr.SolrAdminHTTPClient.java
public JSONObject execute(String relativeHandlerPath, HashMap<String, String> args) { ParameterCheck.mandatory("relativeHandlerPath", relativeHandlerPath); ParameterCheck.mandatory("args", args); String path = getPath(relativeHandlerPath); try {//from w ww . java2 s .c o m URLCodec encoder = new URLCodec(); StringBuilder url = new StringBuilder(); for (String key : args.keySet()) { String value = args.get(key); if (url.length() == 0) { url.append(path); url.append("?"); url.append(encoder.encode(key, "UTF-8")); url.append("="); url.append(encoder.encode(value, "UTF-8")); } else { url.append("&"); url.append(encoder.encode(key, "UTF-8")); url.append("="); url.append(encoder.encode(value, "UTF-8")); } } // PostMethod post = new PostMethod(url.toString()); 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)); return json; } 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.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java
protected JSONObject postQuery(HttpClient httpClient, String url, JSONObject body) throws UnsupportedEncodingException, IOException, HttpException, URIException, JSONException { PostMethod post = new PostMethod(url); if (body.toString().length() > DEFAULT_SAVEPOST_BUFFER) { post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); }/* ww w .j a v a 2s .c o m*/ post.setRequestEntity(new ByteArrayRequestEntity(body.toString().getBytes("UTF-8"), "application/json")); try { httpClient.executeMethod(post); if (post.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || post.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) { Header locationHeader = post.getResponseHeader("location"); if (locationHeader != null) { String redirectLocation = locationHeader.getValue(); post.setURI(new URI(redirectLocation, true)); httpClient.executeMethod(post); } } if (post.getStatusCode() != HttpServletResponse.SC_OK) { throw new LuceneQueryParserException( "Request failed " + post.getStatusCode() + " " + url.toString()); } Reader reader = new BufferedReader( new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet())); // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler JSONObject json = new JSONObject(new JSONTokener(reader)); if (json.has("status")) { JSONObject status = json.getJSONObject("status"); if (status.getInt("code") != HttpServletResponse.SC_OK) { throw new LuceneQueryParserException("SOLR side error: " + status.getString("message")); } } return json; } finally { post.releaseConnection(); } }
From source file:org.alfresco.repo.search.impl.solr.SolrQueryHTTPClient.java
/** * @param storeRef//from w w w. j a v a2 s .c om * @param handler * @param params * @return */ public JSONObject execute(StoreRef storeRef, String handler, HashMap<String, String> params) { try { SolrStoreMappingWrapper mapping = extractMapping(storeRef); URLCodec encoder = new URLCodec(); StringBuilder url = new StringBuilder(); Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl(); HttpClient httpClient = httpClientAndBaseUrl.getFirst(); for (String key : params.keySet()) { String value = params.get(key); if (url.length() == 0) { url.append(httpClientAndBaseUrl.getSecond()); if (!handler.startsWith("/")) { url.append("/"); } url.append(handler); url.append("?"); url.append(encoder.encode(key, "UTF-8")); url.append("="); url.append(encoder.encode(value, "UTF-8")); } else { url.append("&"); url.append(encoder.encode(key, "UTF-8")); url.append("="); url.append(encoder.encode(value, "UTF-8")); } } if (mapping.isSharded()) { url.append("&shards="); url.append(mapping.getShards()); } // PostMethod post = new PostMethod(url.toString()); 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)); return json; } 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.alfresco.repo.solr.EmbeddedSolrTest.java
public JSONObject execute(HashMap<String, String> args) { try {// w ww .j a v a2s. c o m URLCodec encoder = new URLCodec(); StringBuilder url = new StringBuilder(); for (String key : args.keySet()) { String value = args.get(key); if (url.length() == 0) { url.append(baseUrl); url.append("?"); url.append(encoder.encode(key, "UTF-8")); url.append("="); url.append(encoder.encode(value, "UTF-8")); } else { url.append("&"); url.append(encoder.encode(key, "UTF-8")); url.append("="); url.append(encoder.encode(value, "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)); return json; } 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); } }