List of usage examples for org.apache.commons.httpclient HttpClient getState
public HttpState getState()
From source file:org.iavante.sling.gad.administration.impl.AbstractHttpOperation.java
/** * Verify that given URL returns expectedStatusCode * /* w w w . j av a2s . c om*/ * @throws IOException */ protected void assertAuthenticatedHttpStatus(Credentials creds, String urlString, int expectedStatusCode, String assertMessage) throws IOException { URL baseUrl = new URL(HTTP_BASE_URL); List authPrefs = new ArrayList(2); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); HttpClient httpClient = new HttpClient(); httpClient.getParams().setAuthenticationPreemptive(true); httpClient.getState().setCredentials(AuthScope.ANY, creds); httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); GetMethod getMethod = new GetMethod(urlString); getMethod.setDoAuthentication(true); try { final int status = httpClient.executeMethod(getMethod); if (assertMessage == null) { assertEquals(urlString, expectedStatusCode, status); } else { assertEquals(assertMessage, expectedStatusCode, status); } } finally { getMethod.releaseConnection(); } }
From source file:org.iavante.sling.gad.transcoder.impl.TranscoderServiceImpl.java
public int sendConversionTask3(String conversionType, String sourceLocation, String targetLocation, String notificationUrl, String externalStorageServer, String externalStorageUrl, String params, String ds_custom_props) { Map<String, String> envs = System.getenv(); Set<String> keys = envs.keySet(); Iterator<String> it = keys.iterator(); List authPrefs = new ArrayList(2); Credentials defaultcreds;//from w w w . j a v a2s.c o m authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); defaultcreds = new UsernamePasswordCredentials(transcodingServerUser, transcodingServerPassword); // Set client connection params MultiThreadedHttpConnectionManager connManager = new MultiThreadedHttpConnectionManager(); HttpConnectionManagerParams connParams = connManager.getParams(); connParams.setConnectionTimeout(800); HttpClient client = new HttpClient(connManager); client.getState().setCredentials(AuthScope.ANY, defaultcreds); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); if (log.isInfoEnabled()) log.info("Sending conversion task"); Node convTypeNode = null; try { convTypeNode = rootNode.getNode(baseRepoDir + "/" + COMPONENTS_FOLDER + "/" + TRANSCODER_FOLDER) .getNode(conversionType); } catch (PathNotFoundException e) { e.printStackTrace(); } catch (RepositoryException e) { e.printStackTrace(); } String executable = null; try { executable = convTypeNode.getProperty("command").getValue().getString(); if (params == null) { params = convTypeNode.getProperty("params").getValue().getString(); } if (ds_custom_props == null) { ds_custom_props = ""; } } catch (ValueFormatException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (PathNotFoundException e) { e.printStackTrace(); } catch (RepositoryException e) { e.printStackTrace(); } PostMethod post = new PostMethod(sendConversionUrl); //post.getParams().setParameter("http.socket.timeout", new Integer(50)); post.setDoAuthentication(true); NameValuePair[] data = { new NameValuePair("source_location", sourceLocation), new NameValuePair("target_location", targetLocation), new NameValuePair("executable", executable), new NameValuePair("params", params), new NameValuePair("ds_custom_props", ds_custom_props), new NameValuePair("notification_url", notificationUrl), new NameValuePair("extern_storage_server", externalStorageServer), new NameValuePair("sling:resourceType", "gad/job"), new NameValuePair("extern_storage_url", externalStorageUrl) }; post.setRequestBody(data); post.setDoAuthentication(true); int status = 0; int intentos = 0; while ((status != 201) && (intentos < 2)) { try { client.executeMethod(post); status = post.getStatusCode(); log.info("Conversion sent. Status code: " + status); } catch (HttpException e1) { log.error("Excepcion: HttpException"); e1.printStackTrace(); post.releaseConnection(); return status; } catch (IOException e1) { log.error("Excepcion: IOexception"); e1.printStackTrace(); post.releaseConnection(); return status; } finally { intentos++; } } post.releaseConnection(); return status; }
From source file:org.iavante.sling.transcodingServer.impl.TranscodingManagerImpl.java
@SuppressWarnings("unchecked") private String notificateTransformation(Node jobNode) { log.info("notificateTransformation: reading properties"); String response = ""; String source_location = ""; String target_location = ""; String notification_url = null; String gadUser = null;//from w w w . ja v a2s . co m String gadPassword = null; String source_file = null; String target_file = null; HttpClient client = null; List authPrefs = new ArrayList(2); client = new HttpClient(); Credentials defaultcreds; // first of all. We have to take the useful data try { // source_location-->sourcefile // target_location-->targetfile // ds_custom_props-->ds_custom_props source_location = jobNode.getProperty("source_location").getValue().getString(); String[] splitedSourceLocation = source_location.split("/"); source_file = splitedSourceLocation[splitedSourceLocation.length - 1]; target_location = jobNode.getProperty("target_location").getValue().getString(); String[] splitedTargetLocation = target_location.split("/"); target_file = splitedTargetLocation[splitedTargetLocation.length - 1]; notification_url = jobNode.getProperty("notification_url").getValue().getString(); gadUser = Constantes.getGADuser(); gadPassword = Constantes.getGADpassword(); log.info("notificateTransformation: notification_url:" + notification_url); log.info("notificateTransformation: target_file:" + target_file); log.info("notificateTransformation: source_file:" + source_file); } catch (ValueFormatException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (PathNotFoundException e) { e.printStackTrace(); } catch (RepositoryException e) { e.printStackTrace(); } // Using the hhtp client. This has to be authenticated authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); defaultcreds = new UsernamePasswordCredentials(gadUser, gadPassword); client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, defaultcreds); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); // Using the POST Method PostMethod post_notification = new PostMethod(notification_url); post_notification.setDoAuthentication(true); NameValuePair[] data_notification = { new NameValuePair("sling:resourceType", "gad/transformation"), new NameValuePair("title", source_file), // new NameValuePair("file", target_file), new NameValuePair("jcr:lastModified", "") }; post_notification.setRequestBody(data_notification); post_notification.setDoAuthentication(true); try { log.info("notificateTransformation: Notificating..."); client.executeMethod(post_notification); } catch (HttpException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } // Response in Status int status = post_notification.getStatusCode(); post_notification.releaseConnection(); if (status == 200) { response = "OK. Notificated. Status =" + status; log.info(response); } else { response = "Error notificating. Status =" + status; log.error(response); } return response; }
From source file:org.iavante.sling.transcodingServer.impl.TranscodingManagerImpl.java
@SuppressWarnings("unchecked") private String notificateStatus(String mystatus, Node jobNode) { log.info("notificatingStatus: reading properties"); String response = ""; String notification_url = null; String gadUser = null;/*from w w w . j a v a2 s. c o m*/ String gadPassword = null; HttpClient client = null; List authPrefs = new ArrayList(2); client = new HttpClient(); Credentials defaultcreds; // first of all. We have to take the useful data try { notification_url = jobNode.getProperty("notification_url").getValue().getString(); gadUser = Constantes.getGADuser(); gadPassword = Constantes.getGADpassword(); } catch (ValueFormatException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (PathNotFoundException e) { e.printStackTrace(); } catch (RepositoryException e) { e.printStackTrace(); } // Using the hhtp client. This has to be authenticated authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); defaultcreds = new UsernamePasswordCredentials(gadUser, gadPassword); client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, defaultcreds); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); // Using the POST Method PostMethod post_notification = new PostMethod(notification_url); post_notification.setDoAuthentication(true); NameValuePair[] data_notification = { new NameValuePair("sling:resourceType", "gad/transformation"), new NameValuePair("state", mystatus) }; post_notification.setRequestBody(data_notification); post_notification.setDoAuthentication(true); try { log.info("notificatingStatus..."); client.executeMethod(post_notification); } catch (HttpException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } // Response in Status int status = post_notification.getStatusCode(); post_notification.releaseConnection(); if (status == 200) { response = "OK. Notificated. Status =" + status; log.info(response); } else { response = "Error notificating. Status =" + status; log.error(response); } return response; }
From source file:org.infoscoop.request.BasicAuthenticator.java
public void doAuthentication(HttpClient client, ProxyRequest request, HttpMethod method, String uid, String pwd) throws ProxyAuthenticationException { try {/*from w w w. j a v a 2s . co m*/ client.getParams().setAuthenticationPreemptive(true); // create the information of certification(an userID and a password). Credentials defaultcreds1 = new UsernamePasswordCredentials(uid, pwd); // the scope of the certification. URL urlObj = new URL(method.getURI().toString()); AuthScope scope1 = new AuthScope(urlObj.getHost(), urlObj.getPort(), null); // set a pair of a scope and an information of the certification. client.getState().setCredentials(scope1, defaultcreds1); } catch (Exception e) { throw new ProxyAuthenticationException(e); } }
From source file:org.infoscoop.request.NTLMAuthenticator.java
public void doAuthentication(HttpClient client, ProxyRequest request, HttpMethod method, String uid, String pwd) throws ProxyAuthenticationException { try {/* w w w. j a v a2 s. c o m*/ client.getParams().setAuthenticationPreemptive(true); String[] uidDomain = uid.split("\\\\"); String domain = ""; if (uidDomain.length > 1) { domain = uidDomain[0].trim(); uid = uidDomain[1].trim(); } else { uid = uidDomain[0].trim(); } // create the information of certification(an userID and a password). Credentials credentials = new NTCredentials(uid, pwd, domainController, domain); // the scope of the certification. URL urlObj = new URL(method.getURI().toString()); AuthScope scope1 = new AuthScope(urlObj.getHost(), urlObj.getPort(), null); // set a pair of a scope and an information of the certification. client.getState().setCredentials(scope1, credentials); } catch (Exception e) { throw new ProxyAuthenticationException(e); } }
From source file:org.infoscoop.request.ProxyRequest.java
private HttpClient newHttpClient() { HttpConnectionManagerParams params = new HttpConnectionManagerParams(); if (this.timeout > 0) { params.setConnectionTimeout(this.timeout); } else {//from w ww . j a va 2 s. com params.setConnectionTimeout(SOCKET_CONNECTION_TIMEOUT); } HttpConnectionManager manager = new SimpleHttpConnectionManager(); manager.setParams(params); HttpClient client = new HttpClient(manager); client.getParams().setVersion(new HttpVersion(1, 1)); if (proxy != null && proxy.isUseProxy()) { if (log.isInfoEnabled()) log.info("Proxy=" + proxy.getHost() + ":" + proxy.getPort() + ", authentication=" + proxy.needsProxyAuth()); client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); if (proxy.needsProxyAuth()) { client.getParams().setAuthenticationPreemptive(true); client.getState().setProxyCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), proxy.getProxyCredentials()); } } client.getParams().setParameter("http.socket.timeout", new Integer(this.timeout)); String allowCircularRedirect = this.getRequestHeader(ALLOW_CIRCULAR_REDIRECT) == null ? "false" : this.getRequestHeader(ALLOW_CIRCULAR_REDIRECT); if (Boolean.valueOf(allowCircularRedirect).booleanValue()) { if (log.isInfoEnabled()) log.info("Circular redirect on"); client.getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE); } return client; }
From source file:org.infoscoop.request.ProxyRequest.java
public static void main(String args[]) throws MalformedURLException { HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setConnectionTimeout(3000);/*www . j a va 2 s . c om*/ HttpConnectionManager manager = new SimpleHttpConnectionManager(); manager.setParams(params); HttpClient c = new HttpClient(manager); c.getParams().setParameter("http.socket.timeout", new Integer(5000)); Credentials credentials = new NTCredentials("test", "test", "", "");//192.168.233.2INFOSCOOP //Credentials credentials = new UsernamePasswordCredentials("INFOSCOOP\test", "test"); // the scope of the certification. URL urlObj = new URL("http://192.168.233.2/index.html"); AuthScope scope1 = new AuthScope(urlObj.getHost(), urlObj.getPort(), null); // set a pair of a scope and an information of the certification. c.getState().setCredentials(scope1, credentials); //GetMethod g = new GetMethod("http://inicio/syanai/rss.do?category=3bb34-f8f19ded28-038fb3114a5cd639e1963371842f83c0&u=bQRF9JnX%2Bm7H0n4iwJJ3ZA%3D%3D");//"http://172.22.113.111"); //GetMethod g = new GetMethod("http://172.22.113.111"); GetMethod g = new GetMethod("http://192.168.233.2/index.html"); g.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); try { c.executeMethod(g); System.out.println(g.getStatusCode()); System.out.println(g.getResponseBodyAsString()); } catch (HttpException e) { log.error("", e); } catch (IOException e) { log.error("", e); } }
From source file:org.jabox.cis.hudson.HudsonConnector.java
/** * Implementation inspired by groovy code here: * http://wiki.hudson-ci.org/display/HUDSON/Authenticating+scripted+clients *///from www. j av a 2 s. co m public boolean addProject(final Project project, final CISConnectorConfig dc) throws IOException, SAXException { HudsonConnectorConfig hcc = (HudsonConnectorConfig) dc; String url = dc.getServer().getUrl(); HttpClient client = new HttpClient(); client.getState().setCredentials(null, null, new UsernamePasswordCredentials(hcc.getUsername(), hcc.getPassword())); // Hudson does not do any authentication negotiation, // ie. it does not return a 401 (Unauthorized) // but immediately a 403 (Forbidden) client.getState().setAuthenticationPreemptive(true); String uri = url + "createItem?name=" + project.getName(); PostMethod post = new PostMethod(uri); post.setDoAuthentication(true); post.setRequestHeader("Content-type", "text/xml; charset=UTF-8"); InputStream is = getConfigXMLStream(project); String body = parseInputStream(is, project); post.setRequestBody(body); try { int result = client.executeMethod(post); LOGGER.info("Return code: " + result); for (Header header : post.getResponseHeaders()) { LOGGER.info(header.toString().trim()); } LOGGER.info(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } // Trigger the Hudson build PostMethod triggerBuild = new PostMethod(url + "/job/" + project.getName() + "/build"); client.executeMethod(triggerBuild); return true; }
From source file:org.jabox.cis.jenkins.JenkinsConnector.java
/** * Implementation inspired by groovy code here: * http://wiki.jenkins-ci.org/display/*from w ww . jav a 2s .c o m*/ * /JENKINS/Authenticating+scripted+clients */ public boolean addProject(final Project project, final CISConnectorConfig dc) throws IOException, SAXException { JenkinsConnectorConfig hcc = (JenkinsConnectorConfig) dc; String url = dc.getServer().getUrl(); HttpClient client = new HttpClient(); client.getState().setCredentials(null, null, new UsernamePasswordCredentials(hcc.getUsername(), hcc.getPassword())); // Jenkins does not do any authentication negotiation, // ie. it does not return a 401 (Unauthorized) // but immediately a 403 (Forbidden) client.getState().setAuthenticationPreemptive(true); String uri = url + "createItem?name=" + project.getName(); PostMethod post = new PostMethod(uri); post.setDoAuthentication(true); post.setRequestHeader("Content-type", "text/xml; charset=UTF-8"); InputStream is = getConfigXMLStream(project); String body = parseInputStream(is, project); post.setRequestBody(body); try { int result = client.executeMethod(post); LOGGER.info("Return code: " + result); for (Header header : post.getResponseHeaders()) { LOGGER.info(header.toString().trim()); } LOGGER.info(post.getResponseBodyAsString()); } finally { post.releaseConnection(); } // Trigger the Jenkins build PostMethod triggerBuild = new PostMethod(url + "/job/" + project.getName() + "/build"); client.executeMethod(triggerBuild); return true; }