Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpMethod


    return httpClient;
  }

  public Object uploadFiles(String url, ModelMap<String, Object> parameters,
      File... files) throws Exception {
    HttpMethod method = initMethod(url, "post", parameters);
    // 处理多文件上传
    this.processMultipartRequest(parameters, (PostMethod) method, files);
    return doCall(method);
  }
View Full Code Here


    return doCall(method);
  }

  public Object callRemote(String url, String methodType,
      ModelMap<String, Object> parameters) throws Exception {
    HttpMethod method = initMethod(url, methodType, parameters);
    return doCall(method);
  }
View Full Code Here

   * @return
   * @throws Exception
   */
  private HttpMethod initMethod(String url, String methodType,
      ModelMap<String, Object> parameters) throws Exception {
    HttpMethod method = null;
   
    if(parameters == null)
      parameters = new ModelMap<String, Object>();

    Object args = parameters != null ? parameters
        .get(ModelMap.RPC_ARGS_KEY) : null;

    if (methodType.equalsIgnoreCase("get")) {
      method = new GetMethod(url);
    } else if (methodType.equalsIgnoreCase("post")) {
      PostMethod postMethod = new PostMethod(url);
      if (args != null) {
        byte[] output = constructArgs(method, args);
        postMethod.setRequestEntity(new ByteArrayRequestEntity(output));
      }
      method = postMethod;
    } else if (methodType.equalsIgnoreCase("put")) {
      method = new PutMethod(url);
      if (args != null) {
        byte[] output = constructArgs(method, args);
        ((PutMethod) method)
            .setRequestEntity(new ByteArrayRequestEntity(output));
      }
    } else if (methodType.equalsIgnoreCase("delete")) {
      method = new DeleteMethod(url);
    }

    if (parameters.get(ModelMap.RPC_ARGS_KEY) != null)
      method.addRequestHeader("content-type", "application/javabean");

    if (parameters != null) {
      Object value;
      List<String> queryStringList = new ArrayList<String>();
      Set<String> keySet = parameters.keySet();
      for (String key : keySet) {
        if (!key.toString().equalsIgnoreCase(ModelMap.RPC_ARGS_KEY)
            && !key.toString().equalsIgnoreCase(
                ModelMap.FILE_ITEM_ARGS_KEY)) {
          value = parameters.get(key);
          method.getParams().setParameter(key, value);
          queryStringList.add(key + "=" + value);
        }
      }

      if (methodType.equalsIgnoreCase("get"))
        method.setQueryString(StringUtils.join(queryStringList, "&"));
    }
    return method;
  }
View Full Code Here

   */
  public int downloadHttpFile(URL url, FileWrapper destFile, IProxySettings proxySettings)
    throws IOException
  {
    BufferedInputStream is = null;
    HttpMethod method = null;
    int resultCode = -1;
    int result = -1;
    try
    {
      if (s_log.isDebugEnabled())
      {
        s_log.debug("downloadHttpFile: downloading file (" + destFile.getName() + ") from url: " + url);
      }
      HttpClient client = new HttpClient();
      setupProxy(proxySettings, client, url);

      method = new GetMethod(url.toString());
      method.setFollowRedirects(true);

      resultCode = client.executeMethod(method);
      if (s_log.isDebugEnabled())
      {
        s_log.debug("downloadHttpFile: response code was: " + resultCode);
      }

      if (resultCode != 200) { throw new FileNotFoundException("Failed to download file from url (" + url
        + "): HTTP Response Code=" + resultCode); }
      InputStream mis = method.getResponseBodyAsStream();

      is = new BufferedInputStream(mis);

      if (s_log.isDebugEnabled())
      {
        s_log.debug("downloadHttpFile: writing http response body to file: " + destFile.getAbsolutePath());
      }

      result = copyBytesToFile(mis, destFile);
    }
    catch (IOException e)
    {
      s_log.error("downloadHttpFile: Unexpected exception while "
        + "attempting to open an HTTP connection to url (" + url + ") to download a file ("
        + destFile.getAbsolutePath() + "): " + e.getMessage(), e);
      throw e;
    }
    finally
    {
      closeInputStream(is);
      method.releaseConnection();
    }
    return result;
  }
