001    package com.khubla.pragmatach.framework.controller.impl.stat;
002    
003    import java.io.InputStream;
004    import java.util.Map;
005    
006    import javax.servlet.http.HttpServletResponse;
007    
008    import org.apache.commons.io.IOUtils;
009    
010    import com.khubla.pragmatach.framework.api.PragmatachException;
011    import com.khubla.pragmatach.framework.controller.impl.AbstractResponse;
012    
013    /**
014     * @author tome
015     */
016    public class StaticResourceResponse extends AbstractResponse {
017       /**
018        * InputStream
019        */
020       private final InputStream resourceInputStream;
021    
022       /**
023        * ctor
024        */
025       public StaticResourceResponse(Map<String, String> cacheHeaders, InputStream resourceInputStream) {
026          super(cacheHeaders);
027          this.resourceInputStream = resourceInputStream;
028       }
029    
030       @Override
031       public String getContentType() throws PragmatachException {
032          /*
033           * dunno what content type
034           */
035          return null;
036       }
037    
038       @Override
039       public Map<String, String> getHeaders() throws PragmatachException {
040          return getCacheHeaders();
041       }
042    
043       @Override
044       public int getHTTPCode() {
045          if (null != resourceInputStream) {
046             return HttpServletResponse.SC_OK;
047          } else {
048             return HttpServletResponse.SC_NOT_FOUND;
049          }
050       }
051    
052       @Override
053       public void render(HttpServletResponse httpServletResponse) throws PragmatachException {
054          try {
055             if (null != resourceInputStream) {
056                IOUtils.copy(resourceInputStream, httpServletResponse.getOutputStream());
057             }
058          } catch (final Exception e) {
059             throw new PragmatachException("Exception in render", e);
060          }
061       }
062    }