View Javadoc
1   /******************************************************************************
2    * WebAjaxController.java - The Spring controller for the PicMan AJAX calls
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2007 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.web;
9   
10  import java.io.IOException;
11  import java.io.OutputStream;
12  import java.io.UnsupportedEncodingException;
13  import java.net.SocketException;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  
18  import org.apache.catalina.connector.ClientAbortException;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.dom4j.Document;
22  import org.dom4j.io.OutputFormat;
23  import org.dom4j.io.XMLWriter;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import com.buckosoft.BSAccount.web.BSAccountPageController;
27  import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
28  import com.buckosoft.PicMan.business.PicManFacade;
29  import com.buckosoft.PicMan.dom.ChainListDom;
30  import com.buckosoft.PicMan.dom.ChainSetSizeListDom;
31  import com.buckosoft.PicMan.dom.DirsInDirDom;
32  import com.buckosoft.PicMan.dom.EngineReportDom;
33  import com.buckosoft.PicMan.dom.HomeStatusDom;
34  import com.buckosoft.PicMan.dom.MosaicBatchSetupDom;
35  import com.buckosoft.PicMan.dom.MosaicSetupDom;
36  import com.buckosoft.PicMan.dom.MosaicTilesDom;
37  import com.buckosoft.PicMan.dom.PicFilterDom;
38  import com.buckosoft.PicMan.dom.PicsInContactDom;
39  import com.buckosoft.PicMan.dom.PicsInDirDom;
40  import com.buckosoft.PicMan.dom.PicsInSetDom;
41  import com.buckosoft.PicMan.dom.PosterSetupDom;
42  import com.buckosoft.PicMan.dom.RootsDom;
43  import com.buckosoft.PicMan.dom.SetManDom;
44  import com.buckosoft.PicMan.dom.SetsInChainDom;
45  import com.buckosoft.PicMan.dom.SyncStatusDom;
46  import com.buckosoft.PicMan.dom.VirginsDom;
47  import com.buckosoft.PicMan.domain.User;
48  
49  /** Spring controller that returns DOMs for the PicMan javascripts.
50   * @author Dick Balaska
51   * @since 2007/12/01
52   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/WebAjaxController.java">WebAjaxController.java</a>
53   */
54  public class WebAjaxController extends BSAccountPageController {
55  	private static boolean DEBUG = false;
56  	protected final Log logger = LogFactory.getLog(getClass());
57  
58  	private PicManFacade pmf;
59  
60  	/** Set the reference to the PicMan API.
61  	 * @param pmf The PicManFacade
62  	 */
63  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
64  
65  	/** Enable debug logger output on this module
66  	 * @param debugFlag true == turn on debugging.
67  	 */
68  	public void setDEBUG(boolean debugFlag) {
69  		DEBUG = debugFlag;
70  	}
71  
72  	/** Spring standard http request handler.
73  	 * @param request The http request.
74  	 * @param response The http response.
75  	 * @return null to return just an http status because we pushed the DOM out the response.
76  	 */ 
77  	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
78  		String cid = request.getParameter("cid");
79  		String c = request.getParameter("c");
80  		String command = request.getParameter("dom");
81  		if (DEBUG) {
82  			//String sid = request.getParameter("sid");
83  			if (!command.equals("PicManStatus"))
84  				logger.info("XML Request com=" + command + " c=" + c + " cid=" + cid);
85  		}
86  
87  		User user = null;
88  		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
89  		if (userWebSession != null) 
90  			user = (User)userWebSession.getUser();
91  
92  		response.setContentType("text/xml");
93  		response.addHeader("Cache-Control", "max-age=0");
94  		response.addHeader("Cache-Control", "no-cache");
95  		response.addHeader("expires", "0");
96  		response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
97  		response.addHeader("Pragma", "no-cache");
98  
99  		Document dom;
100 		if (command == null)
101 			dom = HomeStatusDom.createDocument(pmf, user);
102 		else if (command.equals("ChainList"))
103 			dom = ChainListDom.createDocument(pmf);
104 		else if (command.equals("DirsInDir"))
105 			dom = DirsInDirDom.createDocument(pmf, Integer.parseInt(cid), c);
106 		else if (command.equals("EngineReport"))
107 			dom = EngineReportDom.createDocument(pmf);
108 		else if (command.equals("Mosaic"))
109 			dom = MosaicSetupDom.createDocument(pmf, Integer.parseInt(cid));
110 		else if (command.equals("MosaicList"))
111 			dom = MosaicSetupDom.createDocument(pmf);
112 		else if (command.equals("MosaicBatchList"))
113 			dom = MosaicBatchSetupDom.createDocument(pmf);
114 		else if (command.equals("MosaicTiles"))
115 			dom = MosaicTilesDom.createDocument(pmf, Integer.parseInt(cid));
116 		else if (command.equals("PicFilter"))
117 			dom = PicFilterDom.createDocument(pmf, c);
118 		else if (command.equals("PicsInContact")) 
119 			 dom = PicsInContactDom.createDocument(pmf, Integer.parseInt(cid), c);
120 		else if (command.equals("PicsInDir"))
121 			dom = PicsInDirDom.createDocument(pmf, user, Integer.parseInt(cid), c);
122 		else if (command.equals("PicManStatus"))
123 			dom = HomeStatusDom.createDocument(pmf, user);
124 		else if (command.equals("PicsInSet"))
125 			dom = PicsInSetDom.createDocument(pmf, Integer.parseInt(cid), 75);
126 		else if (command.equals("PicsInSetCount"))
127 			dom = PicsInSetDom.createDocument(pmf, cid, Integer.parseInt(c));
128 		else if (command.equals("PosterList"))
129 			dom = PosterSetupDom.createDocument(pmf);
130 		else if (command.equals("RootsList"))
131 			dom = RootsDom.createDocument(pmf);
132 		else if (command.equals("SetList"))
133 			dom = SetManDom.createDocument(pmf, null);
134 		else if (command.equals("SetMan"))
135 			dom = SetManDom.createDocument(pmf, user);
136 		else if (command.equals("SetsInChainList"))
137 			dom = SetsInChainDom.createDocument(pmf, Integer.parseInt(cid));
138 		else if (command.equals("SetSizeList")) {
139 			String sid = request.getParameter("sid");
140 			dom = ChainSetSizeListDom.createDocument(pmf, Integer.parseInt(cid), Integer.parseInt(sid));
141 		}
142 		else if (command.equals("SyncStatus"))
143 			dom = SyncStatusDom.createDocument(pmf);
144 		else if (command.equals("Virgins"))
145 			dom = VirginsDom.createDocument(pmf);
146 		else
147 			dom = HomeStatusDom.createDocument(pmf, user);
148 		try {
149 			OutputStream stream = response.getOutputStream();
150 			OutputFormat outformat = OutputFormat.createPrettyPrint();
151 			outformat.setEncoding("ISO-8859-1");
152 			XMLWriter writer = new XMLWriter(stream, outformat);
153 			writer.write(dom);
154 			writer.flush();
155 		} catch (SocketException e) {
156 			logger.info("Caught socket exception");
157 			return(null);
158 		} catch (UnsupportedEncodingException e) {
159 			logger.info(e);
160 			pmf.addError(e);
161 		} catch (ClientAbortException e) {
162 			logger.warn("Caught ClientAbortException");
163 			return(null);
164 		} catch (IOException e) {
165 			logger.info(e);
166 			pmf.addError(e);
167 		} catch (Exception e) {
168 			return(null);
169 		}
170 
171 		return null;
172 	}
173 }