Coverage Report - com.buckosoft.PicMan.web.GetPicController
 
Classes in this File Line Coverage Branch Coverage Complexity
GetPicController
0%
0/72
0%
0/32
7.333
 
 1  
 /******************************************************************************
 2  
  * GetPicController.java - The Spring controller that returns a Pic
 3  
  * 
 4  
  * PicMan - The BuckoSoft Picture Manager in Java
 5  
  * Copyright(c) 2008 - Dick Balaska
 6  
  * 
 7  
  */
 8  
 package com.buckosoft.PicMan.web;
 9  
 
 10  
 import java.awt.Color;
 11  
 import java.awt.Font;
 12  
 import java.awt.Graphics2D;
 13  
 import java.awt.image.BufferedImage;
 14  
 import java.io.IOException;
 15  
 import java.io.PrintWriter;
 16  
 import java.util.Iterator;
 17  
 
 18  
 import javax.imageio.ImageIO;
 19  
 import javax.imageio.ImageWriter;
 20  
 import javax.imageio.stream.ImageOutputStream;
 21  
 import javax.servlet.ServletException;
 22  
 import javax.servlet.ServletOutputStream;
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpServletResponse;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 import org.springframework.web.servlet.ModelAndView;
 29  
 
 30  
 import com.buckosoft.BSAccount.web.BSAccountPageController;
 31  
 import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
 32  
 import com.buckosoft.PicMan.business.PicManFacade;
 33  
 import com.buckosoft.PicMan.db.DatabaseFacade;
 34  
 import com.buckosoft.PicMan.domain.Pic;
 35  
 
 36  
 /** The Spring controller that returns a Pic.
 37  
  * @author Dick Balaska
 38  
  * @since 2008/09/06
 39  
  * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/GetPicController.java">GetPicController.java</a>
 40  
  */
 41  0
 public class GetPicController extends BSAccountPageController {
 42  
         
 43  0
         protected final Log logger = LogFactory.getLog(getClass());
 44  
         
 45  
         private DatabaseFacade dbf;
 46  
         private PicManFacade pmf;
 47  
 
 48  
         /** Set the reference to the PicMan Database
 49  
          * @param dbf The DatabaseFacade
 50  
          */
 51  0
         public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
 52  
 
 53  
         /** Set the reference to the PicMan API.
 54  
          * @param pmf The PicManFacade
 55  
          */
 56  0
         public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
 57  
 
 58  
         
 59  
         /** Spring standard http request handler, returns an image/jpeg
 60  
          * @param request The http request
 61  
          * @param response The http response
 62  
          * @return null
 63  
          */ 
 64  
         public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
 65  
                         throws ServletException, IOException {
 66  0
                 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
 67  
 
 68  
                 String s;
 69  0
                 Pic                pic = null;
 70  0
                 int                width = 1000;
 71  0
                 int                height = 1000;
 72  0
                 boolean        slideShow = false;
 73  0
                 if (!userWebSession.getUser().isRegisteredUser()) {
 74  0
                         response.setContentType("text/plain");
 75  0
                         response.sendError(401, "Not logged in");
 76  0
                         return(null);
 77  
                 }
 78  0
                 s = request.getParameter("w");
 79  0
                 if (s != null)
 80  0
                         width = Integer.parseInt(s);
 81  0
                 s = request.getParameter("h");
 82  0
                 if (s != null)
 83  0
                         height = Integer.parseInt(s);
 84  0
                 s = request.getParameter("ss");
 85  0
                 if (s != null)
 86  0
                         slideShow = true;
 87  0
                 s = request.getParameter("importPic");
 88  0
                 if (s != null) {
 89  0
                         pic = new Pic(true, s);
 90  
                 }
 91  0
                 if (pic == null) {
 92  0
                         s = request.getParameter("pic");
 93  0
                         if (s == null) {
 94  0
                                 PrintWriter out = response.getWriter();
 95  0
                                 out.print("No pic in request");
 96  0
                                 return(null);
 97  
                         }
 98  0
                         pic = dbf.getPic(s);
 99  0
                         if (pic == null) {
 100  0
                                 PrintWriter out = response.getWriter();
 101  0
                                 out.print("Unknown pic \"" + s + "\"");
 102  0
                                 return(null);
 103  
                         }
 104  
                 }
 105  
                 BufferedImage bi;
 106  0
                 bi = pmf.getPicReader().readPic(pic);
 107  0
                 if (slideShow) {
 108  0
                         double scale = 2.0;
 109  0
                         String label = "Hi Marlene";
 110  0
                         label = pmf.getFilterAsString(pic.getName());
 111  0
                         logger.info("label:" + label);
 112  0
                         double scalew = 1.0;
 113  0
                         if (width != 0)
 114  0
                                 scalew = (double)width / (double)bi.getWidth();
 115  0
                         double scaleh = 1.0;
 116  0
                         if (height != 0)
 117  0
                                 scaleh = (double)height / (double)bi.getHeight();
 118  0
                         logger.info("scaleh = " + scaleh + " scalew = " + scalew);
 119  0
                         if (label != null) {
 120  0
                                 if (scaleh < 1.0 && scaleh < scalew)
 121  0
                                         scale /= scaleh;
 122  0
                                 if (scalew < 1.0 && scalew < scaleh)
 123  0
                                         scale /= scalew;
 124  0
                                 logger.info("scale = " + scale);
 125  0
                                 int xp = 0;
 126  0
                                 int yp = 0+bi.getHeight() - 3;
 127  0
                                 Graphics2D g = bi.createGraphics();
 128  0
                                 Font font = new Font("SansSerif", Font.PLAIN, (int)(12.0*scale));
 129  0
                                 g.setFont(font);
 130  0
                                 g.setColor(Color.BLACK);
 131  
 //                                g.drawString(label, (int)(xp-1*scale), (int)(yp-1*scale));
 132  
 //                                g.drawString(label, (int)(xp-1*scale), (int)(yp+1*scale));
 133  
 //                                g.drawString(label, (int)(xp+1*scale), (int)(yp-1*scale));
 134  0
                                 g.drawString(label, (int)(xp+1*scale), (int)(yp+1*scale));
 135  0
                                 g.setColor(Color.WHITE);
 136  0
                                 g.drawString(label, xp,   yp);
 137  
                         }
 138  
                 }
 139  0
                 response.setContentType("image/jpeg");
 140  0
                 ServletOutputStream sos = response.getOutputStream();
 141  0
                 ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
 142  0
                 Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
 143  0
                 ImageWriter writer = (ImageWriter)writers.next();
 144  0
                 writer.setOutput(ios);
 145  0
                 writer.write(bi);
 146  
                 
 147  0
                 return(null);
 148  
         }
 149  
         
 150  
 }