拥有 ResourceHandler 以从数据库流式传输图像 [英] Own ResourceHandler to stream images from DB

查看:18
本文介绍了拥有 ResourceHandler 以从数据库流式传输图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现自己的资源实现.getInputStream 方法不会被调用.

我的处理程序:

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;
    }

}

在我的faces-config.xml中:

In my faces-config.xml:

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

在我的 jsf 中:

In my jsf:

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

以 html 格式输出:

Output in html:

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


从getRequestPath返回:/foo/javax.faces.resource/66?ln=media_lib

MediaResource 被调用并初始化,但 getInputStream 未被调用.FireBug 在此 url 上显示 404(调用两次).我完全不明白我在这里做错了什么.

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.

谢谢
乔尼

推荐答案

发现错误.我的资源实现的getRequestPath有问题.我忘记了 url 中到faces-servlet 的faces-mapping (Util.getFacesMapping(context)):

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.

感谢 BalusC 的帮助.

Thanks to BalusC for his help.

干杯
乔尼

这篇关于拥有 ResourceHandler 以从数据库流式传输图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