jsr369-experts@servlet-spec.java.net

[jsr369-experts] Re: From JSF Expert Group: Mapping HttpServletRequest.getMapping()

From: Edward Burns <edward.burns_at_oracle.com>
Date: Thu, 10 Sep 2015 06:40:58 -0700

Copying over from users list:

>> On Thu, Sep 10, 2015 at 12:59 PM, Mark Thomas <markt_at_apache.org> wrote:

MT> Exactly what would this method do?

On 10/09/2015 13:00, arjan tijms wrote:

AT> The request is described in somewhat more detail here:
AT>
AT> https://java.net/jira/browse/SERVLET_SPEC-73
AT>

MT > What are the use cases?

AT> *** Usecase 1 ***

AT> Simplifying web framework (e.g. JSF) implementation code that now
AT> tries to find out
AT> mapping. Specifically this should make it easier to support additional
AT> mapping types.
AT>
AT> For the JSF RI this is specifically referring to the code in
AT> com.sun.faces.lifecycle.RestoreViewPhase.execute,
AT> com.sun.faces.application.view.MultiViewHandler.derivePhysicalViewId
AT> and com.sun.faces.util.Util.getFacesMapping/getMappingForRequest/isPrefixMapped
AT>
AT> E.g.
AT>
AT> private static String getMappingForRequest(String servletPath, String
AT> pathInfo) {
AT>
AT> if (servletPath == null) {
AT> return null;
AT> }
AT> if (LOGGER.isLoggable(Level.FINE)) {
AT> LOGGER.log(Level.FINE, "servletPath " + servletPath);
AT> LOGGER.log(Level.FINE, "pathInfo " + pathInfo);
AT> }
AT> // If the path returned by HttpServletRequest.getServletPath()
AT> // returns a zero-length String, then the FacesServlet has
AT> // been mapped to '/*'.
AT> if (servletPath.length() == 0) {
AT> return "/*";
AT> }

AT> // presence of path info means we were invoked
AT> // using a prefix path mapping
AT> if (pathInfo != null) {
AT> return servletPath;
AT> } else if (servletPath.indexOf('.') < 0) {
AT> // if pathInfo is null and no '.' is present, assume the
AT> // FacesServlet was invoked using prefix path but without
AT> // any pathInfo - i.e. GET /contextroot/faces or
AT> // GET /contextroot/faces/
AT> return servletPath;
AT> } else {
AT> // Servlet invoked using extension mapping
AT> return servletPath.substring(servletPath.lastIndexOf('.'));
AT> }
AT> }

AT> *** Usecase 2 ***
AT>
AT> Handle virtual resources by using both default and other (e.g. exact)
AT> mapping. If a filter for the faces servlet can determine via which
AT> mapping it was invoked, it can take a special action first, and then
AT> continue the chain.
AT>
AT> For example, map the faces servlet to /foo and /. If it's invoked via
AT> /foo, just continue the chain. If it's invoked via /bar, do something
AT> special, then continue the chain.

AT> *** Signature of the proposed method ***
AT>
AT> /**
AT> * Returns details regarding the mapping that the servlet container used
AT> * to map the current client request to a servlet.
AT> *
AT> **/
AT> public Mapping getMapping();
AT>
AT> Definition of Mapping:
AT>
AT> public interface Mapping {
AT> MappingType getMappingType(); // See Servlet spec 12.2
AT> String getMatch();
AT> String getPatern();
AT> boolean isImplicit(); // see Servlet spec 12.2.1
AT> }
AT>
AT> Definition of MappingType:
AT>
AT> public enum MappingType {
AT> PATH, EXTENSION, CONTEXT_ROOT, DEFAULT, EXACT
AT> }
AT>
AT>
AT> Some examples:
AT>
AT> FacesServlet mapped to /faces/*, /bar/*, *.xhtml and /foo
AT>
AT> Request = /faces/mypage.jsf
AT>
AT> getMappingType() - PATH
AT> getMatch() = /mypage.jsf
AT> getPattern() = /faces/*
AT>
AT>
AT> Request = /bar/mypage.jsf
AT>
AT> getMappingType() - PATH
AT> getMatch() = /mypage.jsf
AT> getPattern() = /bar/*
AT>
AT>
AT> Request = /mypage.xhtml
AT>
AT> getMappingType() - EXTENSION
AT> getMatch() = /mypage
AT> getPattern() = *.xhtml
AT>
AT>
AT> Request = /foo
AT>
AT> getMappingType() - EXACT
AT> getMatch() = /foo
AT> getPattern() = /foo
AT>
AT> Hope this makes it more clear.

>>>>> On Thu, 10 Sep 2015 13:18:32 +0100, Mark Thomas <markt_at_apache.org> said:

MT> Much clearer. Many thanks. +1 from me.


-- 
| edward.burns_at_oracle.com | office: +1 407 458 0017
| 41 Business days til JavaOne 2015
| 56 Business days til DOAG 2015