List of usage examples for org.apache.commons.httpclient.methods MultipartPostMethod getResponseBodyAsStream
@Override public InputStream getResponseBodyAsStream() throws IOException
From source file:org.camera.service.CAMERARESTService.java
/** * File & regular parameters are passed as two separate lists and they are treated * little differently. If flPairList is not null or empty then this method uses Part * Object else no./*from ww w .j ava 2 s . c om*/ * @param pmPairList List of the name and value parameters that user has provided * @param flPairList List of the name and value (full file path)of file parameters. * It is essentially a list of files that user wishes to attach. * @return the results after executing the Post service. */ public String executePostMethod(List<NameValuePair> pmPairList, List<NameValuePair> flPairList, String serSiteURL) throws IllegalActionException { if (_debugging) { _debug("I AM IN POST METHOD"); } log.debug("I AM IN POST METHOD"); String postAddress = serSiteURL; HttpClient client = new HttpClient(); MultipartPostMethod method = new MultipartPostMethod(postAddress); List<NameValuePair> fullNameValueList = pmPairList; if (flPairList != null && flPairList.size() > 0) { fullNameValueList.addAll(flPairList); int totalSize = fullNameValueList.size(); Part[] parts = new Part[totalSize]; try { for (int i = 0; i < parts.length; i++) { String nm = fullNameValueList.get(i).getName(); String vl = fullNameValueList.get(i).getValue(); if (i > totalSize - flPairList.size() - 1) { System.out.println("FILE NAME: " + nm); File file = getFileObject(vl); System.out.println("FILE NAME: " + file.getName()); parts[i] = new FilePart(nm, file); method.addPart(parts[i]); System.out.println("PARTS: " + i + " " + parts[i].getName()); System.out.println("file Name: " + vl); } else { System.out.println("PARAMETER NAME: " + nm); System.out.println("PARAMETER Value: " + vl); parts[i] = new StringPart(nm, vl); method.addPart(parts[i]); } if (_debugging) { _debug("Value of i: " + i); } } } catch (FileNotFoundException fnfe) { if (_debugging) { _debug("File Not Found Exception: " + fnfe.getMessage()); } fnfe.printStackTrace(); throw new IllegalActionException("File Not Found: " + fnfe.getMessage()); } } else { for (NameValuePair nmPair : fullNameValueList) { method.addParameter(nmPair.getName(), nmPair.getValue()); System.out.println("Name: " + nmPair.getName() + " Value:" + nmPair.getValue()); } } InputStream rstream = null; StringBuilder results = new StringBuilder(); try { messageBldr = new StringBuilder(); messageBldr.append("In excutePostMethod, communicating with service: ").append(serSiteURL) .append(" STATUS Code: "); int statusCode = client.executeMethod(method); messageBldr.append(statusCode); log.debug("DEBUG: " + messageBldr.toString()); System.out.println(messageBldr.toString()); rstream = method.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(rstream)); log.debug("BEFORE WHILE LOOP"); String s; while ((s = br.readLine()) != null) { results.append(s).append(ServiceUtils.LINESEP); } } catch (HttpException httpe) { if (_debugging) { _debug("Fatal protocol violation: " + httpe.getMessage()); } httpe.printStackTrace(); // return "Protocol Violation"; throw new IllegalActionException("Fatal protocol violation: " + httpe.getMessage()); } catch (ConnectException conExp) { if (_debugging) { _debug("Perhaps service not reachable: " + conExp.getMessage()); } conExp.printStackTrace(); throw new IllegalActionException("Perhaps service not reachable: " + conExp.getMessage()); // return "Perhaps service not reachable"; } catch (IOException ioe) { if (_debugging) { _debug("Fatal transport error: " + ioe.getMessage()); } ioe.printStackTrace(); // return "IOException: Perhaps could not connect to the service."; throw new IllegalActionException("Fatal transport error: " + ioe.getMessage()); } catch (Exception e) { if (_debugging) { _debug("Unknown error: " + e.getMessage()); } e.printStackTrace(); // return "Exception: Unknown type error"; throw new IllegalActionException("Error: " + e.getMessage()); } finally { // Release the connection. method.releaseConnection(); } return results.toString(); }
From source file:org.junitee.testngee.ant.AntTask.java
public void execute() throws BuildException { validateOptions();/* w w w . ja v a 2 s .com*/ HttpClient client = new HttpClient(); MultipartPostMethod method = new MultipartPostMethod(runnerUrl); if (null != m_isJUnit) { if (m_isJUnit.booleanValue()) { method.addParameter("isJunit", "true"); } } // if (null != m_verbose) { // cmd.createArgument().setValue(TestNGCommandLineArgs.LOG); // cmd.createArgument().setValue(m_verbose.toString()); // } if ((null != m_outputDir)) { if (!m_outputDir.exists()) { m_outputDir.mkdirs(); } if (!m_outputDir.isDirectory()) { throw new BuildException("Output directory is not a directory: " + m_outputDir); } } if ((null != m_testjar)) { method.addParameter("testjar", m_testjar.getName()); } if (null != m_groups) { method.addParameter("groups", m_groups); } if (m_classFilesets.size() > 0) { for (String file : fileset(m_classFilesets)) { method.addParameter("classfile", file); } } if (m_xmlFilesets.size() > 0) { for (String file : fileset(m_xmlFilesets)) { try { method.addParameter("file", new File(file)); } catch (FileNotFoundException e) { throw new BuildException(e); } } } int exitValue = -1; try { client.executeMethod(method); InputStream in = method.getResponseBodyAsStream(); ZipInputStream zipIn = new ZipInputStream(in); ZipEntry zipEntry; while ((zipEntry = zipIn.getNextEntry()) != null) { byte[] buffer = new byte[4096]; File file = new File(m_outputDir, zipEntry.getName()); file.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(file); int r; while ((r = zipIn.read(buffer)) != -1) { out.write(buffer, 0, r); } out.close(); zipIn.closeEntry(); } zipIn.close(); exitValue = 0; } catch (IOException e) { throw new BuildException(e); } actOnResult(exitValue); }
From source file:org.kepler.actor.rest.RESTService.java
/** * File & regular parameters are passed as two separate lists and they are * treated little differently. If flPairList is not null or empty then this * method uses Part Object else no.//w ww.j av a2s .c o m * * @param pmPairList * List of the name and value parameters that user has provided * @param flPairList * List of the name and value (full file path)of file parameters. * It is essentially a list of files that user wishes to attach. * @return the results after executing the Post service. */ public String executePostMethod(List<NameValuePair> pmPairList, List<NameValuePair> flPairList, String serSiteURL) throws IllegalActionException { if (_debugging) { _debug("I AM IN POST METHOD"); } //log.debug("I AM IN POST METHOD"); String postAddress = serSiteURL; HttpClient client = new HttpClient(); MultipartPostMethod method = new MultipartPostMethod(postAddress); List<NameValuePair> fullNameValueList = getCombinedPairList(pmPairList); if (flPairList != null && flPairList.size() > 0) { fullNameValueList.addAll(flPairList); int totalSize = fullNameValueList.size(); Part[] parts = new Part[totalSize]; try { for (int i = 0; i < parts.length; i++) { String nm = fullNameValueList.get(i).getName(); String vl = fullNameValueList.get(i).getValue(); if (i > totalSize - flPairList.size() - 1) { System.out.println("FILE NAME: " + nm); File file = getFileObject(vl); // System.out.println("FILE NAME: " + file.getName()); parts[i] = new FilePart(nm, file); method.addPart(parts[i]); System.out.println("PARTS: " + i + " " + parts[i].getName()); System.out.println("file Name: " + vl); } else { System.out.println("PARAMETER NAME: " + nm); System.out.println("PARAMETER Value: " + vl); parts[i] = new StringPart(nm, vl); method.addPart(parts[i]); } if (_debugging) { _debug("Value of i: " + i); } } } catch (FileNotFoundException fnfe) { if (_debugging) { _debug("File Not Exception: " + fnfe.getMessage()); } fnfe.printStackTrace(); throw new IllegalActionException(this, fnfe, "File Not Found: " + fnfe.getMessage()); } } else { for (NameValuePair nmPair : fullNameValueList) { method.addParameter(nmPair.getName(), nmPair.getValue()); } } InputStream rstream = null; StringBuilder results = new StringBuilder(); try { messageBldr = new StringBuilder(); messageBldr.append("In excutePostMethod, communicating with service: ").append(serSiteURL) .append(" STATUS Code: "); int statusCode = client.executeMethod(method); messageBldr.append(statusCode); log.debug("DEBUG: " + messageBldr.toString()); System.out.println(messageBldr.toString()); // if(statusCode == 201){ // System.out.println("Succuess -- " + statusCode + // ServiceUtils.ANEMPTYSPACE + method.getResponseBodyAsString()); // }else{ // System.out.println("Failure -- " + statusCode + // ServiceUtils.ANEMPTYSPACE + method.getResponseBodyAsString()); // } rstream = method.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(rstream)); log.debug("BEFORE WHILE LOOP"); String s; while ((s = br.readLine()) != null) { results.append(s).append(ServiceUtils.LINESEP); } } catch (HttpException e) { if (_debugging) { _debug("Fatal protocol violation: " + e.getMessage()); } e.printStackTrace(); return "Protocol Violation"; } catch (ConnectException conExp) { if (_debugging) { _debug("Perhaps service '" + serSiteURL + "' is not reachable: " + conExp.getMessage()); } conExp.printStackTrace(); throw new IllegalActionException(this, conExp, "Perhaps service '" + serSiteURL + "' is not reachable: " + conExp.getMessage()); } catch (IOException ioe) { if (_debugging) { _debug("Fatal transport error: " + ioe.getMessage()); } // System.err.println("Fatal transport error: " + e.getMessage()); ioe.printStackTrace(); throw new IllegalActionException(this, ioe, "IOException: Perhaps could not" + " connect to the service '" + serSiteURL + "'. " + ioe.getMessage()); } catch (Exception e) { if (_debugging) { _debug("Error: " + e.getMessage()); } // System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); throw new IllegalActionException(this, e, "Error: " + e.getMessage()); } finally { // Release the connection. method.releaseConnection(); client = null; // close InputStream; if (rstream != null) try { rstream.close(); } catch (IOException e) { e.printStackTrace(); throw new IllegalActionException(this, e, "InputStream Close Exception: " + e.getMessage()); } } return results.toString(); }
From source file:org.openmicroscopy.is.HttpImageServer.java
public InputStream getStackStream(long pixelsID, int theC, int theT, boolean bigEndian) throws ImageServerException { MultipartPostMethod post = startCall(); try {/* w w w . j ava2 s. c o m*/ post.addParameter("Method", "GetStack"); post.addParameter("PixelsID", Long.toString(pixelsID)); post.addParameter("theC", Integer.toString(theC)); post.addParameter("theT", Integer.toString(theT)); post.addParameter("BigEndian", bigEndian ? "1" : "0"); executeCall(post); return post.getResponseBodyAsStream(); } catch (IOException ioe) { throw new ImageServerException("IO Exception: " + ioe); } //The caller will have to close the stream explicitely, this should //release the connection as well b/c of the auto-close mechanism used //in HttpMethodBase.readResponseBody(HttpConnection) line 2103. //REQUIRES FURTHER INVESTIGATION. }