KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > jeffiles > ManageJefFile


1 // $Id: ManageJefFile.java 10570 2005-09-13 09:35:40Z pvollenweider $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// ManageServer
15
//
16
// 12.06.2001 NK added in jahia.
17
//
18

19 package org.jahia.admin.jeffiles;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30
31 import org.jahia.bin.Jahia;
32 import org.jahia.bin.JahiaAdministration;
33 import org.jahia.data.JahiaData;
34 import org.jahia.exceptions.JahiaException;
35 import org.jahia.params.ParamBean;
36 import org.jahia.registries.ServicesRegistry;
37 import org.jahia.resourcebundle.JahiaResourceBundle;
38 import org.jahia.services.jef.InvalidJefFileException;
39 import org.jahia.services.jef.JecFile;
40 import org.jahia.services.jef.JefFileConstants;
41 import org.jahia.services.jef.JefFileInvalidKeyValueException;
42 import org.jahia.services.jef.JefFileKeyNotFoundException;
43 import org.jahia.services.sites.JahiaSite;
44 import org.jahia.services.sites.JahiaSitesService;
45 import org.jahia.services.usermanager.JahiaUser;
46 import org.jahia.services.webapps_deployer.JahiaWebAppsDeployerService;
47
48 import org.jahia.security.license.License;
49
50 /**
51  * desc: This class is used by the administration to manage the
52  * server settings of a jahia portal, like the mail notification service (when
53  * jahia or a user generate error(s), or like the java server home disk path,
54  * mail server, etc.
55  *
56  * Copyright: Copyright (c) 2002
57  * Company: Jahia Ltd
58  *
59  * @author Khue Nguyen
60  * @version 1.0
61  */

62 public class ManageJefFile {
63
64     private static final org.apache.log4j.Logger logger =
65             org.apache.log4j.Logger.getLogger (ManageJefFile.class);
66
67     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
68
69     private JahiaSite site;
70     private JahiaUser user;
71     private ServicesRegistry sReg;
72
73     private static JahiaSitesService sMgr;
74
75     private License coreLicense;
76
77
78     /**
79      * Default constructor.
80      *
81      * @param request Servlet request.
82      * @param response Servlet response.
83      * @param session Servlet session for the current user.
84      */

85     public ManageJefFile (HttpServletRequest JavaDoc request,
86                           HttpServletResponse JavaDoc response,
87                           HttpSession JavaDoc session)
88             throws Throwable JavaDoc
89     {
90         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
91         ParamBean jParams = null;
92         if (jData != null) {
93             jParams = jData.params ();
94         }
95
96         coreLicense = Jahia.getCoreLicense ();
97         if (coreLicense == null) {
98             // set request attributes...
99
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.invalidLicenseKey.label",
100                     jParams, jParams.getLocale ());
101             request.setAttribute ("jahiaDisplayMessage", dspMsg);
102             // redirect...
103
JahiaAdministration.doRedirect (request, response, session, JSP_PATH + "menu.jsp");
104             return;
105         }
106
107         userRequestDispatcher (request, response, session);
108     }
109
110
111     /**
112      * This method is used like a dispatcher for user requests.
113      *
114      * @param request Servlet request.
115      * @param response Servlet response.
116      * @param session Servlet session for the current user.
117      */

