1 | /*
|
---|
2 | * OAIResumptionToken.java
|
---|
3 | * Copyright (C) 2014 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 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 General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 |
|
---|
20 | package org.greenstone.gsdl3.util;
|
---|
21 |
|
---|
22 | import org.greenstone.util.GlobalProperties;
|
---|
23 |
|
---|
24 | import org.w3c.dom.*;
|
---|
25 |
|
---|
26 | import java.io.File;
|
---|
27 | import java.util.HashMap;
|
---|
28 |
|
---|
29 | // import file Logger.java
|
---|
30 | import org.apache.log4j.*;
|
---|
31 |
|
---|
32 | /** */
|
---|
33 | public class OAIResumptionToken {
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | // Other fields we save
|
---|
38 | public static final String CURRENT_SET = "current_set";
|
---|
39 | public static final String CURRENT_CURSOR = "current_cursor";
|
---|
40 |
|
---|
41 | public static final String FILE_SEPARATOR = File.separator;
|
---|
42 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GSXML.class.getName());
|
---|
43 |
|
---|
44 | public static XMLConverter converter = new XMLConverter();
|
---|
45 | public static Element resumption_token_elem = null;
|
---|
46 | //used when saving the token file
|
---|
47 | public static File resumption_token_file = null;
|
---|
48 |
|
---|
49 | private static HashMap<String, HashMap<String,String>> stored_tokens = new HashMap<String, HashMap<String,String>>();
|
---|
50 |
|
---|
51 | private static HashMap<String, Long> expiration_data = new HashMap<String, Long>();
|
---|
52 |
|
---|
53 | /** initialize stored resumption tokens */
|
---|
54 | public static void init() {
|
---|
55 |
|
---|
56 | }
|
---|
57 | /** generate the token, and store all the data for it */
|
---|
58 | public static String createAndStoreResumptionToken(String set, String metadata_prefix, String from, String until, String cursor, String current_set, String current_cursor) {
|
---|
59 | HashMap<String, String> data = new HashMap<String, String>();
|
---|
60 | long expiration_date = System.currentTimeMillis();
|
---|
61 | String base_token_name = ""+expiration_date;
|
---|
62 | stored_tokens.put(base_token_name, data);
|
---|
63 | expiration_date += (OAIXML.token_expiration*1000);
|
---|
64 | expiration_data.put(base_token_name, new Long(expiration_date));
|
---|
65 | String token_name = base_token_name+":" + cursor + ":" + current_set +":"+ current_cursor;
|
---|
66 | data.put(OAIXML.FROM, from);
|
---|
67 | data.put(OAIXML.UNTIL, until);
|
---|
68 | data.put(OAIXML.SET, set);
|
---|
69 | data.put(OAIXML.METADATA_PREFIX, metadata_prefix);
|
---|
70 | return token_name;
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | public static long getExpirationDate(String token) {
|
---|
75 | if (token.indexOf(":") != -1) {
|
---|
76 | token = token.substring(0, token.indexOf(":"));
|
---|
77 | }
|
---|
78 | return expiration_data.get(token).longValue();
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static String updateToken(String token, String cursor, String current_set, String current_cursor) {
|
---|
82 | if (token.indexOf(":") != -1) {
|
---|
83 | token = token.substring(0, token.indexOf(":"));
|
---|
84 | }
|
---|
85 |
|
---|
86 | // when we are generating a new token, we update the expiration date to allow the new token to have the full length of time
|
---|
87 | long exp_date = System.currentTimeMillis();
|
---|
88 | exp_date += (OAIXML.token_expiration*1000);
|
---|
89 | expiration_data.put(token, exp_date);
|
---|
90 |
|
---|
91 | token = token + ":" + cursor + ":" + current_set + ":" + current_cursor;
|
---|
92 | return token;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** check that this token is currently valid */
|
---|
96 | public static boolean isValidToken(String token) {
|
---|
97 | // take off the set/cursor parts to check for the main key
|
---|
98 | if (token.indexOf(":") != -1) {
|
---|
99 | token = token.substring(0, token.indexOf(":"));
|
---|
100 | logger.error("looking up "+token);
|
---|
101 | }
|
---|
102 | if (stored_tokens.containsKey(token)) {
|
---|
103 | return true;
|
---|
104 | }
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public static HashMap<String, String> getTokenData(String token) {
|
---|
109 | // find the base name
|
---|
110 | String base_name = token;
|
---|
111 | if (token.indexOf(":") != -1) {
|
---|
112 | base_name = token.substring(0, token.indexOf(":"));
|
---|
113 | logger.error("getting data for "+base_name);
|
---|
114 | }
|
---|
115 | HashMap<String, String> data = new HashMap<String, String>(stored_tokens.get(base_name));
|
---|
116 | if (data == null) {
|
---|
117 | logger.error("data was null!!");
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 | if (base_name != token) {
|
---|
121 | String[] parts = token.split(":");
|
---|
122 | if (parts.length != 4) {
|
---|
123 | // something wrong!!
|
---|
124 | }
|
---|
125 | data.put(OAIXML.CURSOR, parts[1]);
|
---|
126 | data.put(CURRENT_SET, parts[2]);
|
---|
127 | data.put(CURRENT_CURSOR, parts[3]);
|
---|
128 | }
|
---|
129 | // add in cursor, etc form the token name
|
---|
130 | //return stored_tokens.get(token);
|
---|
131 | return data;
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | public static void clearExpiredTokens() {
|
---|
136 |
|
---|
137 |
|
---|
138 | }
|
---|
139 | // ********************************************************************
|
---|
140 | // *********************************************************************
|
---|
141 | // following is old code
|
---|
142 | /** Read in OAIResumptionToken.xml (residing web/WEB-INF/classes/) */
|
---|
143 | public static Element getOAIResumptionTokenXML() {
|
---|
144 |
|
---|
145 | // The system environment variable $GSDL3HOME(ends ../web) does not contain the file separator
|
---|
146 | resumption_token_file = new File(GlobalProperties.getGSDL3Home() + FILE_SEPARATOR +
|
---|
147 | "WEB-INF" + FILE_SEPARATOR + "classes" +FILE_SEPARATOR + "OAIResumptionToken.xml");
|
---|
148 | if (resumption_token_file.exists()) {
|
---|
149 | Document token_doc = converter.getDOM(resumption_token_file, "utf-8");
|
---|
150 | if (token_doc != null) {
|
---|
151 | resumption_token_elem = token_doc.getDocumentElement();
|
---|
152 | } else {
|
---|
153 | logger.error("Fail to parse resumption token file OAIReceptionToken.xml.");
|
---|
154 | return null;
|
---|
155 | }
|
---|
156 | //remove all expired tokens
|
---|
157 | clearExpiredTokens();
|
---|
158 | return resumption_token_elem;
|
---|
159 | }
|
---|
160 | //if resumption_token_file does not exist
|
---|
161 | logger.info("resumption token file: "+ resumption_token_file.getPath()+" not found! create an empty one.");
|
---|
162 | Document doc = converter.newDOM();
|
---|
163 | resumption_token_elem = doc.createElement(/*OAI_RESUMPTION_TOKENS*/"resumptionTokens");
|
---|
164 | saveOAIResumptionTokenXML(resumption_token_elem);
|
---|
165 | return resumption_token_elem;
|
---|
166 | }
|
---|
167 | public static void saveOAIResumptionTokenXML(Element token_elem) {
|
---|
168 | if(converter.writeDOM(token_elem, resumption_token_file) == false) {
|
---|
169 | logger.error("Fail to save the resumption token file");
|
---|
170 | }
|
---|
171 | }
|
---|
172 | public static void clearExpiredTokensOld() {
|
---|
173 | boolean token_deleted = false;
|
---|
174 | NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN);
|
---|
175 | for (int i=0; i<tokens.getLength(); i++) {
|
---|
176 | Element token_elem = (Element)tokens.item(i);
|
---|
177 | String expire_str = token_elem.getAttribute(OAIXML.EXPIRATION_DATE);
|
---|
178 | long datestamp = OAIXML.getTime(expire_str); // expire_str is in milliseconds
|
---|
179 | if(datestamp < System.currentTimeMillis()) {
|
---|
180 | resumption_token_elem.removeChild(token_elem);
|
---|
181 | token_elem = null;
|
---|
182 | token_deleted = true;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | if(token_deleted) {
|
---|
187 | saveOAIResumptionTokenXML(resumption_token_elem);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | public static boolean containsToken(String token) {
|
---|
191 | NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN);
|
---|
192 | for (int i=0; i<tokens.getLength(); i++) {
|
---|
193 | if(token.equals(GSXML.getNodeText((Element)tokens.item(i)).trim() ))
|
---|
194 | return true;
|
---|
195 | }
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 | public static void addToken(Element token) {
|
---|
199 | Document doc = resumption_token_elem.getOwnerDocument();
|
---|
200 | resumption_token_elem.appendChild(doc.importNode(token, true));
|
---|
201 | saveOAIResumptionTokenXML(resumption_token_elem);
|
---|
202 | }
|
---|
203 | public static void addToken(String token) {
|
---|
204 | Element te = resumption_token_elem.getOwnerDocument().createElement(OAIXML.RESUMPTION_TOKEN);
|
---|
205 | //add expiration att
|
---|
206 | resumption_token_elem.appendChild(te);
|
---|
207 | saveOAIResumptionTokenXML(resumption_token_elem);
|
---|
208 | }
|
---|
209 | public static boolean removeToken(String token) {
|
---|
210 | NodeList tokens = GSXML.getChildrenByTagName(resumption_token_elem, OAIXML.RESUMPTION_TOKEN);
|
---|
211 | int num_tokens = tokens.getLength();
|
---|
212 | for (int i=0; i<num_tokens; i++) {
|
---|
213 | Element e = (Element)(tokens.item(i));
|
---|
214 | if(token.equals(GSXML.getNodeText(e))) {
|
---|
215 | resumption_token_elem.removeChild(e);
|
---|
216 | saveOAIResumptionTokenXML(resumption_token_elem);
|
---|
217 | return true;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | return false;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | }
|
---|