BrightSide Workbench Full Report + Source Code
ContentService.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.action;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.imageio.ImageIO;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.turro.collections.parser.ParserException;
32 import org.turro.string.Strings;
33 import org.apache.commons.fileupload.FileItemIterator;
34 import org.apache.commons.fileupload.FileItemStream;
35 import org.apache.commons.fileupload.FileUploadException;
36 import org.apache.commons.fileupload.servlet.ServletFileUpload;
37 import org.apache.commons.fileupload.util.Streams;
38 import org.turro.barcode.Barcode;
39 import org.turro.collections.KeyValueMap;
40 import org.turro.elephant.context.Application;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.context.IConstructor;
43 import org.turro.elephant.direct.DirectContent;
44 import org.turro.elephant.direct.DirectContents;
45 import org.turro.elephant.direct.EntryPoint;
46 import org.turro.elephant.direct.IDirectContent;
47 import org.turro.elephant.direct.IEntryPoint;
48 import org.turro.elephant.impl.util.CookieUtil;
49 import org.turro.reflection.Instances;
50 import org.turro.util.ImageUtil;
51 
56 @DirectContent(identifier="content-service")
57 public class ContentService implements IDirectContent {
58 
59  public static String createURL(IConstructor constructor, KeyValueMap values) {
60  String exrino = Actions.createRightNowAction(values);
62  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
63  "?" + exrino;
64  }
65 
66  public static String createURL(IConstructor constructor, String values) {
67  String exrino = Actions.createRightNowAction(values);
69  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
70  "?" + exrino;
71  }
72 
73  public static String createEntryPoint(IConstructor constructor, String entryPoint, String valueStr) {
74  try {
75  return createEntryPoint(constructor, entryPoint, new KeyValueMap(valueStr));
76  } catch (ParserException ex) {
77  Logger.getLogger(ContentService.class.getName()).log(Level.SEVERE, null, ex);
78  }
79  return null;
80  }
81 
82  public static String createEntryPoint(IConstructor constructor, String entryPoint, KeyValueMap values) {
83  values.put("type", "entry-point");
84  values.put("entry-point", entryPoint);
85  String exrino = Actions.createRightNowAction(values);
87  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
88  "?" + exrino;
89  }
90 
91  public static String getIdentifier() {
92  return ContentService.class.getAnnotation(DirectContent.class).identifier();
93  }
94 
95  @Override
96  public boolean itsMe(String id) {
97  return getIdentifier().equals(id);
98  }
99 
100  @Override
101  public boolean myTurn(HttpServletRequest request) {
102  return DirectContents.isYourTurn(request, getIdentifier());
103  }
104 
105  @Override
106  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
107  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
108  if(isMultipart) {
109  try {
110  ServletFileUpload upload = new ServletFileUpload();
111  upload.setHeaderEncoding(ElephantContext.getEncoding());
112  FileItemIterator iter = upload.getItemIterator(request);
113  File file = null;
114  String redirect = null;
115  Double fixWidth = null, fixHeight = null;
116  while(iter.hasNext()) {
117  FileItemStream item = iter.next();
118  String name = item.getFieldName();
119  InputStream stream = item.openStream();
120  if(item.isFormField()) {
121  switch(name) {
122  case "fileName":
123  file = new File(ElephantContext.getRealPath(Streams.asString(stream, ElephantContext.getEncoding())));
124  break;
125  case "redirect":
126  redirect = Streams.asString(stream, ElephantContext.getEncoding());
127  break;
128  case "fixWidth":
129  fixWidth = Double.valueOf(Streams.asString(stream, ElephantContext.getEncoding()));
130  break;
131  case "fixHeight":
132  fixHeight = Double.valueOf(Streams.asString(stream, ElephantContext.getEncoding()));
133  break;
134  }
135  } else if(file != null) {
136  file.getParentFile().mkdirs();
137  FileOutputStream fos = new FileOutputStream(file);
138  Streams.copy(item.openStream(), fos, true);
139  }
140  }
141  if(file != null && fixWidth != null && fixHeight != null) {
142  ImageUtil.smart(file, file, fixWidth, fixHeight);
143  }
144  if(!Strings.isBlank(redirect)) {
146  }
147  } catch (FileUploadException | IOException ex) {
148  Logger.getLogger(ContentService.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("Multipart"), ex);
149  }
150  } else {
151  KeyValueMap map = Actions.getRightNowAction(ElephantContext.getConstructor(request, response));
152  if(map != null) {
153  String type = map.get("type");
154  if("qrcode".equals(type)) {
155  try {
156  response.setContentType("image/png");
157  Barcode qr = new Barcode();
158  qr.writeImageCode(
159  response.getOutputStream(),
160  qr.encodeQrCode(map.get("data"), 300),
161  "png");
162  } catch (IOException ex) {
163  Logger.getLogger(ContentService.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("QRCode"), ex);
164  }
165  } else if("image".equals(type)) {
166  try {
167  response.setContentType("image/png");
168  ImageIO.write(ImageIO.read(new File(map.get("data"))), "png", response.getOutputStream());
169  } catch (IOException ex) {
170  Logger.getLogger(ContentService.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("Image"), ex);
171  }
172  } else if("cookie".equals(type)) {
173  if(map.containsKey("remove")) {
174  CookieUtil.deleteCookie(response, map.get("name"), map.getOrDefault("path", "/"));
175  } else {
176  Application.setCookie(map.get("name"), map.get("value"),
177  map.getOrDefault("path", "/"),
178  map.get(Integer.class, "age", 60 * 60 * 24 * 365 * 10));
179  }
180  } else if("entry-point".equals(type)) {
181  if(map.containsKey("entry-point")) {
182  getEntryPoint(map.get("entry-point")).execute(map);
183  }
184  }
185  }
186  }
187  }
188 
189  private IEntryPoint getEntryPoint(String identifier) {
190  return Instances.cached().byAnnotation(EntryPoint.class, IEntryPoint.class).stream()
191  .filter(e -> ((EntryPoint) e.getClass().getAnnotation(EntryPoint.class)).identifier().equals(identifier))
192  .findFirst().orElse(new IEntryPoint() {});
193  }
194 
195 }
196 
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static String createRightNowAction(String values)
Definition: Actions.java:312
boolean myTurn(HttpServletRequest request)
static String createURL(IConstructor constructor, String values)
static String createEntryPoint(IConstructor constructor, String entryPoint, KeyValueMap values)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static String createEntryPoint(IConstructor constructor, String entryPoint, String valueStr)
static String createURL(IConstructor constructor, KeyValueMap values)
abstract void sendRedirect(String uri)
static void setCookie(String name, String value, String path, int age)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static boolean isYourTurn(HttpServletRequest request, String path)
static void deleteCookie(HttpServletResponse response, String name, String path)
Definition: CookieUtil.java:53