118     private void userRequestDispatcher (HttpServletRequest JavaDoc request,
119                                         HttpServletResponse JavaDoc response,
120                                         HttpSession JavaDoc session)
121             throws Throwable JavaDoc {
122
123
124         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
125         ParamBean jParams = null;
126         if (jData != null) {
127             jParams = jData.params ();
128         }
129         sReg = ServicesRegistry.getInstance ();
130
131         if (sReg != null) {
132             sMgr = sReg.getJahiaSitesService ();
133         }
134
135         // check if the user has really admin access to this site...
136
user = (JahiaUser)session.getAttribute (ParamBean.SESSION_USER);
137         site = (JahiaSite) session.getAttribute( ParamBean.SESSION_SITE );
138
139         if (user != null && sReg != null && sMgr != null) {
140
141             String JavaDoc operation = request.getParameter ("sub");
142
143             if (operation.equals ("catlist")) {
144                 displayCategoryList (request, response, session);
145             } else if (operation.equals ("unlockjec")) {
146                 decryptJec (request, response, session);
147             }
148
149         } else {
150             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
151                     jParams, jParams.getLocale ());
152             request.setAttribute ("jahiaDisplayMessage", dspMsg);
153             JahiaAdministration.doRedirect (request,
154                     response,
155                     session, JSP_PATH + "menu.jsp");
156         }
157     }
158
159
160     //-------------------------------------------------------------------------
161
/**
162      * Display the list of encrypted files categories
163      *
164      * @param request Servlet request.
165      * @param response Servlet response.
166      * @param session HttpSession object.
167      */

168     private void displayCategoryList (HttpServletRequest JavaDoc request,
169                                       HttpServletResponse JavaDoc response,
170                                       HttpSession JavaDoc session)
171             throws IOException JavaDoc, ServletException JavaDoc {
172
173         logger.debug ("started");
174
175         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
176         ParamBean jParams = null;
177         if (jData != null) {
178             jParams = jData.params ();
179         }
180         try {
181             JahiaWebAppsDeployerService appDepServ = sReg.getJahiaWebAppsDeployerService ();
182
183             if (appDepServ == null) {
184                 throw new JahiaException ("Unavailable Services",
185                         "Unavailable Services",
186                         JahiaException.SERVICE_ERROR,
187                         JahiaException.ERROR_SEVERITY);
188             }
189
190             JahiaAdministration.doRedirect (request,
191                     response,
192                     session,
193                     JSP_PATH + "manage_jeffile.jsp");
194
195         } catch (JahiaException je) {
196             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
197                     jParams, jParams.getLocale ());
198             request.setAttribute ("jahiaDisplayMessage", dspMsg);
199             JahiaAdministration.doRedirect (request,
200                     response,
201                     session,
202                     JSP_PATH + "menu.jsp");
203         }
204
205     }
206
207
208     /**
209      * Search Jahia Encrypted Components and decrypt them
210      *
211      * @param request Servlet request.
212      * @param response Servlet response.
213      * @param session HttpSession object.
214      */

