Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

jsf 2 - Own ResourceHandler to stream images from DB

I'm strugging with my own resource-implementation. The getInputStream-method doesn't get called.

My handler:

public class ResourceHandlerWrapperImpl extends
        ResourceHandlerWrapper {

  private final ResourceHandler wrapped;

  public ResourceHandlerWrapper(final ResourceHandler wrapped)
  {
    this.wrapped = wrapped;
  }

  @Override
  public ResourceHandler getWrapped()
  {
    return wrapped;
  }

  @Override
  public Resource createResource(final String resourceName, final String libraryName)
  {
    if (AppConstants.RESOURCE_MEDIA_LIB.equals(libraryName))
    {
      return new MediaResource(resourceName);
    }
    else
    {
      return super.createResource(resourceName, libraryName);
    }
  }

  /**
   * @see javax.faces.application.ResourceHandlerWrapper#libraryExists(java.lang.String)
   */
  @Override
  public boolean libraryExists(final String libraryName)
  {
    if (AppConstants.RESOURCE_MEDIA_LIB.equals(libraryName))
    {
      return true;
    }
    else
    {
      return super.libraryExists(libraryName);
    }
  }

  /**
   * @see javax.faces.application.ResourceHandlerWrapper#isResourceRequest(javax.faces.context.FacesContext)
   */
  @Override
  public boolean isResourceRequest(final FacesContext context)
  {
    return super.isResourceRequest(context);
  }

}

My resource implementation:

public class MediaResource extends Resource {

    private final String mediaId;

    public MediaResource(final String mediaId) {
        setLibraryName(AppConstants.RESOURCE_MEDIA_LIB);
        setResourceName(mediaId);
        setContentType("image/png");
        this.mediaId = mediaId;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        if (mediaId != null) {
            System.out.println("Yeahhh!!!");
        }

        return null;
    }

    @Override
    public Map<String, String> getResponseHeaders() {
        return new HashMap<String, String>();
    }

    @Override
    public String getRequestPath() {
        final FacesContext context = FacesContext.getCurrentInstance();
        return context
                .getApplication()
                .getViewHandler()
                .getResourceURL(
                        context,
                        ResourceHandler.RESOURCE_IDENTIFIER + "/" + mediaId
                                + "?ln=" + AppConstants.RESOURCE_MEDIA_LIB);
    }

    @Override
    public URL getURL() {
        return null;
    }

    @Override
    public boolean userAgentNeedsUpdate(final FacesContext context) {
        return true;
    }

}

In my faces-config.xml:

<application>
    <resource-handler>com.foo.bbb.ResourceHandlerWrapperImpl</resource-handler>
</application>

In my jsf:

<h:graphicImage library="media_lib" name="66" width="50" />

Output in html:

<img src="/foo/javax.faces.resource/66?ln=media_lib" width="50" />


Return from getRequestPath: /foo/javax.faces.resource/66?ln=media_lib

MediaResource is called and initialized, but the getInputStream isn't called. FireBug shows a 404 on this url (called twice). I'm totally puzzled what I'm doing wrong here.

Thanks
Jonny

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

found the mistake .The getRequestPath of my rescource implementation was faulty. I forgot the faces-mapping (Util.getFacesMapping(context)) to the faces-servlet in the url:

@Override
    public String getRequestPath() {
        final FacesContext context = FacesContext.getCurrentInstance();
        return context
                .getApplication()
                .getViewHandler()
                .getResourceURL(
                        context,
                        ResourceHandler.RESOURCE_IDENTIFIER + "/" + mediaId + Util.getFacesMapping(context)
                                + "?ln=" + AppConstants.RESOURCE_MEDIA_LIB);

Everything works now as expected.

Thanks to BalusC for his help.

Cheers
Jonny


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...