Package org.structr.rest.resource

Examples of org.structr.rest.resource.Resource


  protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws UnsupportedEncodingException {

    SecurityContext securityContext = null;
    Authenticator authenticator = null;
    Result result = null;
    Resource resource = null;

    try {

      // isolate request authentication in a transaction
      try (final Tx tx = StructrApp.getInstance().tx()) {
        authenticator = config.getAuthenticator();
        securityContext = authenticator.initializeAndExamineRequest(request, response);
        tx.success();
      }
      final App app = StructrApp.getInstance(securityContext);

//                      logRequest("GET", request);
      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");
      response.setContentType("text/csv; charset=utf-8");

      // set default value for property view
      propertyView.set(securityContext, defaultPropertyView);

      // evaluate constraints and measure query time
      double queryTimeStart = System.nanoTime();

      // isolate resource authentication
      try (final Tx tx = app.tx()) {

        resource = ResourceHelper.optimizeNestedResourceChain(ResourceHelper.parsePath(securityContext, request, resourceMap, propertyView, defaultIdProperty), defaultIdProperty);
        authenticator.checkResourceAccess(request, resource.getResourceSignature(), propertyView.get(securityContext));

        tx.success();
      }

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

        String resourceSignature = resource.getResourceSignature();

        // let authenticator examine request again
        authenticator.checkResourceAccess(request, resourceSignature, propertyView.get(securityContext));

        // 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 = HttpService.parseInt(pageSizeParameter, NodeFactory.DEFAULT_PAGE_SIZE);
        int page = HttpService.parseInt(pageParameter, NodeFactory.DEFAULT_PAGE);
        PropertyKey sortKey = null;

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

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

          sortKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, sortKeyName);

        }

        // Should line breaks be removed?
        removeLineBreaks = StringUtils.equals(request.getParameter(REMOVE_LINE_BREAK_PARAM), "1");

        // Should a leading BOM be written?
        writeBom = StringUtils.equals(request.getParameter(WRITE_BOM), "1");

        // do action
        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);

      // Services.removeAttribute(NodeFactory.RAW_RESULT_COUNT + Thread.currentThread().getId());
        // timing..
        double queryTimeEnd = System.nanoTime();

        // commit response
        if (result != null) {

          // store property view that will be used to render the results
          result.setPropertyView(propertyView.get(securityContext));

          // allow resource to modify result set
          resource.postProcessResultSet(result);

          DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH));

          result.setQueryTime(decimalFormat.format((queryTimeEnd - queryTimeStart) / 1000000000.0));
View Full Code Here

TOP

Related Classes of org.structr.rest.resource.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.