BrightSide Workbench Full Report + Source Code
MailUser.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.mail.impl;
19 
20 import java.util.Date;
21 import org.turro.elephant.context.ElephantContext;
22 import org.turro.elephant.context.IConstructor;
23 import org.turro.elephant.impl.context.ContextFactory;
24 import org.turro.elephant.security.IUser;
25 
30 public class MailUser {
31  private IConstructor constructor;
32  private IUser user;
33  private String link, message, exParam;
34 
35  public MailUser(IConstructor constructor, String login, String link, String message, String exParam) {
36  user = ContextFactory.getUser(constructor);
37  user.impersonate(login);
38  this.constructor = constructor;
39  this.link = link;
40  this.message = message;
41  this.exParam = exParam;
42  }
43 
44  public MailUser(IConstructor constructor, IUser user, String link, String message, String exParam) {
45  this.constructor = constructor;
46  this.user = user;
47  this.link = link;
48  this.message = message;
49  this.exParam = exParam;
50  }
51 
52  public String getEmail() {
53  if (user == null) {
54  return null;
55  }
56  return user.getProperty(IUser.CONNECTOR_EMAIL);
57  }
58 
59  public String getMessage(String comment) {
60  if(user == null) return null;
61 
62  if(message.indexOf("{linkTmpSes}") > -1) {
63  String key = user.createTmpKey(new Date(new Date().getTime() + (2L * 24L * 60L * 60L * 1000L)));
64  String server = ElephantContext.getServerUrl("http") +
65  (exParam == null ? "?" : exParam + "&") + "tmpses=" + key;
66  message = message.replaceAll("\\{linkTmpSes\\}", "<a href=\"" + server + "\">" + link + "</a>");
67  }
68 
69  message = message.replaceAll("\\{login\\}", user.getId())
70  .replaceAll("\\{name\\}", user.getName())
71  .replaceAll("\\{comment\\}", comment);
72 
73  return message;
74 }
75 
76  @Override
77  public boolean equals(Object obj) {
78  if (obj == null) {
79  return false;
80  }
81  if (getClass() != obj.getClass()) {
82  return false;
83  }
84  final MailUser other = (MailUser) obj;
85  if (this.user.getId() != other.user.getId() && (this.user.getId() == null || !this.user.getId().equals(other.user.getId()))) {
86  return false;
87  }
88  return true;
89  }
90 
91  @Override
92  public int hashCode() {
93  int hash = 7;
94  hash = 31 * hash + (this.user != null && this.user.getId() != null ? this.user.getId().hashCode() : 0);
95  return hash;
96  }
97 
98 }
static String getServerUrl(String scheme)
static IUser getUser(IConstructor constructor)
MailUser(IConstructor constructor, IUser user, String link, String message, String exParam)
Definition: MailUser.java:44
String getMessage(String comment)
Definition: MailUser.java:59
boolean equals(Object obj)
Definition: MailUser.java:77
MailUser(IConstructor constructor, String login, String link, String message, String exParam)
Definition: MailUser.java:35
static final String CONNECTOR_EMAIL
Definition: IUser.java:32
boolean impersonate(String login)
String getProperty(String key)
String createTmpKey(Date valid)