List of usage examples for org.apache.commons.httpclient HttpClient executeMethod
public int executeMethod(HttpMethod paramHttpMethod) throws IOException, HttpException
From source file:gov.tva.sparky.hbase.RestProxy.java
public static boolean DeleteHbaseRow(String strTablename, String strRowKey) throws URIException { Configuration conf = new Configuration(false); conf.addResource("hadoop-default.xml"); conf.addResource("sparky-site.xml"); int port = conf.getInt("sparky.hbase.restPort", 8092); String uri = conf.get("sparky.hbase.restURI", "http://socdvmhbase"); boolean bResult = false; BufferedReader br = null;//from w w w . j av a 2 s .c o m HttpClient client = new HttpClient(); String strRestPath = uri + ":" + port + "/" + strTablename + "/" + strRowKey; DeleteMethod delete_method = new DeleteMethod(strRestPath); try { int returnCode = client.executeMethod(delete_method); if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { System.out.println("The Post method is not implemented by this URI"); } else { bResult = true; } } catch (Exception e) { System.out.println(e); } finally { delete_method.releaseConnection(); if (br != null) try { br.close(); } catch (Exception fe) { } } return bResult; }
From source file:de.mpg.imeji.presentation.util.Scripts.java
public static String login(String frameworkUrl, String userName, String password) throws Exception { StringTokenizer tokens = new StringTokenizer(frameworkUrl, "//"); tokens.nextToken();/*from w ww .j ava2s. co m*/ StringTokenizer hostPort = new StringTokenizer(tokens.nextToken(), ":"); String host = hostPort.nextToken(); int port = Integer.parseInt(hostPort.nextToken()); HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(host, port, "http"); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check"); login.addParameter("j_username", userName); login.addParameter("j_password", password); client.executeMethod(login); //System.out.println("Login form post: " + login.getStatusLine().toString()); login.releaseConnection(); CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies()); Cookie sessionCookie = logoncookies[0]; PostMethod postMethod = new PostMethod("/aa/login"); postMethod.addParameter("target", frameworkUrl); client.getState().addCookie(sessionCookie); client.executeMethod(postMethod); //System.out.println("Login second post: " + postMethod.getStatusLine().toString()); if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) { throw new HttpException("Wrong status code: " + login.getStatusCode()); } String userHandle = null; Header headers[] = postMethod.getResponseHeaders(); for (int i = 0; i < headers.length; ++i) { if ("Location".equals(headers[i].getName())) { String location = headers[i].getValue(); int index = location.indexOf('='); userHandle = new String(Base64.decode(location.substring(index + 1, location.length()))); //System.out.println("location: "+location); //System.out.println("handle: "+userHandle); } } if (userHandle == null) { throw new ServiceException("User not logged in."); } return userHandle; }
From source file:com.bluexml.xforms.demo.Util.java
private static Collection<? extends Vector<String>> getInstancesById(String alfrescohost, String user, String id) throws Exception { Set<Vector<String>> result = new HashSet<Vector<String>>(); PostMethod post = new PostMethod(alfrescohost + "service/xforms/workflow"); post.setParameter("username", user); post.setParameter("method", "getActiveWorkflows"); post.setParameter("arg0", id); HttpClient client = new HttpClient(); client.executeMethod(post); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(post.getResponseBodyAsStream()); Node root = document.getDocumentElement(); for (int i = 0; i < root.getChildNodes().getLength(); i++) { Node n = root.getChildNodes().item(i); if (n.getNodeType() == Element.ELEMENT_NODE) { Vector<String> v = new Vector<String>(); v.add(findNode(n.getChildNodes(), "id").getTextContent()); v.add(findNode(n.getChildNodes(), "startDate").getTextContent()); String initiator = findNode(findNode(n.getChildNodes(), "initiator").getChildNodes(), "id") .getTextContent();//from ww w . j av a2s .co m String protocol = findNode( findNode(findNode(n.getChildNodes(), "initiator").getChildNodes(), "storeRef") .getChildNodes(), "protocol").getTextContent(); String identifier = findNode( findNode(findNode(n.getChildNodes(), "initiator").getChildNodes(), "storeRef") .getChildNodes(), "identifier").getTextContent(); String username = getUserName(alfrescohost, user, protocol, identifier, initiator); v.add(username); v.add(findNode(findNode(n.getChildNodes(), "definition").getChildNodes(), "version") .getTextContent()); result.add(v); } } return result; }
From source file:com.bluexml.xforms.demo.Util.java
/** * Authenticates a user with an Alfresco instance. * // www . ja v a2 s .c o m * @param host * the address (protocol, hostname, port number) of the host where the BlueXML XForms * webscript is deployed, with NO trailing slash. If NULL, defaults to localhost:8080 * @param userName * the user name to test, which should be known to Alfresco * @param password * the plain text password to test * @return true if the authentication succeeded, false if the authentication failed or if an * exception occurred */ public static boolean authenticate(String host, String userName, String password) { PostMethod post = new PostMethod(host + "service/xforms/auth"); post.setParameter("username", userName); post.setParameter("password", password); HttpClient client = new HttpClient(); try { client.executeMethod(post); } catch (HttpException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } String result; try { result = post.getResponseBodyAsString(); } catch (IOException e) { e.printStackTrace(); return false; } if (result == null) { return false; } result = result.trim(); return result.equals("success"); }
From source file:com.bluexml.xforms.demo.Util.java
public static Collection<? extends Vector<String>> showAvailableContent(String alfrescohost, String user, String password, String taskId) throws Exception { Set<Vector<String>> result = new HashSet<Vector<String>>(); PostMethod post = new PostMethod(alfrescohost + "service/xforms/workflow"); post.setParameter("username", user); post.setParameter("method", "getPackageContents"); post.setParameter("arg0", taskId); HttpClient client = new HttpClient(); client.executeMethod(post); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(post.getResponseBodyAsStream()); Node root = document.getDocumentElement(); for (int i = 0; i < root.getChildNodes().getLength(); i++) { Node n = root.getChildNodes().item(i); if (n.getNodeType() == Element.ELEMENT_NODE) { String protocol = findNode(findNode(n.getChildNodes(), "storeRef").getChildNodes(), "protocol") .getTextContent();/*w ww. ja v a2 s . com*/ String workspace = findNode(findNode(n.getChildNodes(), "storeRef").getChildNodes(), "identifier") .getTextContent(); String id = findNode(n.getChildNodes(), "id").getTextContent(); String url = alfrescohost + "service/api/node/" + protocol + "/" + workspace + "/" + id; GetMethod get = new GetMethod(url); client = new HttpClient(); UsernamePasswordCredentials upc = new UsernamePasswordCredentials(user, password); client.getState().setCredentials(AuthScope.ANY, upc); get.setDoAuthentication(true); client.executeMethod(get); document = builder.parse(get.getResponseBodyAsStream()); Node rootContent = document.getDocumentElement(); Vector<String> v = new Vector<String>(); String downloadUrl = findNode(rootContent.getChildNodes(), "content").getAttributes() .getNamedItem("src").getNodeValue(); String title = findNode(rootContent.getChildNodes(), "title").getTextContent(); String icon = findNode(rootContent.getChildNodes(), "alf:icon").getTextContent(); v.add(title); v.add(downloadUrl); v.add(icon); result.add(v); } } return result; }
From source file:com.bluexml.xforms.demo.Util.java
public static Set<Vector<String>> getPooledTasks(String alfrescohost, String user) { Set<Vector<String>> result = new HashSet<Vector<String>>(); try {//ww w . j a va 2s . co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // the mapping.xml file is private to the controller. Better use the API. // Document mappingDocument = builder.parse(mapping); PostMethod post = new PostMethod(alfrescohost + "service/xforms/workflow"); post.setParameter("username", user); post.setParameter("method", "getPooledTasks"); post.setParameter("arg0", user); HttpClient client = new HttpClient(); client.executeMethod(post); Document document = builder.parse(new ByteArrayInputStream( ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + post.getResponseBodyAsString()).getBytes())); Node root = document.getDocumentElement(); root = findNode(root.getChildNodes(), "list"); for (int i = 0; i < root.getChildNodes().getLength(); i++) { Node n = root.getChildNodes().item(i); if (n.getNodeType() == Element.ELEMENT_NODE) { Vector<String> v = new Vector<String>(); String instanceId = findNode( findNode(findNode(n.getChildNodes(), "path").getChildNodes(), "instance") .getChildNodes(), "id").getTextContent(); String taskId = findNode(n.getChildNodes(), "id").getTextContent(); v.add(taskId); v.add(findNode(n.getChildNodes(), "title").getTextContent()); v.add(findNode(n.getChildNodes(), "description").getTextContent()); String name = findNode(n.getChildNodes(), "name").getTextContent(); v.add(getFormName(name)); v.add(getContentId(alfrescohost, user, taskId)); v.add(instanceId); result.add(v); } } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:com.bluexml.xforms.demo.Util.java
public static Set<Vector<String>> getToDoTasks(String alfrescohost, String user) { Set<Vector<String>> result = new HashSet<Vector<String>>(); try {/*w w w. ja v a 2 s.co m*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // the mapping.xml file is private to the controller. Better use the API. // Document mappingDocument = builder.parse(mapping); PostMethod post = new PostMethod(alfrescohost + "service/xforms/workflow"); post.setParameter("username", user); post.setParameter("method", "getAssignedTasks"); post.setParameter("arg0", user); post.setParameter("arg1", "IN_PROGRESS"); HttpClient client = new HttpClient(); client.executeMethod(post); Document document = builder.parse(new ByteArrayInputStream( ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + post.getResponseBodyAsString()).getBytes())); Node root = document.getDocumentElement(); root = findNode(root.getChildNodes(), "list"); for (int i = 0; i < root.getChildNodes().getLength(); i++) { Node n = root.getChildNodes().item(i); if (n.getNodeType() == Element.ELEMENT_NODE) { Vector<String> v = new Vector<String>(); String instanceId = findNode( findNode(findNode(n.getChildNodes(), "path").getChildNodes(), "instance") .getChildNodes(), "id").getTextContent(); String taskId = findNode(n.getChildNodes(), "id").getTextContent(); v.add(taskId); v.add(findNode(n.getChildNodes(), "title").getTextContent()); v.add(findNode(n.getChildNodes(), "description").getTextContent()); String name = findNode(n.getChildNodes(), "name").getTextContent(); v.add(getFormName(name)); v.add(getContentId(alfrescohost, user, taskId)); v.add(instanceId); result.add(v); } } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:com.bluexml.xforms.demo.Util.java
/** * Calls the XForms webapp with initialization values. * //w ww .j ava2 s . c o m * @param alfrescohost * the address (protocol, host name, port number) to the Alfresco instance, with NO * trailing slash. e.g. http://www.bluexml.com/alfresco * @param xformshost * the address (including context) of the xforms webapp host, with NO trailing slash. * e.g: http://localhost:8081/myforms * @param formsproperties * the path to the forms.properties file * @param redirectxml * the path to the redirect.xml file * @return */ public static boolean initWebApp(String alfrescohost, String xformshost, String formsproperties, String redirectxml) { if (StringUtils.trimToNull(xformshost) == null) { return false; } String serviceURL = xformshost + "/xforms?init=true"; serviceURL += "&alfrescoHost=" + alfrescohost; serviceURL += "&redirectXmlFile=" + redirectxml; serviceURL += "&formsPropertiesFile=" + formsproperties; GetMethod get = new GetMethod(serviceURL); HttpClient client = new HttpClient(); try { client.executeMethod(get); } catch (HttpException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } String result; try { result = get.getResponseBodyAsString(); } catch (IOException e) { e.printStackTrace(); return false; } if (result == null) { return false; } result = result.trim(); return result.equals("success"); }
From source file:it.geosolutions.figis.requester.HTTPUtils.java
/** * Used to query for REST resources.// www .j a va 2 s . c o m * * @param url The URL of the REST resource to query about. * @param username * @param pw * @return true on 200, false on 404. * @throws RuntimeException on unhandled status or exceptions. */ public static boolean exists(String url, String username, String pw) { GetMethod httpMethod = null; try { HttpClient client = new HttpClient(); setAuth(client, url, username, pw); httpMethod = new GetMethod(url); client.getHttpConnectionManager().getParams().setConnectionTimeout(2000); int status = client.executeMethod(httpMethod); switch (status) { case HttpStatus.SC_OK: return true; case HttpStatus.SC_NOT_FOUND: return false; default: throw new RuntimeException("Unhandled response status at '" + url + "': (" + status + ") " + httpMethod.getStatusText()); } } catch (ConnectException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (httpMethod != null) { httpMethod.releaseConnection(); } } }
From source file:name.chengchao.myhttpclient.version3_1.HttpClient3Util.java
/** * ?url?ResponseBody,method=get//from w w w . ja v a 2s.com * * @param url exp:http://192.168.1.1:8080/dir/target.html * @return byte[]? */ public static byte[] getDataFromUrl(String url, int timeout) { if (StringUtils.isBlank(url)) { logger.error("url is blank!"); return null; } HttpClient httpClient = new HttpClient(); // httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000); // ? httpClient.getParams().setSoTimeout(timeout); GetMethod method = new GetMethod(url); // fix??? method.setRequestHeader("Connection", "close"); // ??1 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false)); try { int statusCode = httpClient.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { return method.getResponseBody(); } else { throw new RuntimeException("http request error,return code:" + statusCode + ",msg:" + new String(method.getResponseBody())); } } catch (HttpException e) { method.abort(); logger.error(e.getMessage()); } catch (IOException e) { method.abort(); logger.error(e.getMessage()); } finally { // Release the connection. method.releaseConnection(); } return null; }