Package opendap.dap.http

Examples of opendap.dap.http.HTTPSession


   * Do a HEAD call on the URL.
   * Look for the header "Content-Description" = "ncstream" or "dods".
   */
  static private ServiceType disambiguateHttp(String location) throws IOException {

    HTTPSession session = new HTTPSession();

    // have to do dods first
    ServiceType result = checkIfDods(session,location);
    if (result != null)
      return result;

    HTTPMethod method = null;
    try {
      method = session.newMethodHead(location);
      int statusCode = method.execute();
      if (statusCode >= 300) {
        if (statusCode == 401)
          throw new IOException("Unauthorized to open dataset " + location);
        else
          throw new IOException(location + " is not a valid URL, return status=" + statusCode);
      }

      Header h = method.getResponseHeader("Content-Description");
      if ((h != null) && (h.getValue() != null)) {
        String v = h.getValue();
        if (v.equalsIgnoreCase("ncstream"))
          return ServiceType.CdmRemote;
      }

      return null;

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


   * @param urlString url as a String
   * @return contents of url as a String
   * @throws java.io.IOException on error
   */
  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

   * @param content   PUT this content at the given url.
   * @return the HTTP status return code
   * @throws java.io.IOException on error
   */
  public static int putContent(String urlString, String content) throws IOException {
    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);
        }
      }

      return resultCode;

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

  }

  //////////////////////

  static public String getUrlContentsAsString(HTTPSession session, String urlString, int maxKbytes) {
    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();
      return null;

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

    IO.copy(is, bout, 1000 * maxKbytes);
    return bout.toString(charset);
  }

  static public void copyUrlContentsToFile(HTTPSession session, String urlString, File file) throws HTTPException {
    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();

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

        useSession.close();
    }
  }

  static public long appendUrlContentsToFile(HTTPSession session, String urlString, File file, long start, long end) throws HTTPException {
    HTTPSession useSession = session;
    long nbytes = 0;

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

  private HTTPMethod getHttpResponse( URI uri )
          throws IOException, HTTPException
  {
    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 );
View Full Code Here

        temp = "http:" + temp;
    } catch (Exception e) {
    }
    remoteURI = temp;

    httpClient = new HTTPSession();

    // get the header
    HTTPMethod method = null;
    try {
      String url = remoteURI + "?req=header";
View Full Code Here

    }
  }

  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();
      throw ioe;
    }
  }
View Full Code Here

    this.url = url;
    location = url;
    if (debugLeaks)
      allFiles.add(location);

    session = new HTTPSession();

    boolean needtest = true;

    HTTPMethod method = null;
    try {
View Full Code Here

TOP

Related Classes of opendap.dap.http.HTTPSession

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.