| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 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.util.LinkedList; |
| 14 | |
|
| 15 | |
import javax.servlet.http.HttpServletRequest; |
| 16 | |
import javax.servlet.http.HttpServletResponse; |
| 17 | |
|
| 18 | |
import org.apache.commons.logging.Log; |
| 19 | |
import org.apache.commons.logging.LogFactory; |
| 20 | |
import org.dom4j.Document; |
| 21 | |
import org.dom4j.io.OutputFormat; |
| 22 | |
import org.dom4j.io.XMLWriter; |
| 23 | |
import org.springframework.web.servlet.ModelAndView; |
| 24 | |
import org.springframework.web.servlet.mvc.Controller; |
| 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.FiltersChangedDom; |
| 30 | |
import com.buckosoft.PicMan.domain.Filter; |
| 31 | |
import com.buckosoft.PicMan.domain.SetSize; |
| 32 | |
import com.buckosoft.PicMan.domain.User; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | 0 | public class WebFilterController extends BSAccountPageController implements Controller { |
| 43 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 44 | |
|
| 45 | |
private PicManFacade pmf; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | public void setPicMan(PicManFacade pmf) { this.pmf = pmf; } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { |
| 58 | 0 | response.setContentType("text/xml"); |
| 59 | 0 | response.addHeader("Cache-Control", "max-age=0"); |
| 60 | 0 | response.addHeader("Cache-Control", "no-cache"); |
| 61 | 0 | response.addHeader("expires", "0"); |
| 62 | 0 | response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT"); |
| 63 | 0 | response.addHeader("Pragma", "no-cache"); |
| 64 | |
|
| 65 | 0 | User user = null; |
| 66 | 0 | BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request); |
| 67 | 0 | if (userWebSession != null) |
| 68 | 0 | user = (User)userWebSession.getUser(); |
| 69 | 0 | if (user == null || !user.isRegisteredUser()) { |
| 70 | |
try { |
| 71 | 0 | response.sendError(500, "Not logged in"); |
| 72 | 0 | } catch (IOException e1) { |
| 73 | 0 | e1.printStackTrace(); |
| 74 | 0 | } |
| 75 | 0 | return(null); |
| 76 | |
} |
| 77 | 0 | LinkedList<SetSize> setsChanged = new LinkedList<SetSize>(); |
| 78 | |
|
| 79 | 0 | String picName = request.getHeader("pic"); |
| 80 | 0 | logger.info("Save Filters pic=" + picName); |
| 81 | 0 | Filter filter = pmf.getDB().getFilter(picName); |
| 82 | 0 | Filter filtero = pmf.getDB().getFilter(picName); |
| 83 | |
String v; |
| 84 | 0 | int i = 0; |
| 85 | |
while (true) { |
| 86 | 0 | v = request.getHeader("f" + i++); |
| 87 | 0 | if (v == null) |
| 88 | 0 | break; |
| 89 | 0 | String[] sp = v.split(":"); |
| 90 | 0 | int sp1 = -1; |
| 91 | |
try { |
| 92 | 0 | sp1 = Integer.parseInt(sp[1]); |
| 93 | 0 | } catch (NumberFormatException e2) { |
| 94 | |
try { |
| 95 | 0 | String t = "Error parsing filter for " + picName; |
| 96 | 0 | logger.error(t); |
| 97 | 0 | logger.error(v + ", " + sp[0] + "--" + sp[1] + "==" + filter.getFilter(sp[0])); |
| 98 | 0 | response.sendError(501, t); |
| 99 | 0 | e2.printStackTrace(); |
| 100 | 0 | } catch (IOException e) { |
| 101 | 0 | e.printStackTrace(); |
| 102 | 0 | } |
| 103 | 0 | return(null); |
| 104 | 0 | } |
| 105 | 0 | int sp0 = filter.getFilter(sp[0]); |
| 106 | 0 | logger.debug(v + ", " + sp[0] + "--" + sp1 + "==" + sp0); |
| 107 | 0 | if (sp1 != sp0) { |
| 108 | 0 | SetSize ss = new SetSize(sp[0]); |
| 109 | 0 | setsChanged.add(ss); |
| 110 | |
} |
| 111 | 0 | filter.addFilter(sp[0], sp1); |
| 112 | 0 | } |
| 113 | 0 | filter.setTimeNow(); |
| 114 | 0 | pmf.getDB().addFilter(filter); |
| 115 | |
|
| 116 | 0 | Document dom = FiltersChangedDom.createDocument(pmf, filtero, filter); |
| 117 | |
try { |
| 118 | 0 | OutputStream stream = response.getOutputStream(); |
| 119 | 0 | OutputFormat outformat = OutputFormat.createPrettyPrint(); |
| 120 | 0 | outformat.setEncoding("ISO-8859-1"); |
| 121 | 0 | XMLWriter writer = new XMLWriter(stream, outformat); |
| 122 | 0 | writer.write(dom); |
| 123 | 0 | writer.flush(); |
| 124 | 0 | } catch (UnsupportedEncodingException e) { |
| 125 | 0 | logger.info(e); |
| 126 | 0 | pmf.addError(e); |
| 127 | 0 | } catch (IOException e) { |
| 128 | 0 | logger.info(e); |
| 129 | 0 | pmf.addError(e); |
| 130 | 0 | } |
| 131 | 0 | for (SetSize ss : setsChanged) |
| 132 | 0 | pmf.fireSetSizeChanged(ss); |
| 133 | 0 | return null; |
| 134 | |
} |
| 135 | |
} |