public WebSocketSession(URI requestURI, EventDriver websocket, LogicalConnection connection) {
    if (requestURI == null) {
      throw new RuntimeException("Request URI cannot be null");
    }

    this.requestURI = requestURI;
    this.websocket = websocket;
    this.connection = connection;
    this.outgoingHandler = connection;
    this.incomingHandler = websocket;

    // Get the parameter map (use the jetty MultiMap to do this right)
    MultiMap<String> params = new MultiMap<>();
    String query = requestURI.getQuery();
    if (StringUtil.isNotBlank(query)) {
      UrlEncoded.decodeTo(query, params, StringUtil.__UTF8);
    }

    for (String name : params.keySet()) {
      List<String> valueList = params.getValues(name);
      String valueArr[] = new String[valueList.size()];
      valueArr = valueList.toArray(valueArr);
      parameterMap.put(name, valueArr);
    }
  }
Example #2
0
 @Override
 public CottyRequest onRequest() {
   JSONObject jsonObject = new JSONObject();
   jsonObject.put("vfwebqq", this.cottySession.getStringAttribute("vfwebqq"));
   jsonObject.put(
       "hash",
       FriendHashUtil.hash(
           this.cottySession.getLongAttribute("uin"),
           this.cottySession.getStringAttribute("ptwebqq")));
   String content = UrlEncoded.encodeString(jsonObject.toJSONString());
   CottyRequest cottyRequest =
       new CottyRequest("http://s.web2.qq.com/api/get_user_friends2")
           .setMethod("POST")
           .addContent("r", content);
   return cottyRequest;
 }
  /*
   * @see javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
   */
  @Override
  public void include(ServletRequest request, ServletResponse response)
      throws ServletException, IOException {
    Request baseRequest =
        (request instanceof Request)
            ? ((Request) request)
            : HttpChannel.getCurrentHttpChannel().getRequest();

    if (!(request instanceof HttpServletRequest)) request = new ServletRequestHttpWrapper(request);
    if (!(response instanceof HttpServletResponse))
      response = new ServletResponseHttpWrapper(response);

    final DispatcherType old_type = baseRequest.getDispatcherType();
    final Attributes old_attr = baseRequest.getAttributes();
    MultiMap<String> old_params = baseRequest.getParameters();
    try {
      baseRequest.setDispatcherType(DispatcherType.INCLUDE);
      baseRequest.getResponse().include();
      if (_named != null)
        _contextHandler.handle(
            _named, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
      else {
        String query = _dQuery;

        if (query != null) {
          // force parameter extraction
          if (old_params == null) {
            baseRequest.extractParameters();
            old_params = baseRequest.getParameters();
          }

          MultiMap<String> parameters = new MultiMap<>();
          UrlEncoded.decodeTo(query, parameters, baseRequest.getCharacterEncoding(), -1);

          if (old_params != null) {
            // Merge parameters.
            parameters.addAllValues(old_params);
          }
          baseRequest.setParameters(parameters);
        }

        IncludeAttributes attr = new IncludeAttributes(old_attr);

        attr._requestURI = _uri;
        attr._contextPath = _contextHandler.getContextPath();
        attr._servletPath = null; // set by ServletHandler
        attr._pathInfo = _path;
        attr._query = query;

        baseRequest.setAttributes(attr);

        _contextHandler.handle(
            _path, baseRequest, (HttpServletRequest) request, (HttpServletResponse) response);
      }
    } finally {
      baseRequest.setAttributes(old_attr);
      baseRequest.getResponse().included();
      baseRequest.setParameters(old_params);
      baseRequest.setDispatcherType(old_type);
    }
  }
  public static void main(String[] args) {

    // serve static files from /www in resources
    staticFileLocation("/www");

    // setup the database model
    Sql2o sql2o = new Sql2o("jdbc:h2:~/test", "sa", "");
    Sql2oModel model = new Sql2oModel(sql2o);

    // url /hello
    // simple response return
    get("/hello", (req, res) -> "Hello Spark!");

    // url /test-view
    // simple template "test-view.ftl"
    get(
        "/test-view",
        (req, res) -> {
          Map<String, Object> viewModel = new HashMap<>();
          viewModel.put("message", "Hello Spark!");

          return new ModelAndView(viewModel, "test-view.ftl");
        },
        new FreeMarkerEngine());

    // url /books
    // data bound template
    get(
        "/books",
        (req, res) -> {
          Map<String, Object> viewModel = new HashMap<>();
          viewModel.put("message", "Books");

          List<Book> books = model.getBooks();
          viewModel.put("books", books);
          return new ModelAndView(viewModel, "books.ftl");
        },
        new FreeMarkerEngine());

    // url /books/new
    // initial get of input form for new book in database
    get(
        "/books/new",
        (req, res) -> {
          Map<String, Object> viewModel = new HashMap<>();
          viewModel.put("message", "Add Book");
          return new ModelAndView(viewModel, "new-book.ftl");
        },
        new FreeMarkerEngine());

    // url /books/new
    // subsequent post of new book data back to the
    // server to get saved back to the database
    post(
        "/books/new",
        (req, res) -> {
          // todo: add response book to database, use custom viewModel class
          MultiMap<String> params = new MultiMap<String>();
          UrlEncoded.decodeTo(req.body(), params, "UTF-8");
          NewBookEditModel editModel = new NewBookEditModel();
          BeanUtils.populate(editModel, params);

          long id =
              model.createBook(
                  editModel.getTitle(), editModel.getAuthor(), editModel.getImageUrl());
          res.redirect("/books");
          halt();
          return id; // ?? json & rest??
        });

    get(
        "/books/delete/:id",
        (req, res) -> {
          int id = Integer.parseInt(req.params().get(":id"));
          model.deleteBook(id);

          res.redirect("/books");
          halt();

          return null;
        });

    // url /spi-test
    // json testbed page "spi-test.ftl"
    get(
        "/spi-test",
        (req, res) -> {
          Map<String, Object> viewModel = new HashMap<>();
          viewModel.put("message", "SPI Test Page");

          List<Book> books = model.getBooks();
          viewModel.put("books", books);

          return new ModelAndView(viewModel, "spi-test.ftl");
        },
        new FreeMarkerEngine());

    // Additional JSON Services?
  }