/taurus-web/src/org/taurus/web/gwt/server/BaseServiceRemote.java
Java | 264 lines | 118 code | 46 blank | 100 comment | 16 complexity | 3e8610f3e4cb35710a7a266c25ebebb6 MD5 | raw file
- /*
- * This file is part of Taurus
- *
- * Taurus is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * at your option) any later version.
- *
- * Taurus is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Melenti. If not, see <http://www.gnu.org/licenses/>.
- */
-
- package org.taurus.web.gwt.server;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.commons.beanutils.BeanUtils;
- import org.apache.log4j.Logger;
- import org.taurus.services.ServiceAdministrator;
- import org.taurus.services.ServiceProject;
- import org.taurus.services.ServiceSecurity;
-
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gwt.user.server.rpc.RemoteServiceServlet;
- import com.thoughtworks.xstream.XStream;
- import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
-
- /**
- * Incluir aquí la descripcion de la clase.
- *
- * @version $Revision: 1.0 $ [17/07/2008]
- * @author henry molina <a href="mailto:henrymolinanoboa@gmail.com">taurus</a>
- */
-
- public class BaseServiceRemote extends RemoteServiceServlet {
-
- private static final long serialVersionUID = 4394294482171221329L;
-
- protected static ServiceSecurity serviceSecurity;
-
- protected static ServiceAdministrator serviceAdministrator;
-
- protected static ServiceProject serviceProject;
-
- protected static XStream xstream;
-
- protected static Gson gson = null;
-
- private static Logger log = Logger.getLogger(BaseServiceRemote.class);
-
- /**
- *
- */
-
- public BaseServiceRemote() {
- super();
- if (serviceSecurity == null) {
- serviceSecurity = ServiceSecurity.Util.getInstance();
- }
- if (serviceAdministrator == null) {
- serviceAdministrator = ServiceAdministrator.Util.getInstance();
- }
- if (serviceProject == null) {
- serviceProject = ServiceProject.Util.getInstance();
- }
- if (xstream == null) {
- xstream = new XStream(new JettisonMappedXmlDriver());
-
- }
-
- if (gson == null) {
- final GsonBuilder gsonBuilder = new GsonBuilder();
- gsonBuilder.setDateFormat("dd/MM/yyyy");
- gson = gsonBuilder.excludeFieldsWithoutExposeAnnotation().create();
- }
-
- }
-
- /**
- * Incluir aquí la descripcion del metodo.
- *
- * @param json
- * @return
- */
-
- @SuppressWarnings("unchecked")
- protected synchronized Object fromJson(final String json, Class clazz) {
- Object obj = gson.fromJson(json, clazz);
- if (log.isDebugEnabled()) {
- try {
- BeanUtils.describe(obj);
- } catch (Exception e) {
- log.error(e);
- }
- }
- return obj;
- }
-
- @SuppressWarnings("unchecked")
- protected Object getAttributeContextHttp(final Class clazz) {
- return getThreadLocalRequest().getSession(false).getServletContext().getAttribute(clazz.getName());
- }
-
- /**
- * Incluir aqui la descripcion del metodo.
- *
- * @param name
- * @return
- */
-
- protected Object getAttributeContextHttp(final String name) {
- return getThreadLocalRequest().getSession(false).getServletContext().getAttribute(name);
- }
-
- /**
- * Recupera un objeto desde el request http
- *
- * @param clazz
- * @return
- */
-
- protected Object getAttributeRequestHttp(final Class<Object> clazz) {
- Object obj = getThreadLocalRequest().getAttribute(clazz.getName());
- if (obj == null) {
- obj = getThreadLocalRequest().getParameter(clazz.getName());
- }
- return obj;
- }
-
- /**
- * Recupera un objeto desde el session http
- *
- * @param clazz
- * @return
- */
-
- protected Object getAttributeSessionHttp(final Class clazz) {
- return getThreadLocalRequest().getSession(false).getAttribute(clazz.getName());
- }
-
- /**
- * Incluir aquí la descripcion del metodo.
- *
- * @param name
- * @return
- */
-
- protected Object getAttributeSessionHttp(final String name) {
- return getThreadLocalRequest().getSession(false).getAttribute(name);
- }
-
- protected void invalidateSessionUser() {
- getThreadLocalRequest().getSession().invalidate();
- }
-
- /**
- *
- * @param name
- */
- protected void removeAttributeSessionHttp(String name) {
- getThreadLocalRequest().getSession(false).removeAttribute(name);
- }
-
- protected void resetSessionUser() {
- getThreadLocalRequest().getSession().invalidate();
- getThreadLocalRequest().getSession(true);
- }
-
- /**
- * Establece el valor del atributo usando como nombre el fcn de la clase del objeto pasado como valor
- *
- * @param value
- */
- protected void setAttributeContextHttp(Object value) {
- getThreadLocalRequest().getSession(false).getServletContext().setAttribute(value.getClass().getName(), value);
- }
-
- /**
- * Estblece el valor de una variable de contexto de la aplicación
- *
- * @param name
- * el nombre del atributo
- * @param value
- * el valor que va a tener el atributo
- */
- protected void setAttributeContextHttp(final String name, Object value) {
- getThreadLocalRequest().getSession(false).getServletContext().setAttribute(name, value);
- }
-
- /**
- * Guarda un objeto en el request http
- *
- * @param object
- */
-
- protected void setAttributeRequestHttp(final Object object) {
- getThreadLocalRequest().setAttribute(object.getClass().getName(), object);
- }
-
- /**
- * Guarda un objeto en el session http
- *
- * @param object
- */
-
- protected void setAttributeSessionHttp(final Object object) {
- getThreadLocalRequest().getSession(false).setAttribute(object.getClass().getName(), object);
- }
-
- /**
- * Incluir aquí la descripcion del metodo.
- *
- * @param name
- * @param object
- */
-
- protected void setAttributeSessionHttp(final String name, final Object object) {
- getThreadLocalRequest().getSession(false).setAttribute(name, object);
- }
-
- /**
- * transforma un objeto a formato json
- *
- * @param object
- * @return
- */
-
- protected String toJson(Object object) {
- xstream.alias(object.getClass().getSimpleName().substring(0, 1).toLowerCase()
- + object.getClass().getSimpleName().substring(1), object.getClass());
- return xstream.toXML(object);
-
- }
-
- /**
- * Incluir aquí la descripcion del metodo.
- *
- * @param object
- * @return
- */
-
- protected synchronized String toJson2(final Object object) {
- String json = null;
- if (object instanceof ArrayList) {
- json = gson.toJson(object);
- } else {
- List<Object> list = new ArrayList<Object>();
- list.add(object);
- json = gson.toJson(list);
- }
- if (log.isDebugEnabled()) {
- log.debug(json);
- }
- return json;
- }
-
- }