215     private void decryptJec (HttpServletRequest JavaDoc request,
216                              HttpServletResponse JavaDoc response,
217                              HttpSession JavaDoc session)
218             throws IOException JavaDoc, ServletException JavaDoc {
219
220         logger.debug ("started");
221         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
222         ParamBean jParams = null;
223         if (jData != null) {
224             jParams = jData.params ();
225         }
226
227         try {
228             JahiaWebAppsDeployerService appDepServ = sReg.getJahiaWebAppsDeployerService ();
229
230             if (appDepServ == null) {
231                 throw new JahiaException ("Unavailable Services",
232                         "Unavailable Services",
233                         JahiaException.SERVICE_ERROR,
234                         JahiaException.ERROR_SEVERITY);
235             }
236
237             Enumeration JavaDoc enumeration = null;
238             try {
239                 enumeration = sMgr.getSites ();
240             } catch (JahiaException ex) {
241                 throw new JahiaException ("Could not retrieve the list of sites",
242                         "Could not retrieve the list of sites",
243                         JahiaException.SERVICE_ERROR,
244                         JahiaException.ERROR_SEVERITY);
245             }
246
247             Hashtable JavaDoc cryptedComp = new Hashtable JavaDoc ();
248             JahiaSite siteItem = null;
249             String JavaDoc siteLabel = "";
250
251             while (enumeration.hasMoreElements ()) {
252                 siteItem = (JahiaSite)enumeration.nextElement ();
253
254                 // get the new component disk path for te current site
255
String JavaDoc path = JahiaWebAppsDeployerService.getNewWebAppsPath ()
256                         + File.separator
257                         + siteItem.getSiteKey ();
258
259
260                 File JavaDoc folder = new File JavaDoc (path);
261                 File JavaDoc[] files = new File JavaDoc[0];
262
263                 if (folder.isDirectory ()) {
264                     files = folder.listFiles ();
265                 }
266
267                 int size = files.length;
268                 File JavaDoc f = null;
269                 JecFile jec = null;
270
271                 // get license info
272
int jecLicenseType = 0;
273                 int jecLicenseTypeRelComp = 0;
274                 siteLabel = "[" + siteItem.getServerName () + "]";
275
276                 // decrypt Jahia Encrypted Components
277
for (int i = 0; i < size; i++) {
278                     f = files[i];
279                     if (f.isFile () && (f.getName ().toLowerCase ()).endsWith (".jec")) {
280
281                         boolean tryAgain = true;
282                         boolean goSleep = false;
283                         int status = 0;
284                         String JavaDoc fileName = siteLabel + f.getName ();
285
286                         try {
287
288                            logger.debug ("Found file " + f.getAbsolutePath ());
289
290                             jec = new JecFile (f.getAbsolutePath ());
291                             /*
292                             jecLicenseType = jec.getLicenseType();
293                             jecLicenseTypeRelComp = jec.getLicenseTypeRelComp();
294                             if ( mLicenseKey.compareLicense(jecLicenseType,
295                                                             jecLicenseTypeRelComp) ){
296                             */

297                             jec.extractFiles ();
298                             status = JefFileConstants.UNLOCKED;
299                             f.delete ();
300                             /*
301                             } else {
302                                 status = JefFileConstants.LOCKED;
303                             }
304                             jec = null;
305                             */

306                         } catch (JefFileKeyNotFoundException knf) {
307                             status = JefFileConstants.INVALID_JEF_FILE;
308                             logger.warn (knf.getMessage ());
309                             File JavaDoc tmpFile = new File JavaDoc (f.getAbsolutePath () + "_error");
310                             f.renameTo (tmpFile);
311
312                         } catch (JefFileInvalidKeyValueException ikv) {
313                             status = JefFileConstants.INVALID_JEF_FILE;
314                             logger.warn (ikv.getMessage ());
315                             File JavaDoc tmpFile = new File JavaDoc (f.getAbsolutePath () + "_error");
316                             f.renameTo (tmpFile);
317
318                         } catch (InvalidJefFileException ijf) {
319                             status = JefFileConstants.INVALID_JEF_FILE;
320                             logger.warn (ijf.getMessage ());
321                             File JavaDoc tmpFile = new File JavaDoc (f.getAbsolutePath () + "_error");
322                             f.renameTo (tmpFile);
323
324                         } catch (IOException JavaDoc ioe) {
325                             status = JefFileConstants.IO_ERROR;
326                             logger.warn (ioe.getMessage ());
327                             ioe.printStackTrace ();
328
329                         } catch (JahiaException je) {
330                             status = je.getErrorCode ();
331                             logger.warn (je.getMessage ());
332                             je.printStackTrace ();
333
334                         } catch (Throwable JavaDoc t) {
335                             status = JefFileConstants.UNKNOWN_ERROR;
336                             logger.warn (t.getMessage ());
337                             t.printStackTrace ();
338                         }
339
340
341                         cryptedComp.put (fileName, new Integer JavaDoc (status));
342                     }
343                 }
344             }
345
346             request.setAttribute ("cryptedComp", cryptedComp);
347             JahiaAdministration.doRedirect (request,
348                     response,
349                     session,
350                     JSP_PATH + "manage_jec.jsp");
351
352         } catch (JahiaException je) {
353             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
354                     jParams, jParams.getLocale ());
355             request.setAttribute ("jahiaDisplayMessage", dspMsg);
356             JahiaAdministration.doRedirect (request,
357                     response,
358                     session,
359                     JSP_PATH + "menu.jsp");
360         }
361
362     }
363
364
365 }
Popular Tags