Exemple #1
0
  protected void processRequest(HttpServletRequest req, HttpServletResponse res, HTTPMethods method)
      throws ServletException, IOException {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Origin", "*");

    String pathInfo = req.getPathInfo();

    // Collect all parameter
    Map<String, String> params = new HashMap<>();

    Map names = req.getParameterMap();
    if (names != null) {
      for (Object key : names.keySet()) {
        params.put(
            URLDecoder.decode((String) key, "UTF-8"),
            URLDecoder.decode(req.getParameter((String) key), "UTF-8"));
      }
    }

    // default template is json
    String templateKey = "json";

    if (params.containsKey(Helper.Keys.OutputParam)) {
      String tempKey = params.get(Helper.Keys.OutputParam);
      if (tempKey != null && !tempKey.equals("")) {
        templateKey = tempKey;
      }
    }

    TemplateResult result = null;
    try {
      if (pathInfo != null && !pathInfo.equals("")) {
        // request: /{recommender-id}
        pathInfo = pathInfo.substring(1);
        String[] split = pathInfo.split("/");

        if (split.length == 1) {
          params.put(Helper.Keys.RecommenderId, split[0]);

          result =
              handler.process(
                  RecommenderController.RecommenderRequestAction.Recommend, templateKey, params);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (result == null) {
      //   out.println("TODO print error");
    } else {
      res.setContentType(result.getContentType());
      res.setCharacterEncoding(result.getCharset().name());
      res.setStatus(result.getStatus());
      PrintWriter out = res.getWriter();
      Helper.copy(out, result.getResult());
    }
  }
  private TemplateResult getItems(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    Item[] items = dataSource.getItems();

    items = Helper.applyPaging(items, param);

    return template.transform(items);
  }
  private TemplateResult getInteraction(
      DataAccess access, DataTemplate template, Map<String, String> param) throws BaseException {
    DataSource dataSource = getDataSource(access, param.get(Helper.Keys.SourceId));
    if (dataSource.getState() != DataInformation.DataState.READY)
      throw new NotReadyException("The datasource %s is not yet ready.", dataSource.getId());

    String itemId = param.get(Helper.Keys.ItemId);
    String userId = param.get(Helper.Keys.UserId);

    Interaction[] interaction = dataSource.getInteractions(itemId, userId);

    interaction = Helper.applyPaging(interaction, param);

    return template.transform(interaction);
  }