@Override
  public void processMessage(final WebSocketMessage webSocketData) {

    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();

    final App app = StructrApp.getInstance(securityContext);

    try (final Tx tx = app.tx()) {

      // do search
      List<AbstractNode> filteredResults = getUnattachedNodes(app, securityContext, webSocketData);

      // save raw result count
      int resultCountBeforePaging = filteredResults.size();

      // set full result list
      webSocketData.setResult(PagingHelper.subList(filteredResults, pageSize, page, null));
      webSocketData.setRawResultCount(resultCountBeforePaging);

      // send only over local connection
      getWebSocket().send(webSocketData, true);

      tx.success();

    } catch (FrameworkException fex) {

      logger.warn("Exception occured", fex);
      getWebSocket()
          .send(
              MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(),
              true);
    }
  }
Пример #2
0
  @Override
  public List<GraphObject> getData(
      final SecurityContext securityContext,
      final RenderContext renderContext,
      final String restQuery)
      throws FrameworkException {

    Map<Pattern, Class<? extends Resource>> resourceMap = new LinkedHashMap<>();

    ResourceProvider resourceProvider =
        renderContext == null ? null : renderContext.getResourceProvider();
    if (resourceProvider == null) {
      try {
        resourceProvider = UiResourceProvider.class.newInstance();
      } catch (Throwable t) {
        logger.log(Level.SEVERE, "Couldn't establish a resource provider", t);
        return Collections.EMPTY_LIST;
      }
    }

    // inject resources
    resourceMap.putAll(resourceProvider.getResources());

    Value<String> propertyView = new ThreadLocalPropertyView();
    propertyView.set(securityContext, PropertyView.Ui);

    // initialize variables
    // mimic HTTP request
    HttpServletRequest request =
        new HttpServletRequestWrapper(
            renderContext == null ? securityContext.getRequest() : renderContext.getRequest()) {

          @Override
          public Enumeration<String> getParameterNames() {
            return new IteratorEnumeration(getParameterMap().keySet().iterator());
          }

          @Override
          public String getParameter(String key) {
            String[] p = getParameterMap().get(key);
            return p != null ? p[0] : null;
          }

          @Override
          public Map<String, String[]> getParameterMap() {
            String[] parts = StringUtils.split(getQueryString(), "&");
            Map<String, String[]> parameterMap = new HashMap();
            for (String p : parts) {
              String[] kv = StringUtils.split(p, "=");
              if (kv.length > 1) {
                parameterMap.put(kv[0], new String[] {kv[1]});
              }
            }
            return parameterMap;
          }

          @Override
          public String getQueryString() {
            return StringUtils.substringAfter(restQuery, "?");
          }

          @Override
          public String getPathInfo() {
            return StringUtils.substringBefore(restQuery, "?");
          }

          @Override
          public StringBuffer getRequestURL() {
            return new StringBuffer(restQuery);
          }
        };

    // update request in security context
    securityContext.setRequest(request);

    // HttpServletResponse response = renderContext.getResponse();
    Resource resource =
        ResourceHelper.applyViewTransformation(
            request,
            securityContext,
            ResourceHelper.optimizeNestedResourceChain(
                ResourceHelper.parsePath(
                    securityContext, request, resourceMap, propertyView, GraphObject.id),
                GraphObject.id),
            propertyView);

    // TODO: decide if we need to rest the REST request here
    // securityContext.checkResourceAccess(request, resource.getResourceSignature(),
    // resource.getGrant(request, response), PropertyView.Ui);
    // add sorting & paging
    String pageSizeParameter = request.getParameter(JsonRestServlet.REQUEST_PARAMETER_PAGE_SIZE);
    String pageParameter = request.getParameter(JsonRestServlet.REQUEST_PARAMETER_PAGE_NUMBER);
    String offsetId = request.getParameter(JsonRestServlet.REQUEST_PARAMETER_OFFSET_ID);
    String sortOrder = request.getParameter(JsonRestServlet.REQUEST_PARAMETER_SORT_ORDER);
    String sortKeyName = request.getParameter(JsonRestServlet.REQUEST_PARAMETER_SORT_KEY);
    boolean sortDescending = (sortOrder != null && "desc".equals(sortOrder.toLowerCase()));
    int pageSize = parseInt(pageSizeParameter, NodeFactory.DEFAULT_PAGE_SIZE);
    int page = parseInt(pageParameter, NodeFactory.DEFAULT_PAGE);
    PropertyKey sortKey = null;

    // set sort key
    if (sortKeyName != null) {

      Class<? extends GraphObject> type = resource.getEntityClass();
      if (type == null) {

        // fallback to default implementation
        // if no type can be determined
        type = AbstractNode.class;
      }
      sortKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, sortKeyName);
    }

    // do action
    Result result = resource.doGet(sortKey, sortDescending, pageSize, page, offsetId);
    result.setIsCollection(resource.isCollectionResource());
    result.setIsPrimitiveArray(resource.isPrimitiveArray());

    // Integer rawResultCount = (Integer) Services.getAttribute(NodeFactory.RAW_RESULT_COUNT +
    // Thread.currentThread().getId());
    PagingHelper.addPagingParameter(result, pageSize, page);

    List<GraphObject> res = result.getResults();

    if (renderContext != null) {
      renderContext.setResult(result);
    }

    return res != null ? res : Collections.EMPTY_LIST;
  }