Ejemplo n.º 1
0
 /**
  * Gets the router from url.
  *
  * @param url
  * @return
  * @throws RouterConfigException
  * @throws IOException
  * @since 1.2.2
  */
 private Router getRouter(URL url) throws RouterConfigException, IOException {
   InputStream in = url.openStream();
   try {
     ControllerDescriptor routerDesc = new DescriptorBuilder().build(in);
     return new Router(routerDesc);
   } finally {
     Safe.close(in);
   }
 }
  public UserNodeFilterConfig(Builder builder) {
    if (builder == null) {
      throw new NullPointerException();
    }

    //
    this.visibility = Safe.unmodifiableSet(builder.withVisibility);
    this.authorizationMode = builder.withAuthorizationMode;
    this.temporalCheck = builder.withTemporalCheck;
    this.path = builder.path;
  }
    public void execute(Event<UIPortlet<S, C>> event) throws Exception {
      UIPortlet<S, C> uiPortlet = event.getSource();
      log.trace("Serve Resource for portlet: " + uiPortlet.getPortletContext());
      String resourceId = null;

      //
      PortalRequestContext context = (PortalRequestContext) event.getRequestContext();
      HttpServletResponse response = context.getResponse();

      //
      try {
        // Set the NavigationalState
        String navState =
            context.getRequestParameter(ExoPortletInvocationContext.NAVIGATIONAL_STATE_PARAM_NAME);
        if (navState != null) {
          uiPortlet.setNavigationalState(ParametersStateString.create(navState));
        }

        //
        ResourceInvocation resourceInvocation = uiPortlet.create(ResourceInvocation.class, context);

        // set the resourceId to be used in case of a problem
        resourceId = resourceInvocation.getResourceId();

        //
        PortletInvocationResponse portletResponse = uiPortlet.invoke(resourceInvocation);

        //
        int statusCode;
        MultiValuedPropertyMap<String> transportHeaders;
        String contentType;
        String charset;
        Object content;
        if (!(portletResponse instanceof ContentResponse)) {
          if (portletResponse instanceof ErrorResponse) {
            ErrorResponse errorResponse = (ErrorResponse) portletResponse;
            Throwable cause = errorResponse.getCause();
            if (cause != null) {
              log.trace("Got error response from portlet", cause);
            } else if (errorResponse.getMessage() != null) {
              log.trace("Got error response from portlet:" + errorResponse.getMessage());
            } else {
              log.trace("Got error response from portlet");
            }
          } else {
            log.trace(
                "Unexpected response type ["
                    + portletResponse
                    + "]. Expected a ContentResponse or an ErrorResponse.");
          }
          statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
          contentType = null;
          charset = null;
          transportHeaders = null;
          content = null;
        } else {
          //
          ContentResponse piResponse = (ContentResponse) portletResponse;
          ResponseProperties properties = piResponse.getProperties();
          transportHeaders = properties != null ? properties.getTransportHeaders() : null;

          // Look at status code if there is one and honour it
          String status =
              transportHeaders != null
                  ? transportHeaders.getValue(ResourceResponse.HTTP_STATUS_CODE)
                  : null;
          if (status != null) {
            try {
              statusCode = Integer.parseInt(status);
            } catch (NumberFormatException e) {
              statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            }
          } else {
            statusCode = HttpServletResponse.SC_OK;
          }

          //
          contentType = piResponse.getContentType();
          charset = piResponse.getEncoding();

          //
          log.trace(
              "Try to get a resource of type: "
                  + contentType
                  + " for the portlet: "
                  + uiPortlet.getPortletContext());
          if (piResponse.getChars() != null) {
            content = piResponse.getChars();
          } else if (piResponse.getBytes() != null) {
            content = piResponse.getBytes();
          } else {
            content = null;
          }
        }

        //
        response.setStatus(statusCode);

        // Set content type if any
        if (contentType != null) {
          response.setContentType(contentType);
        }

        // Set encoding
        if (charset != null) {
          response.setCharacterEncoding(charset);
        }

        // Send headers if any
        if (transportHeaders != null) {
          sendHeaders(transportHeaders, context);
        }

        // Send body if any
        if (content instanceof String) {
          context.getWriter().write((String) content);
        } else if (content instanceof byte[]) {
          byte[] bytes = (byte[]) content;
          response.setContentLength(bytes.length);
          OutputStream stream = response.getOutputStream();
          try {
            stream.write(bytes);
          } finally {
            Safe.close(stream);
          }
        }

        //
        response.flushBuffer();
      } catch (Exception e) {
        if (!e.getClass().toString().contains("ClientAbortException")) {
          log.error(
              "Problem while serving resource "
                  + (resourceId != null ? resourceId : "")
                  + " for the portlet: "
                  + uiPortlet.getPortletContext().getId(),
              e);
        }
      } finally {
        /** The resource method does not need to go through the render phase */
        event.getRequestContext().setResponseComplete(true);
      }
    }
  @Override
  public boolean execute(ControllerContext context) throws Exception {
    String resourceParam = context.getParameter(RESOURCE_QN);
    String scopeParam = context.getParameter(SCOPE_QN);

    //
    if (scopeParam != null && resourceParam != null) {
      String compressParam = context.getParameter(COMPRESS_QN);
      String lang = context.getParameter(LANG_QN);
      String moduleParam = context.getParameter(MODULE_QN);

      //
      Locale locale = null;
      if (lang != null && lang.length() > 0) {
        locale = I18N.parseTagIdentifier(lang);
      }

      //
      ResourceScope scope;
      try {
        scope = ResourceScope.valueOf(ResourceScope.class, scopeParam);
      } catch (IllegalArgumentException e) {
        HttpServletResponse response = context.getResponse();
        String msg = "Unrecognized scope " + scopeParam;
        log.error(msg);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
        return true;
      }

      //
      ResourceId resource = new ResourceId(scope, resourceParam);

      ScriptKey key = new ScriptKey(resource, moduleParam, "min".equals(compressParam), locale);

      //
      ScriptResult result = cache.get(context, key);
      HttpServletResponse response = context.getResponse();
      HttpServletRequest request = context.getRequest();

      //
      if (result instanceof ScriptResult.Resolved) {
        ScriptResult.Resolved resolved = (ScriptResult.Resolved) result;

        // Content type + charset
        response.setContentType("text/javascript");
        response.setCharacterEncoding("UTF-8");

        // One hour caching
        // make this configurable later
        response.setHeader("Cache-Control", "max-age:3600");
        response.setDateHeader("Expires", System.currentTimeMillis() + 3600 * 1000);

        // Set content length
        response.setContentLength(resolved.bytes.length);

        long ifModifiedSince = request.getDateHeader(IF_MODIFIED_SINCE);
        if (resolved.isModified(ifModifiedSince)) {
          response.setDateHeader(ResourceRequestFilter.LAST_MODIFIED, resolved.lastModified);
          // Send bytes
          ServletOutputStream out = response.getOutputStream();
          try {
            out.write(resolved.bytes);
          } finally {
            Safe.close(out);
          }
        } else {
          response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        }
      } else if (result instanceof ScriptResult.Error) {
        ScriptResult.Error error = (ScriptResult.Error) result;
        log.error("Could not render script " + key + "\n:" + error.message);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      } else {
        String msg = "Resource " + key + " cannot be found";
        log.error(msg);
        response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
      }
    } else {
      HttpServletResponse response = context.getResponse();
      String msg = "Missing scope or resource param";
      log.error(msg);
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
    }

    //
    return true;
  }