Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpMethod


        doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);

        // Create a document.
        doPostAsAdmin(spaceName, pageName, null, "save", null, null);

        HttpMethod ret = null;
        // Upload the attachment
        ret = doUploadAsAdmin(spaceName, pageName,
            new HashMap<String, byte[]>() {{
                put(FILENAME, ATTACHMENT_CONTENT.getBytes());
            }});
View Full Code Here


        doPostAsAdmin(spaceName, pageName, null, "delete", "?confirm=1", null);

        // Create a document.
        doPostAsAdmin(spaceName, pageName, null, "save", null, null);

        HttpMethod ret = null;
        // Upload the attachment
        ret = doUploadAsAdmin(spaceName, pageName,
            new HashMap<String, byte[]>() {{
                put(FILENAME, ATTACHMENT_CONTENT.getBytes());
            }});

        // Make sure it's there.
        Assert.assertEquals(ATTACHMENT_CONTENT, TestUtils.getPageAsString(attachURL));

        // Do a rollback.
        doPostAsAdmin(spaceName, pageName, null, "rollback", "?rev=1.1&confirm=1", null);

        // Make sure it's nolonger there.
        ret = doPostAsAdmin(spaceName, pageName, FILENAME, "download", null, null);
        Assert.assertFalse(ATTACHMENT_CONTENT.equals(new String(ret.getResponseBody(), "UTF-8")));
        Assert.assertEquals(404, ret.getStatusCode());
    }
View Full Code Here

        doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);

        // Create a document.
        doPostAsAdmin(spaceName, pageName, null, "save", null, null);

        HttpMethod ret = null;
        // Upload the attachment
        ret = doUploadAsAdmin(spaceName, pageName,
            new HashMap<String, byte[]>() {{
                put(FILENAME, ATTACHMENT_CONTENT.getBytes());
            }});
View Full Code Here

        doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);

        // Create a document. v1.1
        doPostAsAdmin(spaceName, pageName, null, "save", null, null);

        HttpMethod ret = null;
        // Upload the attachment v2.1
        ret = doUploadAsAdmin(spaceName, pageName,
            new HashMap<String, byte[]>() {{
                put(FILENAME, ATTACHMENT_CONTENT.getBytes());
            }});

        // Make sure it's there.
        Assert.assertEquals(ATTACHMENT_CONTENT, TestUtils.getPageAsString(attachURL));

        // Delete it v3.1
        doPostAsAdmin(spaceName, pageName, FILENAME, "delattachment", null, null);

        // Upload again v4.1
        ret = doUploadAsAdmin(spaceName, pageName,
            new HashMap<String, byte[]>() {{
                put(FILENAME, versionTwo.getBytes());
            }});

        // Make sure it's there.
        Assert.assertEquals(versionTwo, TestUtils.getPageAsString(attachURL));

        // Do a rollback. v5.1
        doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=2.1&confirm=1", null);

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

        // Make sure it is version1
        Assert.assertEquals(ATTACHMENT_CONTENT, TestUtils.getPageAsString(attachURL));

        // Do rollback to version2. v6.1
        doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=4.1&confirm=1", null);

        // Make sure it is version2
        Assert.assertEquals(versionTwo, TestUtils.getPageAsString(attachURL));

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

