private void processKey(SelectionKey key) {

    try {

      if (key.isReadable()) {
        final DatagramChannel socketChannel = (DatagramChannel) key.channel();
        reader.readAll(socketChannel);
      }

      if (key.isWritable()) {
        final DatagramChannel socketChannel = (DatagramChannel) key.channel();
        try {
          int bytesJustWritten = writer.writeAll(socketChannel);
          contemplateThrottleWrites(bytesJustWritten);
        } catch (NotYetConnectedException e) {
          if (LOG.isDebugEnabled()) LOG.debug("", e);
          serverConnector.connectLater();
        } catch (IOException e) {
          if (LOG.isDebugEnabled()) LOG.debug("", e);
          serverConnector.connectLater();
        }
      }
    } catch (Exception e) {
      LOG.error("", e);
      if (!isClosed) closeEarlyAndQuietly(key.channel());
    }
  }
Example #2
0
 private static String SendToken() throws IOException {
   ServerConnector sc = new ServerConnector("user");
   HashMap<String, String> tokenMap = new HashMap<String, String>();
   tokenMap.put("access_token", access_token);
   tokenMap.put("expires_in", expires_in);
   sc.setRequestParam("userID", Login.userID);
   sc.setRequestParam("sinaToken", JSONHelper.getJSONHelperInstance().convertToString(tokenMap));
   String result = sc.doPost();
   return result;
 }
  public static void main(String[] args) throws Exception {
    showClassesInCLASSPATH();
    MUtil.setJettySystemProperties();
    File log4jPropertiesFile = new File("./m-common/src/assembly/properties/log4j.properties");
    if (!log4jPropertiesFile.exists()) {
      log4jPropertiesFile = new File("./../m-common/src/assembly/properties/log4j.properties");
    }
    logger.info(
        String.format("loading log4j.properties from %s", log4jPropertiesFile.getAbsolutePath()));
    System.setProperty("log4j.configuration", "file:" + log4jPropertiesFile.getAbsolutePath());

    Server server = new Server(MProperties.INSTANCE.getCmJettyPort());

    // Setup HTTP Configuration
    HttpConfiguration httpConf = new HttpConfiguration();
    httpConf.setSecurePort(MProperties.INSTANCE.getCmJettyPort());
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);

    // Restlet servlet
    ServletHolder servletHolder = new ServletHolder(new ServerServlet());
    servletHolder.setName("MultipolyApplication");
    servletHolder.setInitParameter(
        "org.restlet.application",
        MRestletApplication.class.getName()); // This makes sure that the class is loaded!!!
    servletHolder.setInitParameter("org.restlet.clients", "HTTP FILE CLAP");
    context.addServlet(servletHolder, "/m/*");

    // Websocket servletpieter

    // I should come back to this
    /*        ServletHolder websocketServletHolder = new ServletHolder(new NotificationWebsocketServlet());
    websocketServletHolder.setName("Umlg WebSocket Servlet");
    context.addServlet(websocketServletHolder, "/m/broadcastWebsocket");*/

    ContextHandlerCollection contextHandlers = new ContextHandlerCollection();
    contextHandlers.setHandlers(new Handler[] {context});

    ServerConnector serverConnector =
        new ServerConnector(server, new HttpConnectionFactory(httpConf)); // <-- use it!
    serverConnector.setPort(MProperties.INSTANCE.getCmJettyPort());

    server.setConnectors(new Connector[] {serverConnector});
    server.setHandler(contextHandlers);
    setUpStart();
    server.start();
    server.join();
  }
 @Before
 public void startServer() throws Exception {
   server = new Server();
   connector = new ServerConnector(server);
   connector.setIdleTimeout(10000);
   server.addConnector(connector);
 }
  @Test
  public void testPartialReadThenClose() throws Exception {
    server.setHandler(new PartialReaderHandler());
    server.start();

    try (final Socket socket = new Socket("localhost", connector.getLocalPort())) {
      socket.setSoTimeout(1000);

      byte[] content = new byte[32 * 4096];
      Arrays.fill(content, (byte) 88);

      OutputStream out = socket.getOutputStream();
      String header =
          "POST /?read=10 HTTP/1.1\r\n"
              + "Host: localhost\r\n"
              + "Content-Length: "
              + content.length
              + "\r\n"
              + "Content-Type: bytes\r\n"
              + "\r\n";
      byte[] h = header.getBytes(StandardCharsets.ISO_8859_1);
      out.write(h);
      out.write(content, 0, 4096);
      out.flush();

      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      assertThat(in.readLine(), containsString("HTTP/1.1 200 OK"));
      assertThat(in.readLine(), containsString("Content-Length:"));
      assertThat(in.readLine(), containsString("Server:"));
      in.readLine();
      assertThat(in.readLine(), containsString("XXXXXXX"));

      socket.close();
    }
  }
  public void startServer(Handler handler) throws Exception {
    server = new Server();
    connector =
        new ServerConnector(
            server,
            new HttpConnectionFactory() {
              @Override
              public Connection newConnection(Connector connector, EndPoint endPoint) {
                return configure(
                    new HttpConnection(new HttpConfiguration(), connector, endPoint) {
                      @Override
                      public void onFillable() {
                        handles.incrementAndGet();
                        super.onFillable();
                      }
                    },
                    connector,
                    endPoint);
              }
            });

    server.addConnector(connector);
    connector.setPort(0);
    server.setHandler(handler);
    server.start();
  }
 @Override
 public void onTerminated(final ServerConnector<BluetoothSocketConnection> pServerConnector) {
   Debug.d(
       "Closed Server-Connection from: '"
           + pServerConnector
               .getConnection()
               .getBluetoothSocket()
               .getRemoteDevice()
               .getAddress());
 }
