List of usage examples for org.apache.commons.httpclient.params HttpClientParams HttpClientParams
public HttpClientParams(HttpParams paramHttpParams)
From source file:org.codelabor.system.remoting.http.services.HttpAdapterServiceImpl.java
public String requestByGetMethod(Map<String, String> parameterMap) throws Exception { String responseBody = null;/*from w ww . ja v a 2s . c om*/ GetMethod method = null; try { StringBuilder sb = new StringBuilder(); sb.append(url); if (url.indexOf("?") == -1) { sb.append("?"); } Set<String> keySet = parameterMap.keySet(); Iterator<String> iter = keySet.iterator(); String parameterKey = null; while (iter.hasNext()) { parameterKey = iter.next(); sb.append(parameterKey); sb.append("="); sb.append(parameterMap.get(parameterKey)); if (iter.hasNext()) { sb.append("&"); } } String encodedURI = URIUtil.encodeQuery(sb.toString()); logger.debug("encodedURI: {}", encodedURI); method = new GetMethod(encodedURI); HttpParams httpParams = new DefaultHttpParams(); DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(retry, false); httpParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); HttpClient httpClient = new HttpClient(new HttpClientParams(httpParams)); int statusCode = httpClient.executeMethod(method); logger.debug("statusCode: {}", statusCode); switch (statusCode) { case HttpStatus.SC_OK: responseBody = method.getResponseBodyAsString(); logger.debug("responseBody: {}", responseBody); break; } } catch (Exception e) { if (logger.isErrorEnabled()) { String messageKey = "error.http.request"; String userMessage = messageSource.getMessage(messageKey, new String[] {}, "default message", Locale.getDefault()); logger.error(userMessage, e); } e.printStackTrace(); throw e; } finally { if (method != null) { method.releaseConnection(); } } return responseBody; }
From source file:org.codelabor.system.services.HttpAdapterServiceImpl.java
public String request(Map<String, String> parameterMap) { StringBuilder stringBuilder = new StringBuilder(); String responseBody = null;/*from www.j av a2 s. c o m*/ GetMethod method = null; try { stringBuilder.append(url); if (url.indexOf("?") == -1) { stringBuilder.append("?"); } Set<String> keySet = parameterMap.keySet(); Iterator<String> iter = keySet.iterator(); String parameterKey = null; while (iter.hasNext()) { parameterKey = iter.next(); stringBuilder.append(parameterKey); stringBuilder.append("="); stringBuilder.append(parameterMap.get(parameterKey)); if (iter.hasNext()) { stringBuilder.append("&"); } } String encodedURI = URIUtil.encodeQuery(stringBuilder.toString()); if (log.isDebugEnabled()) { log.debug(encodedURI); } method = new GetMethod(encodedURI); HttpParams httpParams = new DefaultHttpParams(); DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(retry, false); httpParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); HttpClient httpClient = new HttpClient(new HttpClientParams(httpParams)); int statusCode = httpClient.executeMethod(method); if (log.isDebugEnabled()) { stringBuilder = new StringBuilder(); stringBuilder.append("statusCode: ").append(statusCode); log.debug(stringBuilder.toString()); } switch (statusCode) { case HttpStatus.SC_OK: responseBody = method.getResponseBodyAsString(); break; } } catch (Exception e) { if (log.isErrorEnabled()) { String messageKey = "error.http.request"; String userMessage = messageSource.getMessage(messageKey, new String[] {}, "default message", Locale.getDefault()); log.error(userMessage, e); } } finally { if (method != null) { method.releaseConnection(); } } return responseBody; }
From source file:org.infoglue.igide.helper.HTTPClientHelper.java
public HTTPClientHelper(URL baseurl, String username, String password) { HttpClientParams params = new HttpClientParams(HttpClientParams.getDefaultParams()); client = new HttpClient(params); Credentials defaultcreds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(new AuthScope(baseurl.getHost(), baseurl.getPort(), AuthScope.ANY_REALM), defaultcreds);//w w w . j av a 2s.com List<String> authPrefs = new ArrayList<String>(2); authPrefs.add(AuthPolicy.BASIC); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.NTLM); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); }
From source file:org.structr.function.PostFunction.java
@Override public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) throws FrameworkException { if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) { final String uri = sources[0].toString(); final String body = sources[1].toString(); String contentType = "application/json"; String charset = "utf-8"; // override default content type if (sources.length >= 3 && sources[2] != null) { contentType = sources[2].toString(); }/*from www . j a v a 2 s . c o m*/ // override default content type if (sources.length >= 4 && sources[3] != null) { charset = sources[3].toString(); } final HttpClientParams params = new HttpClientParams(HttpClientParams.getDefaultParams()); final HttpClient client = new HttpClient(params); final PostMethod postMethod = new PostMethod(uri); // add request headers from context for (final Map.Entry<String, String> header : ctx.getHeaders().entrySet()) { postMethod.addRequestHeader(header.getKey(), header.getValue()); } try { postMethod.setRequestEntity(new StringRequestEntity(body, contentType, charset)); final int statusCode = client.executeMethod(postMethod); final String responseBody = postMethod.getResponseBodyAsString(); final GraphObjectMap response = new GraphObjectMap(); if ("application/json".equals(contentType)) { final FromJsonFunction fromJsonFunction = new FromJsonFunction(); response.setProperty(new StringProperty("body"), fromJsonFunction.apply(ctx, entity, new Object[] { responseBody })); } else { response.setProperty(new StringProperty("body"), responseBody); } response.setProperty(new IntProperty("status"), statusCode); response.setProperty(new StringProperty("headers"), extractHeaders(postMethod.getResponseHeaders())); return response; } catch (IOException ioex) { ioex.printStackTrace(); } } else { return usage(ctx.isJavaScriptContext()); } return null; }
From source file:org.structr.function.UiFunction.java
protected String getFromUrl(final ActionContext ctx, final String requestUrl, final String username, final String password) throws IOException { final HttpClientParams params = new HttpClientParams(HttpClientParams.getDefaultParams()); final HttpClient client = new HttpClient(params); final GetMethod getMethod = new GetMethod(requestUrl); if (username != null && password != null) { Credentials defaultcreds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, defaultcreds); client.getParams().setAuthenticationPreemptive(true); getMethod.setDoAuthentication(true); }/* w w w . ja v a 2 s. co m*/ getMethod.addRequestHeader("Connection", "close"); // add request headers from context for (final Map.Entry<String, String> header : ctx.getHeaders().entrySet()) { getMethod.addRequestHeader(header.getKey(), header.getValue()); } client.executeMethod(getMethod); return getMethod.getResponseBodyAsString(); }
From source file:org.structr.function.UiFunction.java
protected GraphObjectMap headFromUrl(final ActionContext ctx, final String requestUrl, final String username, final String password) throws IOException, FrameworkException { final HttpClientParams params = new HttpClientParams(HttpClientParams.getDefaultParams()); final HttpClient client = new HttpClient(params); final HeadMethod headMethod = new HeadMethod(requestUrl); if (username != null && password != null) { Credentials defaultcreds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, defaultcreds); client.getParams().setAuthenticationPreemptive(true); headMethod.setDoAuthentication(true); }//from w w w . j a v a 2 s. c o m headMethod.addRequestHeader("Connection", "close"); // Don't follow redirects automatically, return status code 302 etc. instead headMethod.setFollowRedirects(false); // add request headers from context for (final Map.Entry<String, String> header : ctx.getHeaders().entrySet()) { headMethod.addRequestHeader(header.getKey(), header.getValue()); } client.executeMethod(headMethod); final GraphObjectMap response = new GraphObjectMap(); response.setProperty(new IntProperty("status"), headMethod.getStatusCode()); response.setProperty(new StringProperty("headers"), extractHeaders(headMethod.getResponseHeaders())); return response; }
From source file:org.structr.web.function.HttpPostFunction.java
@Override public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) throws FrameworkException { if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) { final String uri = sources[0].toString(); final String body = sources[1].toString(); String contentType = "application/json"; String charset = "utf-8"; // override default content type if (sources.length >= 3 && sources[2] != null) { contentType = sources[2].toString(); }/*from www. ja v a 2s . co m*/ // override default content type if (sources.length >= 4 && sources[3] != null) { charset = sources[3].toString(); } final HttpClientParams params = new HttpClientParams(HttpClientParams.getDefaultParams()); final HttpClient client = new HttpClient(params); final PostMethod postMethod = new PostMethod(uri); // add request headers from context for (final Map.Entry<String, String> header : ctx.getHeaders().entrySet()) { postMethod.addRequestHeader(header.getKey(), header.getValue()); } try { postMethod.setRequestEntity(new StringRequestEntity(body, contentType, charset)); final int statusCode = client.executeMethod(postMethod); final String responseBody = postMethod.getResponseBodyAsString(); final GraphObjectMap response = new GraphObjectMap(); if ("application/json".equals(contentType)) { final FromJsonFunction fromJsonFunction = new FromJsonFunction(); response.setProperty(new StringProperty("body"), fromJsonFunction.apply(ctx, entity, new Object[] { responseBody })); } else { response.setProperty(new StringProperty("body"), responseBody); } response.setProperty(new IntProperty("status"), statusCode); response.setProperty(new StringProperty("headers"), extractHeaders(postMethod.getResponseHeaders())); return response; } catch (IOException ioex) { logException(entity, ioex, sources); } } else { logParameterError(entity, sources, ctx.isJavaScriptContext()); return usage(ctx.isJavaScriptContext()); } return null; }
From source file:org.structr.web.function.UiFunction.java
protected String getFromUrl(final ActionContext ctx, final String requestUrl, final String username, final String password) throws IOException { final HttpClientParams params = new HttpClientParams(HttpClientParams.getDefaultParams()); final HttpClient client = new HttpClient(params); final GetMethod getMethod = new GetMethod(requestUrl); if (username != null && password != null) { Credentials defaultcreds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, defaultcreds); client.getParams().setAuthenticationPreemptive(true); getMethod.setDoAuthentication(true); }/*from w ww . ja v a2 s. co m*/ getMethod.addRequestHeader("Connection", "close"); // add request headers from context for (final Map.Entry<String, String> header : ctx.getHeaders().entrySet()) { getMethod.addRequestHeader(header.getKey(), header.getValue()); } client.executeMethod(getMethod); return IOUtils.toString(getMethod.getResponseBodyAsStream()); }