Results 1 to 9 of 9

Thread: How to read META-INF/MANIFEST.MF from an action?

  1. #1

    Default How to read META-INF/MANIFEST.MF from an action?

    I am trying to figure out the preferred way to read my war's manifest file from within an action in webflow. I am a little stuck because I don't have access to the servlet context inside a webflow action. Is there a way to inject this into an action as a Resource? I haven't been able to figure this out - any help much appreciated.

    Thanks!

  2. #2
    Join Date
    Nov 2010
    Posts
    2

    Default

    in ur webflow action get servlet request object(from requestContext) and then using ServletContext read it.

    InputStream in = servletReq.getSession().getServletContext().getRes ourceAsStream("/META-INF/MANIFEST.MF");
    Manifest manifest = new Manifest(in);
    java.util.jar.Attributes attributes = manifest.getMainAttributes();
    System.out.println("in details..."+attributes.size());
    System.out.println("Built-By:"+attributes.getValue("Built-By"));

  3. #3

    Default

    That is all well and good, except for the fact that a webflow RequestContext is not linked to an HTTP request, and you cannot get the request object from it. Hence the point of my question. Your code will not work when passing in a flowRequestContext.

  4. #4
    Join Date
    Nov 2008
    Posts
    742

    Default

    Seems to me you should be able to get the request from externalContext.getNativeRequest(). ExternalContext is a special webflow EL variable. Maybe that's what you're looking for?

  5. #5
    Join Date
    Nov 2010
    Posts
    2

    Default

    Try this way to get HttpServletRequst

    see the methods in ExternalContext class. If your external system that has interacted with the webflow is Servlet, try something like this.

    ((ServletExternalContext)reqContext.getExternalCon text()).getRequest();

    If you are doing this in a Portlet, get portletRequest first and unwrap it to ServletRequest, then to HttpServletRequest.

  6. #6

    Default

    In my Spring Webflow app:
    context.getExternalContext() returns a
    Code:
    org.springframework.webflow.mvc.servlet.MvcExternalContext
    which extends
    Code:
    org.springframework.webflow.context.servlet.ServletExternalContext
    However, the method getRequest(), as in :
    Code:
    HttpServletRequest request = (MvcExternalContext)context.getExternalContext()).getRequest();
    is protected, so I still can't get the HttpServletRequest

  7. #7
    Join Date
    Nov 2008
    Posts
    742

    Default

    Use ExternalContext's getNativeRequest() method, and cast it to an HttpServletRequest.

  8. #8

    Default

    Thanks for all your help. I was able to do it like you suggested:
    Code:
     try {
    
                    HttpServletRequest request = (HttpServletRequest)((MvcExternalContext)context.getExternalContext()).getNativeRequest();
                    InputStream in = request.getSession().getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
                    Manifest manifest = null;
    
                    manifest = new Manifest(in);
                    java.util.jar.Attributes atts = manifest.getMainAttributes();
    
                    version       = atts.getValue("Implementation-Version");
                    buildTime     = atts.getValue("Implementation-Build-Time");
                    artifactTitle = atts.getValue("Implementation-Title");
    
                } catch (Exception e) {
    
                }
    One caveat though, since I am using a MockRequestContext in my tests, I had to refactor my test code like this:
    Code:
    private void startFlowWithMvcMockedContext(MutableAttributeMap input) {
            MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "testurl");
            MockHttpServletResponse servletResponse = new MockHttpServletResponse();
            MockServletContext servletContext = new MockServletContext();
    
            FlowUrlHandler flowUrlHandler = new DefaultFlowUrlHandler();
    
            MvcExternalContext servletExternalContext = new MvcExternalContext(
            servletContext, servletRequest, servletResponse, flowUrlHandler);
    
            MockRequestContext context = new MockRequestContext();
            context.setExternalContext(servletExternalContext) ;
    
            startFlow(input, context.getExternalContext());
        }

  9. #9
    Join Date
    May 2011
    Posts
    3

    Default

    I would recommend to use Manifests from jcabi-manifests. With this utility class you can read any attribute with just one line:

    Code:
    String value = Manifests.read("My-Attribute");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •