List of usage examples for org.apache.commons.httpclient HttpClient getParams
public HttpClientParams getParams()
From source file:de.mpg.escidoc.services.transformation.Util.java
/** * Queries CoNE service and returns the result as DOM node. * The returned XML has the following structure: * <cone>/*from www . ja v a 2 s . c o m*/ * <author> * <familyname>Buxtehude-Mlln</familyname> * <givenname>Heribert</givenname> * <prefix>von und zu</prefix> * <title>Knig</title> * </author> * <author> * <familyname>Mller</familyname> * <givenname>Peter</givenname> * </author> * </authors> * * @param authors * @return */ public static Node queryCone(String model, String query) { DocumentBuilder documentBuilder; String queryUrl = null; try { logger.info("queryCone: " + model + " query: " + query); documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element element = document.createElement("cone"); document.appendChild(element); queryUrl = PropertyReader.getProperty("escidoc.cone.service.url") + model + "/query?format=jquery&q=" + URLEncoder.encode(query, "UTF-8"); String detailsUrl = PropertyReader.getProperty("escidoc.cone.service.url") + model + "/resource/$1?format=rdf"; HttpClient client = new HttpClient(); client.getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); GetMethod method = new GetMethod(queryUrl); String coneSession = getConeSession(); if (coneSession != null) { method.setRequestHeader("Cookie", "JSESSIONID=" + coneSession); } ProxyHelper.executeMethod(client, method); if (method.getStatusCode() == 200) { String[] results = method.getResponseBodyAsString().split("\n"); for (String result : results) { if (!"".equals(result.trim())) { String id = result.split("\\|")[1]; // TODO "&redirect=true" must be reinserted again GetMethod detailMethod = new GetMethod(id + "?format=rdf&eSciDocUserHandle=" + Base64.encode(AdminHelper.getAdminUserHandle().getBytes("UTF-8"))); detailMethod.setFollowRedirects(true); if (coneSession != null) { detailMethod.setRequestHeader("Cookie", "JSESSIONID=" + coneSession); } ProxyHelper.executeMethod(client, detailMethod); if (logger.isDebugEnabled()) { logger.debug("CoNE query: " + id + "?format=rdf&eSciDocUserHandle=" + Base64.encode(AdminHelper.getAdminUserHandle().getBytes("UTF-8")) + " returned " + detailMethod.getResponseBodyAsString()); } if (detailMethod.getStatusCode() == 200) { Document details = documentBuilder.parse(detailMethod.getResponseBodyAsStream()); element.appendChild(document.importNode(details.getFirstChild(), true)); } else { logger.error("Error querying CoNE: Status " + detailMethod.getStatusCode() + "\n" + detailMethod.getResponseBodyAsString()); } } } } else { logger.error("Error querying CoNE: Status " + method.getStatusCode() + "\n" + method.getResponseBodyAsString()); } return document; } catch (Exception e) { logger.error("Error querying CoNE service. This is normal during unit tests. (" + queryUrl + ") .Otherwise it should be clarified if any measures have to be taken.", e); logger.debug("Stacktrace", e); return null; //throw new RuntimeException(e); } }
From source file:com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptorIntegrationTest.java
@Test public void testRewriting() throws Exception { HttpClient client = new HttpClient(); client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); int status = client.executeMethod(getPostMethod()); assertEquals(200, status);// w ww . ja v a 2 s .co m }
From source file:com.predic8.membrane.core.interceptor.authentication.BasicAuthenticationInterceptorIntegrationTest.java
@Test public void testDeny() throws Exception { Rule rule = new ForwardingRule(new ForwardingRuleKey("localhost", "*", ".*", 8000), "thomas-bayer.com", 80); HttpRouter router = new HttpRouter(); router.getRuleManager().addRuleIfNew(rule); BasicAuthenticationInterceptor interceptor = new BasicAuthenticationInterceptor(); Map<String, String> mapping = new HashMap<String, String>(); mapping.put("admin", "admin"); interceptor.setUsers(mapping);/* w w w. j a va2 s. c o m*/ router.getTransport().getInterceptors().add(interceptor); HttpClient client = new HttpClient(); client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); int status = client.executeMethod(getGetMethod()); assertEquals(401, status); //TODO Basic Authentication test }
From source file:com.predic8.membrane.core.interceptor.rest.REST2SOAPInterceptorIntegrationTest.java
@Test public void testRest() throws Exception { HttpClient client = new HttpClient(); client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); GetMethod get = new GetMethod("http://localhost:3004/bank/37050198"); int status = client.executeMethod(get); //System.out.println(get.getResponseBodyAsString()); assertEquals(200, status);// w w w. j a va2 s . c o m }
From source file:de.mpg.escidoc.services.transformation.Util.java
/** * Queries the CoNE service and transforms the result into a DOM node. * //from w w w . j a v a 2 s. c om * @param model The type of object (e.g. "persons") * @param name The query string. * @param ou Specialty for persons * @param coneSession A JSESSIONID to not produce a new session with each call. * @return A DOM node containing the results. */ public static Node queryConeExactWithIdentifier(String model, String identifier, String ou) { DocumentBuilder documentBuilder; try { logger.info("queryConeExactWithIdentifier: " + model + " identifier: " + identifier + " ou: " + ou); documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element element = document.createElement("cone"); document.appendChild(element); String queryUrl = PropertyReader.getProperty("escidoc.cone.service.url") + model + "/query?format=jquery&dc:identifier/" + URLEncoder.encode("rdf:value", "UTF-8") + "=" + URLEncoder.encode("\"" + identifier + "\"", "UTF-8") + "&" + URLEncoder.encode("escidoc:position/eprints:affiliatedInstitution", "UTF-8") + "=" + URLEncoder.encode("\"*" + ou + "*\"", "UTF-8"); String detailsUrl = PropertyReader.getProperty("escidoc.cone.service.url") + model + "/resource/$1?format=rdf"; HttpClient client = new HttpClient(); client.getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); GetMethod method = new GetMethod(queryUrl); String coneSession = getConeSession(); if (coneSession != null) { method.setRequestHeader("Cookie", "JSESSIONID=" + coneSession); } ProxyHelper.executeMethod(client, method); if (logger.isDebugEnabled()) { logger.debug("CoNE query: " + queryUrl + " returned " + method.getResponseBodyAsString()); } if (method.getStatusCode() == 200) { ArrayList<String> results = new ArrayList<String>(); results.addAll(Arrays.asList(method.getResponseBodyAsString().split("\n"))); Set<String> oldIds = new HashSet<String>(); for (String result : results) { if (!"".equals(result.trim())) { String id = result.split("\\|")[1]; if (!oldIds.contains(id)) { // TODO "&redirect=true" must be reinserted again GetMethod detailMethod = new GetMethod(id + "?format=rdf&eSciDocUserHandle=" + Base64.encode(AdminHelper.getAdminUserHandle().getBytes("UTF-8"))); detailMethod.setFollowRedirects(true); ProxyHelper.setProxy(client, detailsUrl.replace("$1", id)); client.executeMethod(detailMethod); // TODO "&redirect=true" must be reinserted again if (logger.isDebugEnabled()) { logger.debug("CoNE query: " + id + "?format=rdf&eSciDocUserHandle=" + Base64.encode(AdminHelper.getAdminUserHandle().getBytes("UTF-8")) + " returned " + detailMethod.getResponseBodyAsString()); } if (detailMethod.getStatusCode() == 200) { Document details = documentBuilder.parse(detailMethod.getResponseBodyAsStream()); element.appendChild(document.importNode(details.getFirstChild(), true)); } else { logger.error("Error querying CoNE: Status " + detailMethod.getStatusCode() + "\n" + detailMethod.getResponseBodyAsString()); } oldIds.add(id); } } } } else { logger.error("Error querying CoNE: Status " + method.getStatusCode() + "\n" + method.getResponseBodyAsString()); } return document; } catch (Exception e) { logger.error("Error querying CoNE service. This is normal during unit tests. " + "Otherwise it should be clarified if any measures have to be taken.", e); return null; //throw new RuntimeException(e); } }
From source file:com.predic8.membrane.core.interceptor.acl.AccessControlInterceptorTest.java
private int callService(String uri) throws Exception { HttpClient client = new HttpClient(); client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); GetMethod get = new GetMethod("http://localhost:4000" + uri); return client.executeMethod(get); }
From source file:com.predic8.membrane.core.interceptor.rest.RESTBLZServiceIntegrationTest.java
@Test public void testRest() throws Exception { HttpClient client = new HttpClient(); client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); GetMethod get = new GetMethod("http://localhost:3005/bank/37050198"); int status = client.executeMethod(get); System.out.println(get.getResponseBodyAsString()); assertEquals(200, status);//from w w w .j a va 2 s .c o m }
From source file:eu.learnpad.core.rest.DefaultRestResource.java
public HttpClient getClient(String userName, String password) { HttpClient httpClient = new HttpClient(); httpClient.getParams().setAuthenticationPreemptive(true); Credentials credentials = new UsernamePasswordCredentials(userName, password); AuthScope authentication = new AuthScope(DefaultRestResource.HOSTNAME, DefaultRestResource.PORT, AuthScope.ANY_REALM);/*from w w w. j a v a2 s . c o m*/ httpClient.getState().setCredentials(authentication, credentials); return httpClient; }
From source file:de.linsin.alterego.notification.AppNotificationService.java
HttpClient setUp() { HttpClient client = new HttpClient(); client.getParams().setParameter(HTTP_USERAGENT, "IRC Bot"); return client; }
From source file:InteractiveAuthenticationExample.java
private void doDemo() throws IOException { HttpClient client = new HttpClient(); client.getParams().setParameter(CredentialsProvider.PROVIDER, new ConsoleAuthPrompter()); GetMethod httpget = new GetMethod("http://target-host/requires-auth.html"); httpget.setDoAuthentication(true);//from w w w . j a va 2 s. c om try { // execute the GET int status = client.executeMethod(httpget); // print the status and response System.out.println(httpget.getStatusLine().toString()); System.out.println(httpget.getResponseBodyAsString()); } finally { // release any connection resources used by the method httpget.releaseConnection(); } }