Example #8
0
  public static boolean unBound() {

    if (isBound()) {

      ServerConnector sc = new ServerConnector("https://api.weibo.com/oauth2", "/revokeoauth2");
      sc.setRequestParam("access_token", access_token);
      try {
        String result = sc.doPost();
        Log.d("revoke", "result");
      } catch (IOException e) {
        // accessToken = null;
        Log.d("revoke exception", e.getMessage());
        e.printStackTrace();
      }

      AccountAPI accountAPI = new AccountAPI(accessToken);
      accountAPI.endSession(
          new RequestListener() {
            @Override
            public void onComplete(String s) {
              clearToken();
            }

            @Override
            public void onIOException(IOException e) {
              clearToken();
            }

            @Override
            public void onError(WeiboException e) {
              clearToken();
            }
          });
      clearToken();
    }

    clearToken();
    return true;
  }
  @Test
  public void testPipelined() throws Exception {
    server.setHandler(new AsyncStreamHandler());
    server.start();

    try (final Socket socket = new Socket("localhost", connector.getLocalPort())) {
      socket.setSoTimeout(1000);

      byte[] content = new byte[32 * 4096];
      Arrays.fill(content, (byte) 120);

      OutputStream out = socket.getOutputStream();
      String header =
          "POST / HTTP/1.1\r\n"
              + "Host: localhost\r\n"
              + "Content-Length: "
              + content.length
              + "\r\n"
              + "Content-Type: bytes\r\n"
              + "\r\n";
      byte[] h = header.getBytes(StandardCharsets.ISO_8859_1);
      out.write(h);
      out.write(content);

      header =
          "POST / HTTP/1.1\r\n"
              + "Host: localhost\r\n"
              + "Content-Length: "
              + content.length
              + "\r\n"
              + "Content-Type: bytes\r\n"
              + "Connection: close\r\n"
              + "\r\n";
      h = header.getBytes(StandardCharsets.ISO_8859_1);
      out.write(h);
      out.write(content);
      out.flush();

      InputStream in = socket.getInputStream();
      String response = IO.toString(in);
      assertTrue(response.indexOf("200 OK") > 0);

      long total = __total.poll(5, TimeUnit.SECONDS);
      assertEquals(content.length, total);
      total = __total.poll(5, TimeUnit.SECONDS);
      assertEquals(content.length, total);
    }
  }
  public void asyncReadTest(int contentSize, int chunkSize, int chunks, int delayMS)
      throws Exception {
    String tst = contentSize + "," + chunkSize + "," + chunks + "," + delayMS;
    // System.err.println(tst);

    try (final Socket socket = new Socket("localhost", connector.getLocalPort())) {

      byte[] content = new byte[contentSize];
      Arrays.fill(content, (byte) 120);

      OutputStream out = socket.getOutputStream();
      out.write("POST / HTTP/1.1\r\n".getBytes());
      out.write("Host: localhost\r\n".getBytes());
      out.write(("Content-Length: " + content.length + "\r\n").getBytes());
      out.write("Content-Type: bytes\r\n".getBytes());
      out.write("Connection: close\r\n".getBytes());
      out.write("\r\n".getBytes());
      out.flush();

      int offset = 0;
      for (int i = 0; i < chunks; i++) {
        out.write(content, offset, chunkSize);
        offset += chunkSize;
        Thread.sleep(delayMS);
      }
      out.write(content, offset, content.length - offset);

      out.flush();

      InputStream in = socket.getInputStream();
      String response = IO.toString(in);
      assertTrue(tst, response.indexOf("200 OK") > 0);

      long total = __total.poll(30, TimeUnit.SECONDS);
      assertEquals(tst, content.length, total);
    }
  }
  /** binds to the server socket and process data This method will block until interrupted */
  @Override
  void processEvent() throws IOException {
    try {

      connectClient().register(selector, OP_READ);
      serverConnector.connectLater();

      while (selector.isOpen()) {
        registerPendingRegistrations();

        // this may block for a long time, upon return the
        // selected set contains keys of the ready channels
        final int n = selector.select(100);

        if (shouldEnableOpWrite) enableWrites();

        checkThrottleInterval();

        if (n == 0) {
          continue; // nothing to do
        }

        if (useJavaNIOSelectionKeys) {
          // use the standard java nio selector

          final Set<SelectionKey> selectionKeys = selector.selectedKeys();
          for (final SelectionKey key : selectionKeys) {
            processKey(key);
          }
          selectionKeys.clear();
        } else {
          // use the netty like selector

          final SelectionKey[] keys = selectedKeys.flip();

          try {
            for (int i = 0; i < keys.length && keys[i] != null; i++) {
              final SelectionKey key = keys[i];

              try {
                processKey(key);
              } catch (BufferUnderflowException e) {
                if (!isClosed) LOG.error("", e);
              }
            }
          } finally {
            for (int i = 0; i < keys.length && keys[i] != null; i++) {
              keys[i] = null;
            }
          }
        }
      }
    } catch (CancelledKeyException
        | ConnectException
        | ClosedChannelException
        | ClosedSelectorException e) {
      if (LOG.isDebugEnabled()) LOG.debug("", e);
    } catch (Exception e) {
      LOG.error("", e);
    } catch (Throwable e) {
      LOG.error("", e);
      throw e;
    } finally {

      if (LOG.isDebugEnabled()) LOG.debug("closing name=" + this.name);
      if (!isClosed) {
        closeResources();
      }
    }
  }
  /**
   * Loads jetty configuration from the given URL.
   *
   * @param cfgUrl URL to load configuration from.
   * @throws GridException if load failed.
   */
  private void loadJettyConfiguration(@Nullable URL cfgUrl) throws GridException {
    if (cfgUrl == null) {
      HttpConfiguration httpCfg = new HttpConfiguration();

      httpCfg.setSecureScheme("https");
      httpCfg.setSecurePort(8443);
      httpCfg.setSendServerVersion(true);
      httpCfg.setSendDateHeader(true);

      String srvPortStr = System.getProperty(GG_JETTY_PORT, "8080");

      int srvPort;

      try {
        srvPort = Integer.valueOf(srvPortStr);
      } catch (NumberFormatException ignore) {
        throw new GridException(
            "Failed to start Jetty server because GRIDGAIN_JETTY_PORT system property "
                + "cannot be cast to integer: "
                + srvPortStr);
      }

      httpSrv = new Server(new QueuedThreadPool(20, 200));

      ServerConnector srvConn = new ServerConnector(httpSrv, new HttpConnectionFactory(httpCfg));

      srvConn.setHost(System.getProperty(GG_JETTY_HOST, "localhost"));
      srvConn.setPort(srvPort);
      srvConn.setIdleTimeout(30000L);
      srvConn.setReuseAddress(true);

      httpSrv.addConnector(srvConn);

      httpSrv.setStopAtShutdown(false);
    } else {
      XmlConfiguration cfg;

      try {
        cfg = new XmlConfiguration(cfgUrl);
      } catch (FileNotFoundException e) {
        throw new GridSpiException("Failed to find configuration file: " + cfgUrl, e);
      } catch (SAXException e) {
        throw new GridSpiException("Failed to parse configuration file: " + cfgUrl, e);
      } catch (IOException e) {
        throw new GridSpiException("Failed to load configuration file: " + cfgUrl, e);
      } catch (Exception e) {
        throw new GridSpiException(
            "Failed to start HTTP server with configuration file: " + cfgUrl, e);
      }

      try {
        httpSrv = (Server) cfg.configure();
      } catch (Exception e) {
        throw new GridException("Failed to start Jetty HTTP server.", e);
      }
    }

    assert httpSrv != null;

    httpSrv.setHandler(jettyHnd);

    override(getJettyConnector());
  }
  @Test
  public void testSlowClientWithPipelinedRequest() throws Exception {
    final int contentLength = 512 * 1024;
    startServer(
        new AbstractHandler() {
          @Override
          public void handle(
              String target,
              Request baseRequest,
              HttpServletRequest request,
              HttpServletResponse response)
              throws IOException, ServletException {
            baseRequest.setHandled(true);
            if ("/content".equals(target)) {
              // We simulate what the DefaultServlet does, bypassing the blocking
              // write mechanism otherwise the test does not reproduce the bug
              OutputStream outputStream = response.getOutputStream();
              HttpOutput output = (HttpOutput) outputStream;
              // Since the test is via localhost, we need a really big buffer to stall the write
              byte[] bytes = new byte[contentLength];
              Arrays.fill(bytes, (byte) '9');
              ByteBuffer buffer = ByteBuffer.wrap(bytes);
              // Do a non blocking write
              output.sendContent(buffer);
            }
          }
        });

    Socket client = new Socket("localhost", connector.getLocalPort());
    OutputStream output = client.getOutputStream();
    output.write(
        (""
                + "GET /content HTTP/1.1\r\n"
                + "Host: localhost:"
                + connector.getLocalPort()
                + "\r\n"
                + "\r\n"
                + "")
            .getBytes(StandardCharsets.UTF_8));
    output.flush();

    InputStream input = client.getInputStream();

    int read = input.read();
    Assert.assertTrue(read >= 0);
    // As soon as we can read the response, send a pipelined request
    // so it is a different read for the server and it will trigger NIO
    output.write(
        (""
                + "GET /pipelined HTTP/1.1\r\n"
                + "Host: localhost:"
                + connector.getLocalPort()
                + "\r\n"
                + "\r\n"
                + "")
            .getBytes(StandardCharsets.UTF_8));
    output.flush();

    // Simulate a slow reader
    Thread.sleep(1000);
    Assert.assertThat(handles.get(), lessThan(10));

    // We are sure we are not spinning, read the content
    StringBuilder lines = new StringBuilder().append((char) read);
    int crlfs = 0;
    while (true) {
      read = input.read();
      lines.append((char) read);
      if (read == '\r' || read == '\n') ++crlfs;
      else crlfs = 0;
      if (crlfs == 4) break;
    }
    Assert.assertTrue(lines.toString().contains(" 200 "));
    // Read the body
    for (int i = 0; i < contentLength; ++i) input.read();

    // Read the pipelined response
    lines.setLength(0);
    crlfs = 0;
    while (true) {
      read = input.read();
      lines.append((char) read);
      if (read == '\r' || read == '\n') ++crlfs;
      else crlfs = 0;
      if (crlfs == 4) break;
    }
    Assert.assertTrue(lines.toString().contains(" 200 "));

    client.close();
  }