001    package com.khubla.pragmatach.plugin.responsive;
002    
003    import java.io.InputStream;
004    import java.util.Map;
005    
006    import javax.servlet.http.HttpServletResponse;
007    
008    import com.khubla.pragmatach.framework.api.PragmatachException;
009    import com.khubla.pragmatach.framework.controller.impl.AbstractResponse;
010    
011    /**
012     * @author tome
013     */
014    public class ResponsiveImageResponse extends AbstractResponse {
015       /**
016        * imageResource
017        */
018       private final InputStream imageResource;
019       /**
020        * width
021        */
022       private final int width;
023       /**
024        * xscale
025        */
026       private final double xscale;
027       /**
028        * yscale
029        */
030       private final double yscale;
031       /**
032        * height
033        */
034       private final int height;
035    
036       public ResponsiveImageResponse(Map<String, String> cacheHeaders, InputStream imageResource, double xscale, double yscale) {
037          super(cacheHeaders);
038          this.imageResource = imageResource;
039          width = 0;
040          height = 0;
041          this.xscale = xscale;
042          this.yscale = yscale;
043       }
044    
045       public ResponsiveImageResponse(Map<String, String> cacheHeaders, InputStream imageResource, int width, int height) {
046          super(cacheHeaders);
047          this.imageResource = imageResource;
048          this.width = width;
049          this.height = height;
050          xscale = 0;
051          yscale = 0;
052       }
053    
054       @Override
055       public String getContentType() throws PragmatachException {
056          return CONTENT_TYPE_HTML;
057       }
058    
059       @Override
060       public Map<String, String> getHeaders() throws PragmatachException {
061          return super.getCacheHeaders();
062       }
063    
064       @Override
065       public void render(HttpServletResponse httpServletResponse) throws PragmatachException {
066          try {
067             if (0 != width) {
068                ImageResizer.resize(imageResource, httpServletResponse.getOutputStream(), width, height);
069             } else {
070                ImageResizer.resize(imageResource, httpServletResponse.getOutputStream(), xscale, yscale);
071             }
072          } catch (final Exception e) {
073             throw new PragmatachException("Exception in render", e);
074          }
075       }
076    }