예제 #1
0
 /** Tests http:send-request((),()). */
 @Test
 public void sendReqNoParams() {
   final Command cmd = new XQuery(_HTTP_SEND_REQUEST.args("()"));
   try {
     cmd.execute(ctx);
   } catch (final BaseXException ex) {
     assertTrue(ex.getMessage().contains(ErrType.HC.toString()));
   }
 }
예제 #2
0
 public void run() {
   try {
     InputStream in;
     OutputStream out;
     try {
       in = sk.getInputStream();
       out = sk.getOutputStream();
     } catch (IOException e) {
       throw (new RuntimeException(e));
     }
     while (true) {
       try {
         int len = Utils.int32d(read(in, 4), 0);
         if (!auth && (len > 256)) return;
         Message msg = new MessageBuf(read(in, len));
         String cmd = msg.string();
         Object[] args = msg.list();
         Object[] reply;
         if (auth) {
           Command cc = commands.get(cmd);
           if (cc != null) reply = cc.run(this, args);
           else reply = new Object[] {"nocmd"};
         } else {
           if (cmd.equals("nonce")) {
             reply = new Object[] {nonce};
           } else if (cmd.equals("auth")) {
             if (Arrays.equals((byte[]) args[0], ckey)) {
               reply = new Object[] {"ok"};
               auth = true;
             } else {
               reply = new Object[] {"no"};
             }
           } else {
             return;
           }
         }
         MessageBuf rb = new MessageBuf();
         rb.addlist(reply);
         byte[] rbuf = new byte[4 + rb.size()];
         Utils.uint32e(rb.size(), rbuf, 0);
         rb.fin(rbuf, 4);
         out.write(rbuf);
       } catch (IOException e) {
         return;
       }
     }
   } catch (InterruptedException e) {
   } finally {
     try {
       sk.close();
     } catch (IOException e) {
       throw (new RuntimeException(e));
     }
   }
 }
예제 #3
0
  public void run() {
    if (proxy == null || sock == null) return;

    Thread thisThread = Thread.currentThread();

    try {
      while (recvThread == thisThread) {
        Command cmd = new Command();
        if (cmd.recv(sock) == false) break;
        proxy.commandReceived(cmd);
      }
    } catch (Exception e) {
    }
  }
예제 #4
0
  /**
   * Instantiates a new MDC server.
   *
   * @param host the host
   * @param port the port
   */
  protected MDCServer(String host, int port) {
    _conf = Config.getConfig();

    address = (host == null) ? new InetSocketAddress(port) : new InetSocketAddress(host, port);

    /** initialize app command */
    Command.init();

    /** initialize the connection center */
    TConnCenter.init(_conf, port);

    synchronized (_conf) {
      /** load public key from database */
      TConn.pub_key = SystemConfig.s("pub_key", null);
      TConn.pri_key = SystemConfig.s("pri_key", null);

      /** initialize the RSA key, hardcode 2048 bits */
      if (TConn.pub_key == null
          || TConn.pri_key == null
          || "".equals(TConn.pub_key)
          || "".equals(TConn.pri_key)) {
        /** print out the old state */
        log.warn(
            "the pub_key or pri_key missed, the old state are pub_key:["
                + TConn.pub_key
                + "], pri_key:["
                + TConn.pri_key
                + "]");

        Key k = RSA.generate(2048);
        TConn.pri_key = k.pri_key;
        TConn.pub_key = k.pub_key;

        /** print out the new public key */
        log.warn("create new RSA key pair, pub_key:[" + TConn.pub_key + ']');

        /** set back in database */
        SystemConfig.setConfig("pri_key", TConn.pri_key);
        SystemConfig.setConfig("pub_key", TConn.pub_key);
      }

      MAX_SIZE = SystemConfig.i("mdc.max_size", MAX_SIZE);
    }
  }
예제 #5
0
 @Override
 protected void execute(final Command cmd, final OutputStream os) throws IOException {
   execute(cmd.toString(), os);
 }
예제 #6
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();
    }
  }