{
    public static final String[] ADMIN_CREDENTIALS = new String[] {"Admin", "admin"};

    public static String getPageAsString(final String address) throws IOException
    {
        final HttpMethod ret = doPost(address, null, null);
        return new String(ret.getResponseBody(), "UTF-8");
    }
View Full Code Here

    tureq.setFirstName(firstName);
    tureq.setLastName(lastName);
    tureq.setUserName(userName);
    //}

    HttpMethod meth = fetch(tureq, httpClientInstance);
    if (meth == null) {
      setFetchError();
    }else{

      Header responseHeader = meth.getResponseHeader("Content-Type");
      String mimeType;
      if (responseHeader == null) {
        setFetchError();
        mimeType = null;
      } else {
        mimeType = responseHeader.getValue();
      }
 
      if (mimeType != null && mimeType.startsWith("text/html")) {
        // we have html content, let doDispatch handle it for
        // inline rendering, update hreq for next content request
        String body;
        try {
          body = meth.getResponseBodyAsString();
        } catch (IOException e) {
          Tracing.logWarn("Problems when tunneling URL::" + tureq.getUri(), e, TunnelComponent.class);
          htmlContent = "Error: cannot display inline :"+tureq.getUri()+": Unknown transfer problem '";
          return;
        }
        SimpleHtmlParser parser = new SimpleHtmlParser(body);
        if (!parser.isValidHtml()) { // this is not valid HTML, deliver
          // asynchronuous
        }
        meth.releaseConnection();
        htmlHead = parser.getHtmlHead();
        jsOnLoad = parser.getJsOnLoad();
        htmlContent = parser.getHtmlContent();
      } else {
        htmlContent = "Error: cannot display inline :"+tureq.getUri()+": mime type was '" + mimeType +
View Full Code Here

    tureq.setFirstName(firstName);
    tureq.setLastName(lastName);
    tureq.setUserName(userName);
    //}

    HttpMethod meth = fetch(tureq, httpClientInstance);
    if (meth == null) {
      setFetchError();
      return null;
    }

    Header responseHeader = meth.getResponseHeader("Content-Type");
    if (responseHeader == null) {
      setFetchError();
      return null;
    }

    String mimeType = responseHeader.getValue();
    if (mimeType != null && mimeType.startsWith("text/html")) {
      // we have html content, let doDispatch handle it for
      // inline rendering, update hreq for next content request
      String body;
      try {
        body = meth.getResponseBodyAsString();
      } catch (IOException e) {
        Tracing.logWarn("Problems when tunneling URL::" + tureq.getUri(), e, TunnelComponent.class);
        return null;
      }
      SimpleHtmlParser parser = new SimpleHtmlParser(body);
      if (!parser.isValidHtml()) { // this is not valid HTML, deliver
        // asynchronuous
        return new HttpRequestMediaResource(meth);
      }
      meth.releaseConnection();
      htmlHead = parser.getHtmlHead();
      jsOnLoad = parser.getJsOnLoad();
      htmlContent = parser.getHtmlContent();
      setDirty(true);
    } else return new HttpRequestMediaResource(meth); // this is a async browser
View Full Code Here

   */
  public HttpMethod fetch(TURequest tuReq, HttpClient client) {

    String modulePath = tuReq.getUri();

    HttpMethod meth = null;
    String method = tuReq.getMethod();
    if (method.equals("GET")) {
      GetMethod cmeth = new GetMethod(modulePath);
      String queryString = tuReq.getQueryString();
      if (queryString != null) cmeth.setQueryString(queryString);
      meth = cmeth;
      if (meth == null) return null;
      // if response is a redirect, follow it
      meth.setFollowRedirects(true);
     
    } else if (method.equals("POST")) {
      String type = tuReq.getContentType();
      if (type == null || type.equals("application/x-www-form-urlencoded")) {
        //regular post, no file upload
      }

      PostMethod pmeth = new PostMethod(modulePath);
      Set postKeys = tuReq.getParameterMap().keySet();
      for (Iterator iter = postKeys.iterator(); iter.hasNext();) {
        String key = (String) iter.next();
        String vals[] = (String[])tuReq.getParameterMap().get(key);
        for (int i = 0; i < vals.length; i++) {
          pmeth.addParameter(key, vals[i]);
        }
        meth = pmeth;
      }
      if (meth == null) return null;
      // Redirects are not supported when using POST method!
      // See RFC 2616, section 10.3.3, page 62
    }
   
    // Add olat specific headers to the request, can be used by external
    // applications to identify user and to get other params
    // test page e.g. http://cgi.algonet.se/htbin/cgiwrap/ug/test.py
    meth.addRequestHeader("X-OLAT-USERNAME", tuReq.getUserName());
    meth.addRequestHeader("X-OLAT-LASTNAME", tuReq.getLastName());
    meth.addRequestHeader("X-OLAT-FIRSTNAME", tuReq.getFirstName());
    meth.addRequestHeader("X-OLAT-EMAIL", tuReq.getEmail());

    try {
      client.executeMethod(meth);
      return meth;
    } catch (Exception e) {
      meth.releaseConnection();
    }
    return null;
  }
View Full Code Here

      Mapper mapper = new Mapper() {
        public MediaResource handle(String relPath, HttpServletRequest hreq) {
          MediaResource mr = null;
          String method = hreq.getMethod();
          String uri = relPath;
          HttpMethod meth = null;
 
          if (uri == null) uri = (startUri == null) ? "" : startUri;
          if (uri.length() > 0 && uri.charAt(0) != '/') uri = "/" + uri;
         
          //String contentType = hreq.getContentType();
 
          // if (allowedToSendPersonalHeaders) {
          String userName = ident.getName();
          User u = ident.getUser();
          String lastName = u.getProperty(UserConstants.LASTNAME, loc);
          String firstName = u.getProperty(UserConstants.FIRSTNAME, loc);
          String email = u.getProperty(UserConstants.EMAIL, loc);
 
          if (method.equals("GET")) {
            GetMethod cmeth = new GetMethod(uri);
            String queryString = hreq.getQueryString();
            if (queryString != null) cmeth.setQueryString(queryString);
            meth = cmeth;
            // if response is a redirect, follow it
            if (meth == null) return null;
            meth.setFollowRedirects(true);
           
          } else if (method.equals("POST")) {
            //if (contentType == null || contentType.equals("application/x-www-form-urlencoded")) {
              // regular post, no file upload
            //}
            Map params = hreq.getParameterMap();
            PostMethod pmeth = new PostMethod(uri);
            Set postKeys = params.keySet();
            for (Iterator iter = postKeys.iterator(); iter.hasNext();) {
              String key = (String) iter.next();
              String vals[] = (String[]) params.get(key);
              for (int i = 0; i < vals.length; i++) {
                pmeth.addParameter(key, vals[i]);
              }
              meth = pmeth;
            }
            if (meth == null) return null;
            // Redirects are not supported when using POST method!
            // See RFC 2616, section 10.3.3, page 62

          }
 
         
          // Add olat specific headers to the request, can be used by external
          // applications to identify user and to get other params
          // test page e.g. http://cgi.algonet.se/htbin/cgiwrap/ug/test.py
          meth.addRequestHeader("X-OLAT-USERNAME", userName);
          meth.addRequestHeader("X-OLAT-LASTNAME", lastName);
          meth.addRequestHeader("X-OLAT-FIRSTNAME", firstName);
          meth.addRequestHeader("X-OLAT-EMAIL", email);
 
          boolean ok = false;
          try {
            httpClientInstance.executeMethod(meth);
            ok = true;
          } catch (Exception e) {
            // handle error later
          }
 
          if (!ok) {
            // error
            meth.releaseConnection();
            return new NotFoundMediaResource(relPath);
          }
 
          // get or post successfully
          Header responseHeader = meth.getResponseHeader("Content-Type");
          if (responseHeader == null) {
            // error
            return new NotFoundMediaResource(relPath);
          }
          mr = new HttpRequestMediaResource(meth);
View Full Code Here

      httpClientParams.setConnectionManagerTimeout(2500);
      httpClient.setParams(httpClientParams);
      try {
        // Could throw IllegalArgumentException if argument is not a valid url
        // (e.g. contains whitespaces)
        HttpMethod httpMethod = new GetMethod(XING_NAME_VALIDATION_URL + textElement.getValue());
        // Don't allow redirects since otherwise, we won't be able to get the correct status
        httpMethod.setFollowRedirects(false);
        // Get the user profile page
        httpClient.executeMethod(httpMethod);
        int httpStatusCode = httpMethod.getStatusCode();
        // Looking at the HTTP status code tells us whether a user with the given Xing name exists.
        if (httpStatusCode == HttpStatus.SC_OK) {
          // If the user exists, we get a 200...
          result = true;
        } else if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
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.