001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jsp.userdata;
029
030import org.opencms.json.JSONException;
031import org.opencms.json.JSONObject;
032import org.opencms.main.CmsLog;
033
034import org.apache.commons.lang3.RandomStringUtils;
035import org.apache.commons.logging.Log;
036
037/**
038 * The stored information about a user data request.
039 * <p>
040 * This contains both the user data itself, as well as authentication information, for making sure the information can not just be automatically downloaded by someone
041 * intercepting the email sent to the user.
042 */
043public class CmsUserDataRequestInfo {
044
045    /** JSON key. */
046    public static final String A_AUTH = "auth";
047
048    /** JSON key. */
049    public static final String A_CONTENT = "content";
050
051    /** JSON key. */
052    public static final String A_EMAIL = "email";
053
054    /** JSON key. */
055    public static final String A_EXPIRATION = "exp";
056
057    /** JSON key. */
058    public static final String A_ID = "id";
059
060    /** JSON key. */
061    public static final String A_TYPE = "type";
062
063    /** JSON key. */
064    public static final String A_USERIDS = "userids";
065
066    /** JSON key. */
067    private static final String A_HTML = "html";
068
069    /** JSON key. */
070    private static final String A_USER = "user";
071
072    /** Logger instance for this class. */
073    private static final Log LOG = CmsLog.getLog(CmsUserDataRequestInfo.class);
074
075    /** The JSON object used to store the information. */
076    private JSONObject m_json = new JSONObject();
077
078    /**
079     * Creates a new user data request object.
080     */
081    public CmsUserDataRequestInfo() {
082
083        setId(RandomStringUtils.randomAlphanumeric(20).toLowerCase());
084        setAuth(RandomStringUtils.randomAlphanumeric(20).toLowerCase());
085    }
086
087    /**
088     * Creates a new instance from the JSON data in a string.
089     *
090     * @param data the data as a string
091     * @throws JSONException if parsing the JSON fails
092     */
093    public CmsUserDataRequestInfo(String data)
094    throws JSONException {
095
096        m_json = new JSONObject(data);
097    }
098
099    /**
100     * Checks that the given authorization code matches the one stored in this object.
101     *
102     * @param auth the authorization code
103     * @return true if the code matches
104     */
105    public boolean checkAuthCode(String auth) {
106
107        String storedAuth = getAuthCode();
108        return (storedAuth != null) && storedAuth.equals(auth);
109    }
110
111    /**
112     * Gets the authorization code.
113     *
114     * @return the authorization code
115     */
116    public String getAuthCode() {
117
118        return m_json.optString(A_AUTH);
119    }
120
121    /**
122     * Gets the email address.
123     *
124     * @return the email address
125     */
126    public String getEmail() {
127
128        return m_json.optString(A_EMAIL);
129    }
130
131    /**
132     * Gets the expiration date as a long.
133     *
134     * @return the expiration date
135     */
136    public long getExpiration() {
137
138        return m_json.optLong(A_EXPIRATION, -1l);
139    }
140
141    /**
142     * Gets the id of the user data request.
143     *
144     * @return the id
145     */
146    public String getId() {
147
148        return m_json.optString(A_ID);
149    }
150
151    /**
152     * Gets the user data HTML text.
153     *
154     * @return the user data HTML text
155     */
156    public String getInfoHtml() {
157
158        return m_json.optString(A_HTML);
159    }
160
161    /**
162     * Gets the user data request type (single user or email).
163     *
164     * @return the user data request type
165     */
166    public CmsUserDataRequestType getType() {
167
168        String strType = m_json.optString(A_TYPE);
169        if (strType == null) {
170            return null;
171        }
172        return CmsUserDataRequestType.valueOf(strType);
173    }
174
175    /**
176     * Gets the full user name.<p>
177     *
178     * This is not set in case of email-only user data requests.
179     *
180     * @return the full user name
181     */
182    public String getUserName() {
183
184        return m_json.optString(A_USER);
185    }
186
187    /**
188     * Returns true if the user data request has expired.
189     *
190     * @return true if the user data request has expired
191     */
192    public boolean isExpired() {
193
194        return System.currentTimeMillis() > getExpiration();
195    }
196
197    /**
198     * Sets the authorization code.
199     *
200     * @param auth the authorization code.
201     */
202    public void setAuth(String auth) {
203
204        setString(A_AUTH, auth);
205    }
206
207    /**
208     * Sets the email address.
209     *
210     * @param email the email address
211     */
212    public void setEmail(String email) {
213
214        setString(A_EMAIL, email);
215    }
216
217    /**
218     * Sets the expiration date.
219     *
220     * @param expiration the expiration date
221     */
222    public void setExpiration(long expiration) {
223
224        try {
225            m_json.put(A_EXPIRATION, expiration);
226        } catch (JSONException e) {
227            LOG.error(e.getLocalizedMessage(), e);
228        }
229    }
230
231    /**
232     * Sets the id.
233     *
234     * @param id the id
235     */
236    public void setId(String id) {
237
238        setString(A_ID, id);
239    }
240
241    /**
242     * Sets the user data HTML.
243     *
244     * @param infoHtml the user data HTML
245     */
246    public void setInfoHtml(String infoHtml) {
247
248        setString(A_HTML, infoHtml);
249    }
250
251    /**
252     * Sets the user data request type.
253     *
254     * @param type the user data request type
255     */
256    public void setType(CmsUserDataRequestType type) {
257
258        setString(A_TYPE, type.toString());
259    }
260
261    /**
262     * Sets the full user name.
263     *
264     * @param user the full user name
265     */
266    public void setUser(String user) {
267
268        setString(A_USER, user);
269    }
270
271    /**
272     * Formats this object as JSON text.
273     *
274     * @return the JSON text
275     */
276    public String toJson() {
277
278        return m_json.toString();
279    }
280
281    /**
282     * @see java.lang.Object#toString()
283     */
284    @Override
285    public String toString() {
286
287        return toJson();
288    }
289
290    /**
291     * Helper method to set a string value in the JSON.
292     *
293     * @param key the attribute name
294     * @param value the value
295     */
296    private void setString(String key, String value) {
297
298        try {
299            m_json.put(key, value);
300        } catch (JSONException e) {
301            LOG.error(e.getLocalizedMessage(), e);
302        }
303    }
304
305}