View Javadoc
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  public class GetPicController extends BSAccountPageController {
42  	
43  	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  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
52  
53  	/** Set the reference to the PicMan API.
54  	 * @param pmf The PicManFacade
55  	 */
56  	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  		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
67  
68  		String s;
69  		Pic		pic = null;
70  		int		width = 1000;
71  		int		height = 1000;
72  		boolean	slideShow = false;
73  		if (!userWebSession.getUser().isRegisteredUser()) {
74  			response.setContentType("text/plain");
75  			response.sendError(401, "Not logged in");
76  			return(null);
77  		}
78  		s = request.getParameter("w");
79  		if (s != null)
80  			width = Integer.parseInt(s);
81  		s = request.getParameter("h");
82  		if (s != null)
83  			height = Integer.parseInt(s);
84  		s = request.getParameter("ss");
85  		if (s != null)
86  			slideShow = true;
87  		s = request.getParameter("importPic");
88  		if (s != null) {
89  			pic = new Pic(true, s);
90  		}
91  		if (pic == null) {
92  			s = request.getParameter("pic");
93  			if (s == null) {
94  				PrintWriter out = response.getWriter();
95  				out.print("No pic in request");
96  				return(null);
97  			}
98  			pic = dbf.getPic(s);
99  			if (pic == null) {
100 				PrintWriter out = response.getWriter();
101 				out.print("Unknown pic \"" + s + "\"");
102 				return(null);
103 			}
104 		}
105 		BufferedImage bi;
106 		bi = pmf.getPicReader().readPic(pic);
107 		if (slideShow) {
108 			double scale = 2.0;
109 			String label = "Hi Marlene";
110 			label = pmf.getFilterAsString(pic.getName());
111 			logger.info("label:" + label);
112 			double scalew = 1.0;
113 			if (width != 0)
114 				scalew = (double)width / (double)bi.getWidth();
115 			double scaleh = 1.0;
116 			if (height != 0)
117 				scaleh = (double)height / (double)bi.getHeight();
118 			logger.info("scaleh = " + scaleh + " scalew = " + scalew);
119 			if (label != null) {
120 				if (scaleh < 1.0 && scaleh < scalew)
121 					scale /= scaleh;
122 				if (scalew < 1.0 && scalew < scaleh)
123 					scale /= scalew;
124 				logger.info("scale = " + scale);
125 				int xp = 0;
126 				int yp = 0+bi.getHeight() - 3;
127 				Graphics2D g = bi.createGraphics();
128 				Font font = new Font("SansSerif", Font.PLAIN, (int)(12.0*scale));
129 				g.setFont(font);
130 				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 				g.drawString(label, (int)(xp+1*scale), (int)(yp+1*scale));
135 				g.setColor(Color.WHITE);
136 				g.drawString(label, xp,   yp);
137 			}
138 		}
139 		response.setContentType("image/jpeg");
140 		ServletOutputStream sos = response.getOutputStream();
141 		ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
142 		Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
143 		ImageWriter writer = (ImageWriter)writers.next();
144 		writer.setOutput(ios);
145 		writer.write(bi);
146 		
147 		return(null);
148 	}
149 	
150 }