Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 453
user871611 Avatar asked Mar 03 '12 12:03

user871611


1 Answers

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

like image 127
user871611 Avatar answered Oct 29 '22 19:10

user871611