View Full Code Here

    return "v";
  }

  private InputStream retreiveXMLReply(String partOfSpeech, String word) {
    HttpClient client = HttpClientFactory.getHttpClientInstance();
    HttpMethod method = new GetMethod(MORPHOLOGICAL_SERVICE_ADRESS);
    NameValuePair posValues = new NameValuePair(PART_OF_SPEECH_PARAM, partOfSpeech);
    NameValuePair wordValues = new NameValuePair(GLOSS_TERM_PARAM, word);
    if (log.isDebug()) {
      String url = MORPHOLOGICAL_SERVICE_ADRESS + "?" + PART_OF_SPEECH_PARAM + "=" + partOfSpeech + "&" + GLOSS_TERM_PARAM + "=" + word;
      log.debug("Send GET request to morph-service with URL: " + url);
    }
    method.setQueryString(new NameValuePair[] { posValues, wordValues });
    try {
      client.executeMethod(method);
      int status = method.getStatusCode();
      if (status == HttpStatus.SC_NOT_MODIFIED || status == HttpStatus.SC_OK) {
        if (log.isDebug()) {
          log.debug("got a valid reply!");
        }
      } else if (method.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
        log.error("Morphological Service unavailable (404)::" + method.getStatusLine().toString());
      } else {
        log.error("Unexpected HTTP Status::" + method.getStatusLine().toString());
      }
    } catch (Exception e) {
      log.error("Unexpected exception trying to get flexions!", e);
    }
    Header responseHeader = method.getResponseHeader("Content-Type");
    if (responseHeader == null) {
      // error
      log.error("URL not found!");
    }
    HttpRequestMediaResource mr = new HttpRequestMediaResource(method);
View Full Code Here

   {
      String baseURLNoAuth = "http://" + getServerHostForURL()
              + ":" + Integer.getInteger("web.port", 8080) + "/";
      String path = "war1/TestServlet";
      // try to perform programmatic auth without supplying login information.
      HttpMethod indexGet = null;
      try
      {
         indexGet = new GetMethod(baseURLNoAuth + path + "?operation=login");
         int responseCode = httpConn.executeMethod(indexGet);
         assertTrue("Get Error(" + responseCode + ")",
               responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR);
         // assert access to the restricted area of the first application is denied.
         SSOBaseCase.checkAccessDenied(this.httpConn, baseURLNoAuth +
               "war1/restricted/restricted.html");
         // assert access to the second application is not granted, as no successful login
         // was performed (and therefore no ssoid has been set).
         SSOBaseCase.checkAccessDenied(this.httpConn, baseURLNoAuth + "war2/index.html");
      }
      finally
      {
         if(indexGet != null)
           indexGet.releaseConnection();
      }
      // try to perform programmatic auth with no valid username/password.
      path = path + "?operation=login&username=dummy&pass=dummy";
      try
      {
         indexGet = new GetMethod(baseURLNoAuth + path);
         int responseCode = httpConn.executeMethod(indexGet);
         assertTrue("Get Error(" + responseCode + ")",
               responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR);
         // assert access to the restricted applications remains denied.
         SSOBaseCase.checkAccessDenied(this.httpConn, baseURLNoAuth +
               "war1/restricted/restricted.html");
         SSOBaseCase.checkAccessDenied(this.httpConn, baseURLNoAuth + "war2/index.html");
      }
      finally
      {
         if(indexGet != null)
           indexGet.releaseConnection();
      }
   }
View Full Code Here

   public void testSuccessfulLogin() throws Exception
   {
      String baseURLNoAuth = "http://" + getServerHostForURL()
              + ":" + Integer.getInteger("web.port", 8080) + "/";
      String path1 = "war1/TestServlet?operation=login&username=jduke&pass=theduke";
      HttpMethod indexGet = null;
      HttpMethod indexGet2 = null;
      try
      {
         indexGet = new GetMethod(baseURLNoAuth + path1);
         int responseCode = httpConn.executeMethod(indexGet);
         assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
         // assert access to the restricted are of the first application is now allowed.
         SSOBaseCase.checkAccessAllowed(this.httpConn, baseURLNoAuth +
               "war1/restricted/restricted.html");
         // assert the sso cookie has been created.
         SSOBaseCase.processSSOCookie(this.httpConn.getState(), baseURLNoAuth, baseURLNoAuth);
         // assert access to the second application is allowed.
         SSOBaseCase.checkAccessAllowed(this.httpConn, baseURLNoAuth + "war2/index.html");

         // perform a programmatic logout and assert access is not allowed anymore.
         indexGet2 = new GetMethod(baseURLNoAuth + "war1/TestServlet?operation=logout");
         responseCode = httpConn.executeMethod(indexGet2);
         assertTrue("Get OK("+responseCode+")", responseCode == HttpURLConnection.HTTP_OK);
         SSOBaseCase.checkAccessDenied(this.httpConn, baseURLNoAuth +
               "war1/restricted/restricted.html");
         SSOBaseCase.checkAccessDenied(this.httpConn, baseURLNoAuth + "war2/index.html");
      }
      finally
      {
         if(indexGet != null)
           indexGet.releaseConnection();
         if(indexGet2 != null)
           indexGet2.releaseConnection();
      }
   }
View Full Code Here

        // Make sure it's the same as version 1.
        Assert.assertEquals("<p>" + versionOne + "</p>", TestUtils.getPageAsString(pageURL));

        // Make sure the latest current version is actually v3.1
        HttpMethod ret =
            doPostAsAdmin(spaceName, pageName, null, "preview", "xpage=plain",
                new HashMap<String, String>() {{
                    put("content", "{{velocity}}$doc.getVersion(){{/velocity}}");
                }});
        Assert.assertEquals("<p>3.1</p>", new String(ret.getResponseBody(), "UTF-8"));
    }
