19 package org.turro.action;
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;
56 @DirectContent(identifier=
"content-service")
75 return createEntryPoint(constructor, entryPoint,
new KeyValueMap(valueStr));
76 }
catch (ParserException ex) {
77 Logger.getLogger(
ContentService.class.getName()).log(Level.SEVERE,
null, ex);
83 values.put(
"type",
"entry-point");
84 values.put(
"entry-point", entryPoint);
96 public boolean itsMe(String
id) {
97 return getIdentifier().equals(
id);
101 public boolean myTurn(HttpServletRequest request) {
106 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
107 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
110 ServletFileUpload upload =
new ServletFileUpload();
112 FileItemIterator iter = upload.getItemIterator(request);
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()) {
135 }
else if(file !=
null) {
136 file.getParentFile().mkdirs();
137 FileOutputStream fos =
new FileOutputStream(file);
138 Streams.copy(item.openStream(), fos,
true);
141 if(file !=
null && fixWidth !=
null && fixHeight !=
null) {
142 ImageUtil.smart(file, file, fixWidth, fixHeight);
144 if(!Strings.isBlank(redirect)) {
147 }
catch (FileUploadException | IOException ex) {
153 String type = map.get(
"type");
154 if(
"qrcode".equals(type)) {
156 response.setContentType(
"image/png");
157 Barcode qr =
new Barcode();
159 response.getOutputStream(),
160 qr.encodeQrCode(map.get(
"data"), 300),
162 }
catch (IOException ex) {
165 }
else if(
"image".equals(type)) {
167 response.setContentType(
"image/png");
168 ImageIO.write(ImageIO.read(
new File(map.get(
"data"))),
"png", response.getOutputStream());
169 }
catch (IOException ex) {
172 }
else if(
"cookie".equals(type)) {
173 if(map.containsKey(
"remove")) {
177 map.getOrDefault(
"path",
"/"),
178 map.get(Integer.class,
"age", 60 * 60 * 24 * 365 * 10));
180 }
else if(
"entry-point".equals(type)) {
181 if(map.containsKey(
"entry-point")) {
182 getEntryPoint(map.get(
"entry-point")).execute(map);
189 private IEntryPoint getEntryPoint(String identifier) {
191 .filter(e -> ((
EntryPoint) e.getClass().getAnnotation(
EntryPoint.class)).identifier().equals(identifier))
static KeyValueMap getRightNowAction(IConstructor constructor)
static String createRightNowAction(String values)
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 getIdentifier()
static String createURL(IConstructor constructor, KeyValueMap values)
abstract void sendRedirect(String uri)
static Application getApplication()
static void setCookie(String name, String value, String path, int age)
static String getRealPath(String path)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static String getRootWebPath()
static String logMsg(String msg)
static String getEncoding()
static boolean isYourTurn(HttpServletRequest request, String path)
static final String DIRECT_CONTENT_PATH
static void deleteCookie(HttpServletResponse response, String name, String path)