|
@@ -1,290 +0,0 @@
|
|
|
-package commons.utility;
|
|
|
|
|
-
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
-import java.lang.reflect.Field;
|
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.StringTokenizer;
|
|
|
|
|
-
|
|
|
|
|
-import jakarta.servlet.ServletContext;
|
|
|
|
|
-import jakarta.servlet.http.Cookie;
|
|
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
-
|
|
|
|
|
-import org.hibernate.Hibernate;
|
|
|
|
|
-
|
|
|
|
|
-import commons.exception.ZSystemException;
|
|
|
|
|
-import commons.utility.database.NullValue;
|
|
|
|
|
-import commons.exception.ZCryptoException;
|
|
|
|
|
-import commons.utility.field.BinaryBooleanWebField;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-public class WebUtility
|
|
|
|
|
-{
|
|
|
|
|
- public static String riceviRealPath(ServletContext context, String percorsoFileSystemCondiviso)
|
|
|
|
|
- {
|
|
|
|
|
- if (percorsoFileSystemCondiviso != null && !percorsoFileSystemCondiviso.equalsIgnoreCase(NullValue.STRING))
|
|
|
|
|
- return percorsoFileSystemCondiviso;
|
|
|
|
|
-
|
|
|
|
|
- String returnValue = context.getRealPath("/");
|
|
|
|
|
- if (null == returnValue)
|
|
|
|
|
- return NullValue.STRING;
|
|
|
|
|
- if (!String.valueOf(returnValue.charAt(returnValue.length() - 1)).equalsIgnoreCase(File.separator))
|
|
|
|
|
- returnValue += File.separator;
|
|
|
|
|
- return returnValue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String riceviRealPathHBM(ServletContext context)
|
|
|
|
|
- {
|
|
|
|
|
- String returnValue = context.getRealPath("/");
|
|
|
|
|
- if (!String.valueOf(returnValue.charAt(returnValue.length() - 1)).equalsIgnoreCase(File.separator))
|
|
|
|
|
- returnValue += File.separator;
|
|
|
|
|
- return returnValue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static void clearAuthCookie(HttpServletRequest req, HttpServletResponse res, String cookieName)
|
|
|
|
|
- {
|
|
|
|
|
- //cancellazione eventuale cookie di autenticazione
|
|
|
|
|
- Cookie[] cookies = req.getCookies();
|
|
|
|
|
- if (cookies != null)
|
|
|
|
|
- {
|
|
|
|
|
- for (int i = 0; i < cookies.length; i++)
|
|
|
|
|
- {
|
|
|
|
|
- Cookie thisCookie = cookies[i];
|
|
|
|
|
- if (thisCookie.getName().equals(cookieName))
|
|
|
|
|
- {
|
|
|
|
|
- thisCookie.setValue("");
|
|
|
|
|
- thisCookie.setMaxAge(0);
|
|
|
|
|
- res.addCookie(thisCookie);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- public static void writeAuthCookie(HttpServletResponse res, String cf, String cookieName) throws ZCryptoException
|
|
|
|
|
- {
|
|
|
|
|
- Cookie ck = new Cookie(cookieName, Crypto.encrypt(cf));
|
|
|
|
|
- res.addCookie(ck);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Cancella un cookie
|
|
|
|
|
- * @param res
|
|
|
|
|
- * @param cf
|
|
|
|
|
- * @param cookieName - nome del cookie
|
|
|
|
|
- * @param percoso
|
|
|
|
|
- * @throws ZCryptoException
|
|
|
|
|
- */
|
|
|
|
|
- public static void writeAuthCookie(HttpServletResponse res, String cf, String cookieName, String path) throws ZCryptoException
|
|
|
|
|
- {
|
|
|
|
|
- Cookie ck = new Cookie(cookieName, Crypto.encrypt(cf));
|
|
|
|
|
- ck.setPath(path);
|
|
|
|
|
- res.addCookie(ck);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Cancella un cookie
|
|
|
|
|
- * @param res
|
|
|
|
|
- * @param cf
|
|
|
|
|
- * @param cookieName - nome del cookie
|
|
|
|
|
- * @throws ZCryptoException
|
|
|
|
|
- */
|
|
|
|
|
- public static void deleteCookie(HttpServletRequest req, HttpServletResponse res, String cookieName)
|
|
|
|
|
- {
|
|
|
|
|
- //cancellazione eventuale cookie di autenticazione
|
|
|
|
|
- Cookie[] cookies = req.getCookies();
|
|
|
|
|
- if (cookies != null)
|
|
|
|
|
- {
|
|
|
|
|
- for (int i = 0; i < cookies.length; i++)
|
|
|
|
|
- {
|
|
|
|
|
- Cookie thisCookie = cookies[i];
|
|
|
|
|
- if (thisCookie.getName().equals(cookieName))
|
|
|
|
|
- {
|
|
|
|
|
- thisCookie.setValue("");
|
|
|
|
|
- thisCookie.setMaxAge(0);
|
|
|
|
|
- res.addCookie(thisCookie);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Legge un cookie con propriet? VALUE CRIPTATA. Il cookie viene ritornato con la propriet? VALUE DECRIPTATO
|
|
|
|
|
- * @param req
|
|
|
|
|
- * @param cookieName
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public static Cookie readCookie(HttpServletRequest req, String cookieName)
|
|
|
|
|
- {
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- Cookie[] cookies = req.getCookies();
|
|
|
|
|
- if (cookies != null)
|
|
|
|
|
- {
|
|
|
|
|
- for (int i = 0; i < cookies.length; i++)
|
|
|
|
|
- {
|
|
|
|
|
- Cookie thisCookie = cookies[i];
|
|
|
|
|
- if (thisCookie.getName().equals(cookieName))
|
|
|
|
|
- {
|
|
|
|
|
- //thisCookie.setValue(Crypto.decrypt(thisCookie.getValue()));
|
|
|
|
|
- return new CryptedCookie(thisCookie);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- catch (Exception ex)
|
|
|
|
|
- {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Scrive un cookie CRIPTANDONE la propriet? value
|
|
|
|
|
- * @param res
|
|
|
|
|
- * @param cookieName
|
|
|
|
|
- * @param testo
|
|
|
|
|
- * @throws ZCryptoException
|
|
|
|
|
- */
|
|
|
|
|
- public static void writeCookie(HttpServletResponse res, String cookieName, String testo) throws ZCryptoException
|
|
|
|
|
- {
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- CryptedCookie cryptoC = new CryptedCookie(cookieName, testo);
|
|
|
|
|
- cryptoC.setMaxAge(31536000);
|
|
|
|
|
- Cookie writeCk = new Cookie(cookieName, cryptoC.getCk().getValue());
|
|
|
|
|
- writeCk.setMaxAge(cryptoC.getMaxAge());
|
|
|
|
|
- res.addCookie(writeCk);
|
|
|
|
|
- }
|
|
|
|
|
- catch (Exception ex)
|
|
|
|
|
- {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Campo di comodo per le conversioni
|
|
|
|
|
- private static BinaryBooleanWebField booleanConv = new BinaryBooleanWebField(null,false);
|
|
|
|
|
- /** Converte un valore string nel relativo valore boolean
|
|
|
|
|
- * @param value Il valore del boolean in formato stringa 0/1
|
|
|
|
|
- * @return Il valore boolean (false come default)
|
|
|
|
|
- */
|
|
|
|
|
- public final static boolean convertBinaryBoolean(final String value)
|
|
|
|
|
- {
|
|
|
|
|
- booleanConv.setFormattedText(value);
|
|
|
|
|
- return booleanConv.getBoolean();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /** Inizializza le properties di una rootEntity mappata con Hibernate
|
|
|
|
|
- * @param <T>
|
|
|
|
|
- * @param rootEntity L'entit? connessa alla sessione
|
|
|
|
|
- * @param properties La lista di property nella forma prop1.prop11.prop111,prop2.prop22... etc
|
|
|
|
|
- * @return L'oggetto con le property specificate inizializzate!
|
|
|
|
|
- */
|
|
|
|
|
- public final static <T> T initEntityProperties(final T rootEntity,final String...properties)
|
|
|
|
|
- {
|
|
|
|
|
- if(null != rootEntity)
|
|
|
|
|
- {
|
|
|
|
|
- for(final String property : properties)
|
|
|
|
|
- {
|
|
|
|
|
- Object currentProperty = rootEntity;
|
|
|
|
|
- {
|
|
|
|
|
- final StringTokenizer strtok = new StringTokenizer(property,".");
|
|
|
|
|
- while(strtok.hasMoreTokens())
|
|
|
|
|
- {
|
|
|
|
|
- // Verifica se la property ? stata inizializzata!
|
|
|
|
|
- final String token = strtok.nextToken();
|
|
|
|
|
- // L'oggetto corrente va letto!
|
|
|
|
|
- final String getMethodName = "get" + token.substring(0,1).toUpperCase() + token.substring(1);
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- final Method getMethod = currentProperty.getClass().getMethod(getMethodName);
|
|
|
|
|
- boolean getMethodAccessible = getMethod.isAccessible();
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- getMethod.setAccessible(true);
|
|
|
|
|
- currentProperty = getMethod.invoke(currentProperty);
|
|
|
|
|
- }
|
|
|
|
|
- catch (Throwable e)
|
|
|
|
|
- {
|
|
|
|
|
- throw new ZSystemException(e);
|
|
|
|
|
- }
|
|
|
|
|
- finally
|
|
|
|
|
- {
|
|
|
|
|
- getMethod.setAccessible(getMethodAccessible);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- catch (SecurityException e)
|
|
|
|
|
- {
|
|
|
|
|
- throw new ZSystemException(e);
|
|
|
|
|
- }
|
|
|
|
|
- catch (NoSuchMethodException e)
|
|
|
|
|
- {
|
|
|
|
|
- // Accesso tramite field!
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- Field propertyField = currentProperty.getClass().getDeclaredField(token);
|
|
|
|
|
- boolean propertyFieldAccessible = propertyField.isAccessible();
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- propertyField.setAccessible(true);
|
|
|
|
|
- currentProperty = propertyField.get(currentProperty);
|
|
|
|
|
- }
|
|
|
|
|
- catch (Throwable e2)
|
|
|
|
|
- {
|
|
|
|
|
- throw new ZSystemException(e2);
|
|
|
|
|
- }
|
|
|
|
|
- finally
|
|
|
|
|
- {
|
|
|
|
|
- propertyField.setAccessible(propertyFieldAccessible);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- catch (SecurityException e1)
|
|
|
|
|
- {
|
|
|
|
|
- throw new ZSystemException(e);
|
|
|
|
|
- }
|
|
|
|
|
- catch (NoSuchFieldException e1)
|
|
|
|
|
- {
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Se non ? stata inizializzata...
|
|
|
|
|
- if(!Hibernate.isInitialized(currentProperty))
|
|
|
|
|
- {
|
|
|
|
|
- // allora ? da fare!
|
|
|
|
|
- Hibernate.getClass(currentProperty);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return rootEntity;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /** Inizializza le properties di una lista di entity mappate con Hibernate
|
|
|
|
|
- * @param <T>
|
|
|
|
|
- * @param rootEntities la lista di oggetto
|
|
|
|
|
- * @param properties la lista di properties da inizializzare
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public final static <T> List<T> initEntitiesProperties(final List<T> rootEntities,final String...properties)
|
|
|
|
|
- {
|
|
|
|
|
- if(null != rootEntities)
|
|
|
|
|
- {
|
|
|
|
|
- for(final T rootEntity : rootEntities)
|
|
|
|
|
- initEntityProperties(rootEntity, properties);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return rootEntities;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String traduciPathPerJsp(String path)
|
|
|
|
|
- {
|
|
|
|
|
- return path.replaceAll("\\\\", "\\\\\\\\") ;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|