View Full Code Here

        doPostAsAdmin("Test", "Attachment", null, "save", null,
            new HashMap<String, String>() {{
                put("content", test);
            }});

        HttpMethod ret = null;

        // Test getAttachment()
        ret = doPostAsAdmin("Test", "Attachment", null, "view", "xpage=plain", null);
        Assert.assertEquals("<p>" + ATTACHMENT_CONTENT + "</p>", ret.getResponseBodyAsString());

        // Test downloadAction.
        ret = doPostAsAdmin("Test", "Attachment", FILENAME, "download", null, null);
        Assert.assertEquals(ATTACHMENT_CONTENT, new String(ret.getResponseBody(), "UTF-8"));
        Assert.assertEquals(200, ret.getStatusCode());

        // Make sure there is exactly 1 version of this attachment.
        ret = doPostAsAdmin("Test", "Attachment", null, "preview", "xpage=plain",
            new HashMap<String, String>() {{
                put("content", "{{velocity}}$doc.getAttachment('"
                    + FILENAME + "').getVersions().size(){{/velocity}}");
            }});
        Assert.assertEquals("<p>1</p>", ret.getResponseBodyAsString());

        // Make sure that version contains the correct content.
        ret = doPostAsAdmin("Test", "Attachment", null, "preview", "xpage=plain",
            new HashMap<String, String>() {{
                put("content", "{{velocity}}$doc.getAttachment('" + FILENAME
                    + "').getAttachmentRevision('1.1').getContentAsString(){{/velocity}}");
            }});
        Assert.assertEquals("<p>" + ATTACHMENT_CONTENT + "</p>", ret.getResponseBodyAsString());
    }
View Full Code Here

        final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME;

        // Delete the document if it exists.
        doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);

        HttpMethod ret = null;
        // Upload the XAR to import.
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(this.getClass().getResourceAsStream("/Test.Attachment2.xar"), baos);
        ret = doUploadAsAdmin("XWiki", "XWikiPreferences",
            new HashMap<String, byte[]>() {{
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.