Examples of HttpMethod


Examples of opendap.dap.http.HTTPMethod

    }
  }

  // not sure what other opendap servers do, so fall back on check for dds
  static private ServiceType checkIfDods(HTTPSession session, String location) throws IOException {
    HTTPMethod method = null;
    // Strip off any trailing constraints
    if(location.indexOf('?') >= 0) {
        location = location.substring(0,location.indexOf('?'));
    }
    // Strip off any trailing .dds, .das, or .dods
    if(location.endsWith(".dds"))
        location = location.substring(0,location.length()-".dds".length());
    if(location.endsWith(".das"))
      location = location.substring(0,location.length()-".das".length());
    if(location.endsWith(".dods"))
      location = location.substring(0,location.length()-".dods".length());
    // Must encode the URL before sending
    location = EscapeStrings.escapeURL(location);
    try {
      // For some reason, the head method is not using credentials
      // method = session.newMethodHead(location + ".dds");
      method = session.newMethodGet(location + ".dds");

      int status = method.execute();
      if (status == 200) {
        Header h = method.getResponseHeader("Content-Description");
        if ((h != null) && (h.getValue() != null)) {
          String v = h.getValue();
          if (v.equalsIgnoreCase("dods-dds") || v.equalsIgnoreCase("dods_dds"))
            return ServiceType.OPENDAP;
          else
            throw new IOException("OPeNDAP Server Error= " + method.getResponseAsString());
        }
      }
      if (status == 401)
        throw new IOException("Unauthorized to open dataset " + location);

      // not dods
      return null;

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

Examples of opendap.dap.http.HTTPMethod

  public static String getContentAsString(HTTPSession session, String urlString) throws IOException {
    HTTPSession useSession = session;
    try {
      if (useSession == null)
        useSession = new HTTPSession();
      HTTPMethod m = useSession.newMethodGet(urlString);
      m.execute();
      return m.getResponseAsString();
    } finally {
      if ((session == null) && (useSession != null))
        useSession.close();
    }
  }
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    HTTPSession session = null;

    try {

      session = new HTTPSession();
      HTTPMethod m = session.newMethodPut(urlString);

      m.setRequestContentAsString(content);

      m.execute();

      int resultCode = m.getStatusCode();

      // followRedirect wont work for PUT
      if (resultCode == 302) {
        String redirectLocation;
        Header locationHeader = m.getResponseHeader("location");
        if (locationHeader != null) {
          redirectLocation = locationHeader.getValue();
          resultCode = putContent(redirectLocation, content);
        }
      }
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    HTTPSession useSession = session;
    try {
      if (useSession == null)
        useSession = new HTTPSession();

      HTTPMethod m = useSession.newMethodGet(urlString);
      m.setFollowRedirects(true);
      m.setRequestHeader("Accept-Encoding", "gzip,deflate");

      int status = m.execute();
      if (status != 200) {
        throw new RuntimeException("failed status = " + status);
      }

      String charset = m.getResponseCharSet();
      if (charset == null) charset = "UTF-8";

      // check for deflate and gzip compression
      Header h = m.getResponseHeader("content-encoding");
      String encoding = (h == null) ? null : h.getValue();

      if (encoding != null && encoding.equals("deflate")) {
        byte[] body = m.getResponseAsBytes();
        InputStream is = new BufferedInputStream(new InflaterInputStream(new ByteArrayInputStream(body)), 10000);
        return readContents(is, charset, maxKbytes);

      } else if (encoding != null && encoding.equals("gzip")) {
        byte[] body = m.getResponseAsBytes();
        InputStream is = new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(body)), 10000);
        return readContents(is, charset, maxKbytes);

      } else {
        byte[] body = m.getResponseAsBytes(maxKbytes * 1000);
        return new String(body, charset);
      }

    } catch (Exception e) {
      e.printStackTrace();
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    HTTPSession useSession = session;
    try {
      if (useSession == null)
        useSession = new HTTPSession();

      HTTPMethod m = useSession.newMethodGet(urlString);
      m.setRequestHeader("Accept-Encoding", "gzip,deflate");

      int status = m.execute();

      if (status != 200) {
        throw new RuntimeException("failed status = " + status);
      }

      String charset = m.getResponseCharSet();
      if (charset == null) charset = "UTF-8";

      // check for deflate and gzip compression
      Header h = m.getResponseHeader("content-encoding");
      String encoding = (h == null) ? null : h.getValue();

      if (encoding != null && encoding.equals("deflate")) {
        InputStream is = new BufferedInputStream(new InflaterInputStream(m.getResponseAsStream()), 10000);
        IO.writeToFile(is, file.getPath());

      } else if (encoding != null && encoding.equals("gzip")) {
        InputStream is = new BufferedInputStream(new GZIPInputStream(m.getResponseAsStream()), 10000);
        IO.writeToFile(is, file.getPath());

      } else {
        IO.writeToFile(m.getResponseAsStream(), file.getPath());
      }

    } catch (Exception e) {
      e.printStackTrace();
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    try {
      if (useSession == null)
        useSession = new HTTPSession();

      HTTPMethod m = useSession.newMethodGet(urlString);
      m.setRequestHeader("Accept-Encoding", "gzip,deflate");
      m.setRequestHeader("Range", "bytes=" + start + "-" + end);

      int status = m.execute();
      if ((status != 200) && (status != 206)) {
        throw new RuntimeException("failed status = " + status);
      }

      String charset = m.getResponseCharSet();
      if (charset == null) charset = "UTF-8";

      // check for deflate and gzip compression
      Header h = m.getResponseHeader("content-encoding");
      String encoding = (h == null) ? null : h.getValue();

      if (encoding != null && encoding.equals("deflate")) {
        InputStream is = new BufferedInputStream(new InflaterInputStream(m.getResponseAsStream()), 10000);
        nbytes = IO.appendToFile(is, file.getPath());

      } else if (encoding != null && encoding.equals("gzip")) {
        InputStream is = new BufferedInputStream(new GZIPInputStream(m.getResponseAsStream()), 10000);
        nbytes = IO.appendToFile(is, file.getPath());

      } else {
        nbytes = IO.appendToFile(m.getResponseAsStream(), file.getPath());
      }

    } catch (Exception e) {
      e.printStackTrace();
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

  {
    if(session == null)
        session = new HTTPSession();
    session.setConnectionManagerTimeout( this.connectionTimeout );
    session.setSoTimeout( this.socketTimeout );
    HTTPMethod method = session.newMethodGet( uri.toString() );
    method.setFollowRedirects( this.followRedirects );
    method.setRequestHeader( "Accept-Encoding", this.contentEncoding );

   method.execute();
    int statusCode = method.getStatusCode();
    if ( statusCode == 200 || statusCode == 201 )
    {
      return method;
    }
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    remoteURI = temp;

    httpClient = new HTTPSession();

    // get the header
    HTTPMethod method = null;
    try {
      String url = remoteURI + "?req=header";
      method = httpClient.newMethodGet(url);
      method.setFollowRedirects(true);
      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.getResponseAsStream();
      NcStreamReader reader = new NcStreamReader();
      reader.readStream(is, this);
      this.location = SCHEME + remoteURI;

    } finally {
      if (method != null) method.close();
    }
    long took = System.currentTimeMillis() - start;
    if (showRequest) System.out.printf(" took %d msecs %n", took);
  }
View Full Code Here

Examples of opendap.dap.http.HTTPMethod

    sbuff.append( URLEncoder.encode(f.toString(), "UTF-8")); // % escape entire thing varname and section

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

    HTTPMethod method = null;
    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());

      Header h = method.getResponseHeader("Content-Length");
      if (h != null) {
        String s = h.getValue();
        int readLen = Integer.parseInt(s);
        if (showRequest)
          System.out.printf(" content-length = %d%n", readLen);
        if (v.getDataType() != DataType.SEQUENCE) {
          int wantSize = (int) (v.getElementSize() * (section == null ? v.getSize() : section.computeSize()));
          if (readLen != wantSize)
            throw new IOException("content-length= " + readLen + " not equal expected Size= " + wantSize); // LOOK
        }
      }

      InputStream is = method.getResponseAsStream();
      NcStreamReader reader = new NcStreamReader();
      NcStreamReader.DataResult result = reader.readData(is, this);

      assert v.getFullNameEscaped().equals(result.varNameFullEsc);
      result.data.setUnsigned(v.isUnsigned());
      return result.data;

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

Examples of opendap.dap.http.HTTPMethod

  }

  public static InputStream sendQuery(String remoteURI, String query) throws IOException {

    HTTPSession session = null;
    HTTPMethod method = null;
    HTTPMethodStream hmstream = null;
    InputStream stream = null;
    int statusCode = 0;

    StringBuilder sbuff = new StringBuilder(remoteURI);
    sbuff.append("?");
    sbuff.append(query);

    if (showRequest)
      System.out.println(" CdmRemote sendQuery=" + sbuff);

    try {

      try {
        session = new HTTPSession();
        method = session.newMethodGet(sbuff.toString());
        statusCode = method.execute();
      } catch (HTTPException he) {
        throw new IOException(he);
      }

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

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

      stream = method.getResponseBodyAsStream();
      hmstream = new HTTPMethodStream(session, method, stream);
      return hmstream;

    } catch (IOException ioe) {
      if (session != null) session.close();
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.