Examples of GetMethod


Examples of org.apache.commons.httpclient.methods.GetMethod

    HttpClient hClient = new HttpClient();
    HostConfiguration hc = hClient.getHostConfiguration();
    hc = setProxySetttings(hClient, hc);

    GetMethod gMethod = new GetMethod(url.toString());
    gMethod.setRequestHeader("cache-control", "no-cache");
    try {
      int status = hClient.executeMethod(gMethod);
      if (status != HttpStatus.SC_OK) {
        // Check for redirection.
        if (status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY
            || status == HttpStatus.SC_SEE_OTHER
            || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
          String redirectLocation;
          Header locationHeader = gMethod
              .getResponseHeader("location");
          if (locationHeader != null) {
            redirectLocation = locationHeader.getValue();
            gMethod = new GetMethod(redirectLocation);
            status = hClient.executeMethod(gMethod);
            if (status != HttpStatus.SC_OK) {
              throw new NetworkException(gMethod.getStatusLine()
                  .getReasonPhrase());
            }
          } else {
            // The response is invalid and did not provide the
            // new
            // location for
            // the resource. Report an error or possibly handle
            // the
            // response
            // like a 404 Not Found error.
          }
        } else {
          if (status == HttpStatus.SC_UNAUTHORIZED) {
            // Retry with password.
            hc = hClient.getHostConfiguration();
            hc = setProxySetttings(hClient, hc);

            hClient.getState()
                .setCredentials(
                    new AuthScope(AuthScope.ANY_HOST,
                        AuthScope.ANY_PORT,
                        AuthScope.ANY_REALM),
                    getCredentials(feed));

            gMethod = new GetMethod(url.toString());
            gMethod.setDoAuthentication(true);
            status = hClient.executeMethod(gMethod);
            if (status != HttpStatus.SC_OK) {
              throw new NetworkException(gMethod.getStatusLine()
                  .getReasonPhrase());
            }
          } else {
            throw new NetworkException(gMethod.getStatusLine()
                .getReasonPhrase());
          }
        }
      }
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

      HttpClient hClient = new HttpClient();
      hClient.getParams().setSoTimeout(NetPropertiesHandler.timeoutValue);
      HostConfiguration hc = hClient.getHostConfiguration();
      hc = setProxySetttings(hClient, hc);

      GetMethod gMethod = new GetMethod(url.toString());
      gMethod.setFollowRedirects(false);
      gMethod.setRequestHeader("cache-control", "no-cache");
      try {
        int status = hClient.executeMethod(gMethod);
        if (status != HttpStatus.SC_OK) {
          // Check for redirection.
          if (status == HttpStatus.SC_MOVED_PERMANENTLY
              || status == HttpStatus.SC_MOVED_TEMPORARILY
              || status == HttpStatus.SC_SEE_OTHER
              || status == HttpStatus.SC_TEMPORARY_REDIRECT) {
            String redirectLocation;
            Header locationHeader = gMethod
                .getResponseHeader("location");
            if (locationHeader != null) {
              redirectLocation = locationHeader.getValue();

              gMethod = new GetMethod(redirectLocation);
              status = hClient.executeMethod(gMethod);
              if (status != HttpStatus.SC_OK) {
                throw new NetworkException(gMethod
                    .getStatusLine().getReasonPhrase());
              }
            } else {
              // The response is invalid and did not provide
              // the
              // new
              // location for
              // the resource. Report an error or possibly
              // handle
              // the
              // response
              // like a 404 Not Found error.
            }
          } else {
            if (status == HttpStatus.SC_UNAUTHORIZED) {
              hc = hClient.getHostConfiguration();
              hc = setProxySetttings(hClient, hc);
              hClient.getState().setCredentials(
                  new AuthScope(AuthScope.ANY_HOST,
                      AuthScope.ANY_PORT,
                      AuthScope.ANY_REALM),
                  getCredentials(pFeed));
              gMethod = new GetMethod(url.toString());
              gMethod.setDoAuthentication(true);
              status = hClient.executeMethod(gMethod);
              if (status != HttpStatus.SC_OK) {
                throw new NetworkException(gMethod
                    .getStatusLine().getReasonPhrase());
              }
            } else {
              throw new NetworkException(gMethod.getStatusLine()
                  .getReasonPhrase());
            }

          }
        }

        head = getHeadInfo(gMethod);
        lInputStream = gMethod.getResponseBodyAsStream();

      } catch (HttpException he) {
        throw new NetworkException(he.getMessage());
      } catch (IOException ioe) {
        throw new NetworkException(ioe.getMessage());
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

        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())
      {
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

    }

     sLog.info("Starting download: "
     + pDownload.getAttachment().getName());

    GetMethod gMethod = null;
    FileOutputStream lOutputStream = null;
    InputStream lInputStream = null;
    int lCount = 0; // Downloaded bytes count

    // STATUS CHANGE CONNECTING
    pDownload.setState(DownloadService.CONNECTING);
    notifyNetworkEvent(new NetTaskEvent(pDownload,
        NetTaskEvent.DOWNLOAD_STATUS_CHANGED));
    boolean lRedirect = false;

    try {
      boolean append = false;

      if ("http".equals(lUrl.getProtocol())) {
        HttpClient hClient = new HttpClient();
        // CB TODO, get this from core preferences.
        hClient.getParams().setSoTimeout(
            NetPropertiesHandler.timeoutValue);
        HostConfiguration hc = hClient.getHostConfiguration();
        hc = setProxySetttings(hClient, hc);
        gMethod = new GetMethod(lUrl.toExternalForm());

        // Add a byte range header asking for partial content.
        if (pDownload.getAttachment().isIncomplete()) {
          append = true;
          addByteRangeHeader(pDownload.getAttachment(), gMethod);
          gMethod.setFollowRedirects(true);
        } else {
          gMethod.setFollowRedirects(false);
        }
       
        // Need to do redirects manually to get the filename for
        // storage. We follow the redirections until a 200 OK.
        // we break in case of error.

        boolean lContinue = true;
        while (lContinue) {

          int status = hClient.executeMethod(gMethod);
          switch (status) { // Switch the result.
          case HttpStatus.SC_OK: {
            lContinue = false;
          }
            break;
          case HttpStatus.SC_MOVED_PERMANENTLY:
          case HttpStatus.SC_MOVED_TEMPORARILY:
          case HttpStatus.SC_SEE_OTHER:
          case HttpStatus.SC_TEMPORARY_REDIRECT: {
            lRedirect = true;
            // The redirection code fails for
            // localhost, use IP address as a workaround.
            String redirectLocation;
            Header locationHeader = gMethod
                .getResponseHeader("location");

            if (locationHeader != null) {
              redirectLocation = locationHeader.getValue();
              gMethod.setFollowRedirects(true);
              lUrl = new URL(redirectLocation);
            } else {

            }
          }
            break;
          case HttpStatus.SC_PARTIAL_CONTENT:
            // sLog.info("(1) Partial download granted for: "
            // + pUrl.toExternalForm());
            // sLog.info("(2) Start at byte: "
            // + gMethod.getRequestHeader("Range")
            // .getValue());
            lContinue = false;
            break;
          case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE: {
            // 28-07-2006, Duh? OK let's try again without
            // Range.
            // sLog.warn("(1) Partial download denied for: "
            // + pUrl.toExternalForm());

            Header lRHeader = gMethod
                .getResponseHeader("Content-Range");
            if (lRHeader != null) {
              // sLog.warn("(2) The content-range is: "
              // + lRHeader.getValue());
            }

            // sLog.warn(gMethod.getResponseBodyAsString());

            Header h = gMethod.getRequestHeader("Range");
            gMethod.removeRequestHeader(h);
            append = false;
          }
            break;
          case HttpStatus.SC_UNAUTHORIZED: {
            // Retry with password.
            hc = hClient.getHostConfiguration();
            hc = setProxySetttings(hClient, hc);
            hClient.getState()
                .setCredentials(
                    new AuthScope(AuthScope.ANY_HOST,
                        AuthScope.ANY_PORT,
                        AuthScope.ANY_REALM),
                    getCredentials(pDownload
                        .getAttachment().getNews()
                        .getFeedReference().resolve()));

            gMethod = new GetMethod(lUrl.toString());
            gMethod.setDoAuthentication(true);
          }
            break;
          case HttpStatus.SC_METHOD_NOT_ALLOWED: {
            // This sometimes happens, we exit with an error
            // message.
             sLog.warn(gMethod.getStatusLine().getReasonPhrase()
             + " to URL " + lUrl.toExternalForm());
          }
            break;
          default: {
            pDownload.setState(DownloadService.ERROR);
            pDownload.setMessage(gMethod.getStatusLine()
                .getReasonPhrase());

            throw new NetworkException(pDownload.getMessage());
          }
          }
        }
       
        // CB TODO Asser the HEADER information.
        HTTPHEADInfo lInfo = getHeadInfo(gMethod);
        if (!append) {
          try {
            pDownload.getAttachment().setContentSize(lInfo.length);
          } catch (IllegalArgumentException iae) {
            // Likely parsing of the HEAD Date failed.
          }
        } else {
          pDownload.getAttachment().setContentSize(
              (int) pDownload.getAttachment().getLength());
        }
        pDownload.getAttachment().setContentDate(
            lInfo.getModifiedLong());

        pDownload.setLength(pDownload.getAttachment().getContentSize());
        lInputStream = gMethod.getResponseBodyAsStream();
      } else {
        pDownload.setLength(0);
        lInputStream = lUrl.openStream();
      }

      // REDIRECT Handling.
      // In case of re-direct we re-create a download file.
      // We don't store the re-direct URL. (It is volatile).
      // The volatile redirect URL is used to generate the file name.
      // if it can't be generated, we create a filename from the
      // the RSS entry and set it in the enclosure object. Before we
      // redownload
      // the file, we also want to
      // check if the file is by chance an already existing file in
      // the folder, cache or player. If so we cancel the download.
      // We fire a change event.

      boolean lInspect = false;
      if (lRedirect) {
        File lFile = pPAttachment.getFile(true);
        pPAttachment.setFileName(lFile.getName());
      }

      File lF = pPAttachment.getFile();
      if (!lF.exists()) {
        lF.createNewFile();
        lInspect = false;
      }

      // --- File Inspection.
      // CB FIXME Do we have to inspect only after redirect?
      // We don't inspect a file with a random number.
      if (lInspect) {
        // If we re-inspect and remark, we ignore the
        // candidate restriction has we are already
        // in the download phase. The enclosure was either
        // a candidate before or downloaded manually.
        pPAttachment.setCandidate(false);

        // CB TODO, migrate inspections.
        // XFeedLogic.getInstance().inspectSingleFile(pEncl);
        // XFeedLogic.getInstance().markSingleEnclosure(pEncl);

        if (pPAttachment.isIncomplete()) {
          // CB FIXME This is a problem, as we
          // need to execute again to get the byte range.
          // Rethink the whole redirect handling.
        }

        // CB TODO, this is very jPodder specific.
        // updating the candidate, can likely be done
        // in the model.

        // pEncl.getFeed().updateSingleCandidate(pEncl,
        // Configuration.getInstance().getMarkMax());

        if (!pPAttachment.isMarked()) {
          pDownload.setState(DownloadService.CANCELLED);
          pDownload.setMessage("Redirect leads to existing file");
          throw new NetworkException(
              "Redirect leads to existing file");
        }
      }

      // THE ENCLOSURE FILE IS NEVER NULL, AND EXISTS FROM HERE!
      // WE ARE IN DEEP SHIT OTHERWISE.

       sLog.info(Messages.getString("tasks.download.newDownload")
       + pPAttachment.getFile());

      lOutputStream = new FileOutputStream(lF, append);

      if (append) {
        lCount = (int) pPAttachment.getFile().length();
      }
      if (pDownload.getLength() != 0) {
      } else {
        // Length of the file is unknown.
        // This not good. (only for HTTP)
      }

      pDownload.setStart(lCount);

      // STATUS CHANGE DOWNLOADING
      pDownload.setState(DownloadService.DOWNLOADING);
      notifyNetworkEvent(new NetTaskEvent(pDownload,
          NetTaskEvent.DOWNLOAD_STATUS_CHANGED));
      int read;
      byte[] buffer = new byte[8192];
      while ((read = lInputStream.read(buffer)) > 0) {
        lOutputStream.write(buffer, 0, read);
        lCount += read;
        pDownload.setCurrent(lCount);
        if (pDownload.getState() == DownloadService.CANCELLED
            || pDownload.getState() == DownloadService.PAUZED) {
          notifyNetworkEvent(new NetTaskEvent(pDownload,
              NetTaskEvent.DOWNLOAD_STATUS_CHANGED));
          break;
        }
      }

      // sLog.info(Messages
      // .getMessage("net.download.start", new String[] {
      // lCount + "", pDownload.lengthOfTask + "" }));

    } catch (org.apache.commons.httpclient.HttpException he) {
      pDownload.setState(DownloadService.ERROR);
      pDownload.setMessage(he.getMessage());
    } catch (java.io.FileNotFoundException fnfe) {
      pDownload.setState(DownloadService.ERROR);
      pDownload.setMessage(fnfe.getMessage());
    } catch (java.io.IOException ioe) {
      pDownload.setState(DownloadService.ERROR);
      pDownload.setMessage(ioe.getMessage());
    } catch (Exception e) {
      pDownload.setState(DownloadService.ERROR);
      pDownload.setMessage(e.getMessage());
    }

    // catch (java.lang.IllegalArgumentException iae) {
    // pDownload.setState(DownloadLogic.ERROR);
    // pDownload.setMessage("Programatic Error");
    // }
    finally {

      lState = pDownload.getState();
      if (lState == DownloadService.DOWNLOADING) {
        // Incompleted downlads are checked.
        if (pDownload.getLength() != 0
            && lCount != pDownload.getLength()) {
          // sLog.info("Partial download of : "
          // + pDownload.getEnclosure().getFile());
          pDownload.setState(DownloadService.ERROR);
          // pDownload.setMessage(Messages
          // .getString("nettask.incomplete"));
        } else {
          // STATUS CHANGE COMPLETE
          pDownload.setState(DownloadService.COMPLETED);
          notifyNetworkEvent(new NetTaskEvent(pDownload,
              NetTaskEvent.DOWNLOAD_STATUS_CHANGED));
          notifyNetworkEvent(new NetTaskEvent(pDownload,
              NetTaskEvent.DOWNLOAD_SUCCESS));
        }
      }

      if (lState == DownloadService.ERROR) {
        notifyNetworkEvent(new NetTaskEvent(pDownload,
            NetTaskEvent.DOWNLOAD_STATUS_CHANGED));
        throw new NetworkException(pDownload.getMessage());
      }

      if (gMethod != null) {
        gMethod.abort();
        gMethod.releaseConnection();
      }
      try {
        if (lOutputStream != null)
          lOutputStream.close();
        if (lInputStream != null) {
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

    HttpMethodBase method = null;
    try {
      if (properties != null && properties.containsKey(IConnectionPropertyConstants.POST))
        method = new PostMethod(link.toString());
      else
        method = new GetMethod(link.toString());
    } catch (RuntimeException e) {
      throw new IOException(e.getMessage());
    }

    /* Ignore Cookies */
 
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

    /* Init Client */
    HttpClient client = initClient(properties);

    /* Init the connection */
    GetMethod getMethod = null;
    InputStream inS = null;
    try {
      getMethod = initConnection(link, properties);

      /* Proxy if required */
      setupProxy(link, client);

      /* Authentication if required */
      setupAuthentication(link, client, getMethod);

      /* Open the connection */
      inS = openConnection(client, getMethod);

      /* Try to pipe the resulting stream into a GZipInputStream */
      inS = pipeStream(inS, getMethod);
    } catch (IOException e) {
      throw new ConnectionException(Activator.getDefault().createErrorStatus(e.getMessage(), e));
    }

    /* In case authentication required / failed */
    if (getMethod.getStatusCode() == HTTP_ERROR_AUTH_REQUIRED)
      throw new AuthenticationRequiredException(Activator.getDefault().createErrorStatus("Authentication required!", null)); //$NON-NLS-1$

    /* In case proxy-authentication required / failed */
    if (getMethod.getStatusCode() == HTTP_ERROR_PROXY_AUTH_REQUIRED)
      throw new ProxyAuthenticationRequiredException(Activator.getDefault().createErrorStatus("Proxy-Authentication required!", null)); //$NON-NLS-1$

    /* If status code is 4xx, throw an IOException with the status code included */
    if (getMethod.getStatusCode() >= HTTP_ERRORS)
      throw new ConnectionException(Activator.getDefault().createErrorStatus("Server returned HTTP Status " + String.valueOf(getMethod.getStatusCode()), null)); //$NON-NLS-1$

    /* In case the Feed has not been modified since */
    if (getMethod.getStatusCode() == HTTP_STATUS_NOT_MODIFIED)
      throw new NotModifiedException(Activator.getDefault().createInfoStatus("Feed has not been modified since!", null)); //$NON-NLS-1$

    /* In case response body is not available */
    if (inS == null)
      throw new ConnectionException(Activator.getDefault().createErrorStatus("Response Stream is not available!", null)); //$NON-NLS-1$
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

  }

  private GetMethod initConnection(URI link, Map<Object, Object> properties) throws IOException {

    /* Create the Get Method. Wrap any RuntimeException into an IOException */
    GetMethod getMethod = null;
    try {
      getMethod = new GetMethod(link.toString());
    } catch (RuntimeException e) {
      throw new IOException(e.getMessage());
    }

    /* Ignore Cookies */
    getMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);

    /* Set Headers */
    setHeaders(properties, getMethod);

    /* Follow Redirects */
    getMethod.setFollowRedirects(true);

    return getMethod;
  }
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

   }
  
   protected void makeWebRequest(String url, String responseContent)
   {
      HttpClient client = new HttpClient();
      GetMethod method = new GetMethod(url);
      int responseCode = 0;
      try
      {
         responseCode = client.executeMethod(method);
        
         assertTrue("Get OK with url: " +url + " responseCode: " +responseCode
               , responseCode == HttpURLConnection.HTTP_OK);
        
         InputStream rs = method.getResponseBodyAsStream();
         InputStreamReader reader = new InputStreamReader(rs);
         StringWriter writer = new StringWriter();
         int c;
         while ((c = reader.read())  != -1)
            writer.write(c);
        
         String rsp = writer.toString();
        
         assertTrue("Response contains " + responseContent, rsp.indexOf(responseContent) >= 0);
      }
      catch (IOException e)
      {
         e.printStackTrace();
         fail("HttpClient executeMethod fails." +e.toString());
      }
      finally
      {
         method.releaseConnection();
      }
   }
View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

   /**
    *  Access the passed URL and return back the response
    */
   private String getResponse(String url) throws Exception
   {
      HttpMethodBase request = new GetMethod(url);
      client.executeMethod(request);

      String responseBody = request.getResponseBodyAsString();
      if (responseBody == null)
      {
         throw new Exception("Unable to get response from server for URL: " + url);
      }

View Full Code Here

Examples of org.apache.commons.httpclient.methods.GetMethod

   }
  
   private String accessURL(String url) throws Exception
   {
      HttpClient httpConn = new HttpClient();
      HttpMethodBase request = new GetMethod(baseURL + url);
      log.debug("RequestURI: " + request.getURI());
      int responseCode = httpConn.executeMethod(request);
      String response = request.getStatusText();
      log.debug("responseCode="+responseCode+", response="+response);
      String content = request.getResponseBodyAsString();
      log.debug(content);
      assertEquals(HttpURLConnection.HTTP_OK, responseCode);
      return content;
   }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.