BrightSide Workbench Full Report + Source Code
ImageMailTracker.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.mail.tracker;
20 
21 import java.awt.image.BufferedImage;
22 import java.io.File;
23 import java.io.OutputStream;
24 import java.io.UnsupportedEncodingException;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import javax.imageio.ImageIO;
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import org.turro.string.Strings;
34 import org.turro.collections.KeyValueMap;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.context.IConstructor;
37 import org.turro.elephant.db.ElephantPU;
38 import org.turro.elephant.direct.DirectContent;
39 import org.turro.elephant.direct.DirectContents;
40 import org.turro.elephant.direct.IDirectContent;
41 import org.turro.elephant.entities.db.MailTracker;
42 import org.turro.plugin.contacts.IContact;
43 
48 @DirectContent(identifier="img-tk")
49 public class ImageMailTracker implements IDirectContent {
50 
51  public static String createURL(IConstructor constructor, String image, String entityPath, IContact contact)
52  throws UnsupportedEncodingException, Exception {
53  if(contact != null && contact.isWebUser()) {
54  HashMap<String, String> values = new HashMap<>();
55  values.put("idcontact", contact.getId());
56  values.put("entityPath", entityPath);
57  values.put("image", image);
58  return DirectContents.doCreateParameter(getIdentifier(), getIdentifier(), values);
59  } else {
60  return "";
61  }
62  }
63 
64  public static String getIdentifier() {
65  return ImageMailTracker.class.getAnnotation(DirectContent.class).identifier();
66  }
67 
68  @Override
69  public boolean itsMe(String id) {
70  return getIdentifier().equals(id);
71  }
72 
73  @Override
74  public boolean myTurn(HttpServletRequest request) {
75  return DirectContents.isYourTurn(request, getIdentifier());
76  }
77 
78  @Override
79  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
80  try {
81  IConstructor constructor = ElephantContext.getConstructor(request, response);
82  String imgtk = constructor.getParameter(getIdentifier(), false);
83  if(!Strings.isBlank(imgtk)) {
84  KeyValueMap map = new KeyValueMap(ElephantContext.decrypt(imgtk));
85  response.setContentType("image/png");
86  File f = new File(ElephantContext.getRealPath(map.get("image")));
87  BufferedImage bi = ImageIO.read(f);
88  OutputStream out = response.getOutputStream();
89  ImageIO.write(bi, "png", out);
90  out.close();
91  MailTracker mt = new MailTracker();
92  mt.setEntityPath(map.get("entityPath"));
93  mt.setIdContact(map.get("idcontact"));
94  mt.setReadDate(new Date());
95  new ElephantPU().saveObject(mt);
96  }
97  } catch (Exception ex) {
98  Logger.getLogger(ImageMailTracker.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
99  }
100  }
101 
102 }
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static boolean isYourTurn(HttpServletRequest request, String path)
static String doCreateParameter(String id, String parameter, Map< String, String > values)
boolean myTurn(HttpServletRequest request)
static String createURL(IConstructor constructor, String image, String entityPath, IContact contact)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)