Пример #1
0
 public static void main(String args[]) throws Exception {
   int nconstraints = DFALTNCONSTRAINTS;
   if (args.length > 0) {
     try {
       int n = Integer.parseInt(args[0]);
       if (n > 0) nconstraints = n;
     } catch (NumberFormatException nfe) {
       System.err.println("GenerateConstraints: non-int argument");
     }
   }
   try {
     new GenerateConstraints("GenerateConstraints").generate(nconstraints);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #2
0
  /**
   * Open a connection to the DODS server.
   *
   * @param urlString the URL to open.
   * @param command execute this command on the input stream
   * @throws IOException if an IO exception occurred.
   * @throws DAP2Exception if the DODS server returned an error.
   * @throws ParseException is cant parse the return
   */
  private void openConnection(String urlString, Command command)
      throws IOException, DAP2Exception, ParseException {
    HTTPMethod method = null;
    InputStream is = null;

    initSession();

    try {

      method = _session.newMethodGet(urlString);

      if (acceptCompress) method.setRequestHeader("Accept-Encoding", "deflate,gzip");

      // enable sessions
      if (allowSessions) method.setRequestHeader("X-Accept-Session", "true");

      // Execute the method.
      int statusCode = method.execute();

      // debug
      // if (debugHeaders) ucar.nc2.util.net.HttpClientManager.showHttpRequestInfo(f, method);

      if (statusCode == HTTPSession.SC_NOT_FOUND) {
        throw new DAP2Exception(DAP2Exception.NO_SUCH_FILE, method.getStatusText());
      }

      if (statusCode == HTTPSession.SC_UNAUTHORIZED) {
        throw new InvalidCredentialsException(method.getStatusText());
      }

      if (statusCode != HTTPSession.SC_OK) {
        throw new DAP2Exception(
            "Method failed:" + method.getStatusText() + " on URL= " + urlString);
      }

      // Get the response body.
      is = method.getResponseAsStream();

      // check if its an error
      Header header = method.getResponseHeader("Content-Description");
      if (header != null
          && (header.getValue().equals("dods-error") || header.getValue().equals("dods_error"))) {
        // create server exception object
        DAP2Exception ds = new DAP2Exception();
        is = dumpStream(is);
        // parse the Error object from stream and throw it
        ds.parse(is);
        throw ds;
      }

      ver = new ServerVersion(method);

      checkHeaders(method);

      // check for deflator
      Header h = method.getResponseHeader("content-encoding");
      String encoding = (h == null) ? null : h.getValue();
      // if (encoding != null) System.out.println("encoding= " + encoding);

      if (encoding != null && encoding.equals("deflate")) {
        is = new BufferedInputStream(new InflaterInputStream(is), 1000);

      } else if (encoding != null && encoding.equals("gzip")) {
        is = new BufferedInputStream(new GZIPInputStream(is), 1000);
      }

      command.process(is);

    } catch (Exception e) {
      e.printStackTrace();
      throw new DAP2Exception(e);

    } finally {
      // Release the connection.
      if (method != null) method.close();
    }
  }