Examples of HttpMethod


Examples of opendap.dap.http.HTTPMethod

    long size = 4;
    fos.write(NcStream.MAGIC_START);

    // header
    HTTPMethod method = null;
    try {
      // get the header
      String url = remoteURI + "?req=header";
      method = httpClient.newMethodGet(url);
      if (showRequest) System.out.printf("CdmRemote request %s %n", url);
      int statusCode = method.execute();

      if (statusCode == 404)
        throw new FileNotFoundException(method.getURI() + " " + method.getStatusLine());

      if (statusCode >= 300)
        throw new IOException(method.getURI() + " " + method.getStatusLine());

      InputStream is = method.getResponseBodyAsStream();
      size += IO.copyB(is, fos, IO.default_socket_buffersize);

    } finally {
      if (method != null) method.close();
    }

    for (Variable v : getVariables()) {
      StringBuilder sbuff = new StringBuilder(remoteURI);
      sbuff.append("?var=");
      sbuff.append(URLEncoder.encode(v.getShortName(), "UTF-8"));

      if (showRequest)
        System.out.println(" CdmRemote data request for variable: " + v.getFullName() + " url=" + sbuff);

      try {
        method = httpClient.newMethodGet(sbuff.toString());
        int statusCode = method.execute();

        if (statusCode == 404)
          throw new FileNotFoundException(method.getPath() + " " + method.getStatusLine());

        if (statusCode >= 300)
          throw new IOException(method.getPath() + " " + method.getStatusLine());

        int wantSize = (int) (v.getSize());
        Header h = method.getResponseHeader("Content-Length");
        if (h != null) {
          String s = h.getValue();
          int readLen = Integer.parseInt(s);
          if (readLen != wantSize)
            throw new IOException("content-length= " + readLen + " not equal expected Size= " + wantSize);
        }

        InputStream is = method.getResponseBodyAsStream();
        size += IO.copyB(is, fos, IO.default_socket_buffersize);

      } finally {
        if (method != null) method.close();
      }
    }

    fos.flush();
    fos.close();
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    session = new HTTPSession();

    boolean needtest = true;

    HTTPMethod method = null;
    try {
      method = session.newMethodHead(url);

      doConnect(method);

      Header head = method.getResponseHeader("Accept-Ranges");
      if (head == null) {
        needtest = true; // header is optional - need more testing

      } else if (head.getValue().equalsIgnoreCase("bytes")) {
        needtest = false;

      } else if (head.getValue().equalsIgnoreCase("none")) {
        throw new IOException("Server does not support byte Ranges");
      }

      head = method.getResponseHeader("Content-Length");
      if (head == null) {
        throw new IOException("Server does not support Content-Length");
      }

      try {
        total_length = Long.parseLong(head.getValue());
      } catch (NumberFormatException e) {
        throw new IOException("Server has malformed Content-Length header");
      }

    } finally {
      if (method != null) method.close();
    }

    if (needtest && !rangeOk(url))
      throw new IOException("Server does not support byte Ranges");
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

      session = null;
    }
  }

  private boolean rangeOk(String url) {
    HTTPMethod method = null;
    try {
      method = session.newMethodGet(url);
      method.setFollowRedirects(true);
      method.setRequestHeader("Range", "bytes=" + 0 + "-" + 1);
      doConnect(method);

      int code = method.getStatusCode();
      if (code != 206)
        throw new IOException("Server does not support Range requests, code= " + code);

      // clear stream
      method.close();
      return true;

    } catch (IOException e) {
      return false;

    } finally {
      if (method != null) method.close();
    }
  }
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    if (end >= total_length)
      end = total_length - 1;

    if (debug) System.out.println(" HTTPRandomAccessFile bytes=" + pos + "-" + end + ": ");

    HTTPMethod method = null;
    try {
      method = session.newMethodGet(url);
      method.setFollowRedirects(true);
      method.setRequestHeader("Range", "bytes=" + pos + "-" + end);
      doConnect(method);

      int code = method.getStatusCode();
      if (code != 206)
        throw new IOException("Server does not support Range requests, code= " + code);

      String s = method.getResponseHeader("Content-Length").getValue();
      if (s == null)
        throw new IOException("Server does not send Content-Length header");

      int readLen = Integer.parseInt(s);
      readLen = Math.min(len, readLen);

      InputStream is = method.getResponseAsStream();
      readLen = copy(is, buff, offset, readLen);
      return readLen;

    } finally {
      if (method != null) method.close();
    }
  }
View Full Code Here

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

Examples of org.apache.commons.httpclient.HttpMethod

    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

Examples of org.apache.commons.httpclient.HttpMethod

   * @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

Examples of org.apache.commons.httpclient.HttpMethod

   */
  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

Examples of org.apache.commons.httpclient.HttpMethod

    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

Examples of org.apache.commons.httpclient.HttpMethod

   {
      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
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.