LocalizedNotificationImpl.java
6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.LocaleUtils
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.notifications.impl.push;
import com.adobe.cq.mobile.notifications.impl.LocalizedNotification;
import com.adobe.cq.mobile.notifications.impl.exceptions.NotificationException;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LocalizedNotificationImpl
implements LocalizedNotification {
private Locale locale;
private String message;
private String title;
private String link;
private String linkText;
private final Logger logger = LoggerFactory.getLogger(LocalizedNotificationImpl.class);
private LocalizedNotificationImpl() {
}
public LocalizedNotificationImpl(String locale, String title, String message) throws NotificationException {
this(locale, title, message, null, null);
}
public LocalizedNotificationImpl(String locale, String title, String message, String link, String linkText) throws NotificationException {
this.setMessage(message);
this.setTitle(title);
this.setLocale(LocaleUtils.toLocale((String)locale));
this.setLink(link);
this.setLinkText(linkText);
}
public LocalizedNotificationImpl(Resource resource) throws NotificationException {
if (resource == null) {
throw new NotificationException("Invalid resource.");
}
MobileResource mobileResource = (MobileResource)resource.getParent().getParent().adaptTo(MobileResource.class);
if (mobileResource == null || !mobileResource.isA(MobileResourceType.NOTIFICATION.getType())) {
throw new NotificationException("Invalid resource. Resource must be a notification message.");
}
ValueMap vm = resource.getChild("jcr:content").getValueMap();
this.setMessage((String)vm.get("message", String.class));
this.setTitle((String)vm.get("title", String.class));
this.setLink((String)vm.get("link", String.class));
this.setLinkText((String)vm.get("linkText", String.class));
this.setLocale(LocaleUtils.toLocale((String)((String)vm.get("locale", String.class))));
}
@Override
public Locale getLocale() {
return this.locale;
}
@Override
public String getLocaleAsString() {
String localeStr = "";
if (this.locale != null) {
if (!StringUtils.isBlank((CharSequence)this.locale.getLanguage())) {
localeStr = localeStr + this.locale.getLanguage();
}
if (!StringUtils.isBlank((CharSequence)this.locale.getCountry())) {
localeStr = localeStr + "_" + this.locale.getCountry();
}
if (!StringUtils.isBlank((CharSequence)this.locale.getVariant())) {
localeStr = localeStr + "_" + this.locale.getVariant();
}
}
return localeStr;
}
@Override
public void setLocale(Locale locale) throws NotificationException {
if (locale == null) {
throw new NotificationException("No valid locale was specified for this notification message.");
}
this.locale = locale;
}
@Override
public void setMessage(String message) throws NotificationException {
if (StringUtils.isBlank((CharSequence)message)) {
throw new NotificationException("No message was specified for this notification message: " + this.getLocaleAsString());
}
if (message.trim().length() > 250) {
throw new NotificationException("Notification message exceeded maximum length. The maximum length is 250.");
}
this.message = message.trim();
}
@Override
public String getMessage() {
return this.message;
}
@Override
public void setTitle(String title) throws NotificationException {
if (title == null) {
this.title = "";
} else {
if (title.trim().length() > 50) {
throw new NotificationException("Notification title exceeded maximum length. The maximum length is 50.");
}
this.title = title.trim();
}
}
@Override
public String getTitle() {
return this.title;
}
@Override
public String getLink() {
return this.link;
}
@Override
public void setLink(String link) throws NotificationException {
this.link = link;
}
@Override
public String getLinkText() {
return this.linkText;
}
@Override
public void setLinkText(String linkText) throws NotificationException {
this.linkText = StringUtils.isBlank((CharSequence)linkText) ? "OK" : linkText;
}
@Override
public Map<String, Object> getProperties() {
HashMap<String, Object> props = new HashMap<String, Object>();
this.addPropertyToMap(props, "locale", this.getLocaleAsString());
this.addPropertyToMap(props, "message", this.getMessage());
this.addPropertyToMap(props, "title", this.getTitle());
this.addPropertyToMap(props, "linkText", this.getLinkText());
this.addPropertyToMap(props, "link", this.getLink());
return Collections.unmodifiableMap(props);
}
@Override
public JSONObject toJSONObject() {
JSONObject json = new JSONObject();
this.addJsonStringProperty(json, "locale", this.getLocaleAsString());
this.addJsonStringProperty(json, "title", this.getTitle());
this.addJsonStringProperty(json, "message", this.getMessage());
this.addJsonStringProperty(json, "linkText", this.getLinkText());
this.addJsonStringProperty(json, "link", this.getLink());
return json;
}
private void addPropertyToMap(Map<String, Object> map, String key, Object value) {
if (value != null) {
map.put(key, value);
}
}
private void addJsonStringProperty(JSONObject json, String key, String value) {
try {
json.put(key, (Object)value);
}
catch (JSONException jsonEx) {
this.logger.warn("Error converting Notification to JSON with value '" + key + "'", (Throwable)jsonEx);
}
}
}