public static String calculateResourceBasePath(FacesContext facesContext) {
    FacesServletMapping mapping = getFacesServletMapping(facesContext);
    ExternalContext externalContext = facesContext.getExternalContext();

    if (mapping != null) {
      String resourceBasePath = null;
      if (mapping.isExtensionMapping()) {
        // Mapping using a suffix. In this case we have to strip
        // the suffix. If we have a url like:
        // http://localhost:8080/testjsf20/javax.faces.resource/imagen.jpg.jsf?ln=dojo
        //
        // The servlet path is /javax.faces.resource/imagen.jpg.jsf
        //
        // For obtain the resource name we have to remove the .jsf
        // suffix and
        // the prefix ResourceHandler.RESOURCE_IDENTIFIER
        resourceBasePath = externalContext.getRequestServletPath();
        int stripPoint = resourceBasePath.lastIndexOf('.');
        if (stripPoint > 0) {
          resourceBasePath = resourceBasePath.substring(0, stripPoint);
        }
      } else {
        // Mapping using prefix. In this case we have to strip
        // the prefix used for mapping. If we have a url like:
        // http://localhost:8080/testjsf20/faces/javax.faces.resource/imagen.jpg?ln=dojo
        //
        // The servlet path is /faces
        // and the path info is /javax.faces.resource/imagen.jpg
        //
        // For obtain the resource name we have to remove the /faces
        // prefix and
        // then the prefix ResourceHandler.RESOURCE_IDENTIFIER
        resourceBasePath = externalContext.getRequestPathInfo();
      }
      return resourceBasePath;
    } else {
      // If no mapping is detected, just return the
      // information follows the servlet path but before
      // the query string
      return externalContext.getRequestPathInfo();
    }
  }
  public static FacesServletMapping getFacesServletMapping(final FacesContext facesContext) {
    Map<Object, Object> attributes = facesContext.getAttributes();

    // Has the mapping already been determined during this request?
    FacesServletMapping mapping =
        (FacesServletMapping) attributes.get(FacesServletMapping.CACHED_SERVLET_MAPPING);
    if (mapping == null) {
      ExternalContext externalContext = facesContext.getExternalContext();
      mapping =
          FacesServletMapping.calculateFacesServletMapping(
              externalContext.getRequestServletPath(), externalContext.getRequestPathInfo());

      attributes.put(FacesServletMapping.CACHED_SERVLET_MAPPING, mapping);
    }
    return mapping;
  }