Author: echatellier Date: 2014-06-12 18:30:47 +0200 (Thu, 12 Jun 2014) New Revision: 203 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/203 Log: Ajout du support des actions de type json Added: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonAction.java trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonResultSupport.java Modified: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailActionSupport.java trunk/faxtomail-ui-web/src/main/resources/struts.xml trunk/faxtomail-ui-web/src/main/webapp/WEB-INF/decorators.xml Modified: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailActionSupport.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailActionSupport.java 2014-06-12 16:12:04 UTC (rev 202) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailActionSupport.java 2014-06-12 16:30:47 UTC (rev 203) @@ -63,7 +63,7 @@ protected FaxToMailSession session; - protected Gson gson; + protected static Gson gson; public void setApplicationConfig(FaxToMailConfiguration applicationConfig) { this.applicationConfig = applicationConfig; @@ -103,7 +103,7 @@ return result; } - public Gson getGson() { + public static Gson getGson() { if (gson == null) { GsonBuilder builder = new GsonBuilder(); Added: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonAction.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonAction.java (rev 0) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonAction.java 2014-06-12 16:30:47 UTC (rev 203) @@ -0,0 +1,49 @@ +package com.franciaflex.faxtomail.web; + +/* + * #%L + * FaxToMail :: Web + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2014 Franciaflex, Code Lutin + * %% + * This program 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. + * + * This program 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 this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import org.apache.struts2.convention.annotation.Result; +import org.apache.struts2.convention.annotation.Results; + + +/** + * Abstract action used to render custom objects as json string using gson directly in response output stream. + * + * @author Eric Chatellier + */ +@Results({ + @Result(type = "faxtomail-json", name = "success") +}) +public abstract class FaxToMailJsonAction extends FaxToMailActionSupport { + + /** + * Method to override to get object data to render as json. Method HAS to be public because result support will use + * this method. + * + * @return object to render as json + */ + public abstract Object getJsonData(); + +} Property changes on: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonAction.java ___________________________________________________________________ Added: svn:eol-style + native Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonResultSupport.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonResultSupport.java (rev 0) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonResultSupport.java 2014-06-12 16:30:47 UTC (rev 203) @@ -0,0 +1,74 @@ +package com.franciaflex.faxtomail.web; + +/* + * #%L + * FaxToMail :: Web + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2014 Franciaflex, Code Lutin + * %% + * This program 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. + * + * This program 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 this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import java.io.IOException; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.struts2.dispatcher.StrutsResultSupport; + +import com.google.common.base.Charsets; +import com.opensymphony.xwork2.ActionInvocation; + +public class FaxToMailJsonResultSupport extends StrutsResultSupport { + + private static final Log log = LogFactory.getLog(FaxToMailJsonResultSupport.class); + + @Override + protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { + Object jsonData = invocation.getStack().findValue("jsonData"); + + String json = FaxToMailActionSupport.getGson().toJson(jsonData); + + // Work-arround for IE to not display download dialog for json result + // see https://github.com/blueimp/jQuery-File-Upload/issues/1795 + HttpServletRequest servletRequest = (HttpServletRequest) invocation.getInvocationContext().get(HTTP_REQUEST); + HttpServletResponse servletResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE); + servletResponse.setCharacterEncoding(Charsets.UTF_8.name()); + if (servletRequest.getHeader("accept").indexOf("application/json") != -1) { + servletResponse.setContentType("application/json"); + } else { + // IE workaround + servletResponse.setContentType("text/plain"); + } + + try { + ServletOutputStream outputStream = servletResponse.getOutputStream(); + byte[] jsonBytes = json.getBytes(Charsets.UTF_8); // On transforme en bytes pour assurer l'encodage + outputStream.write(jsonBytes); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Unable to write JSON output into Servlet Response"); + } + } + + } + +} Property changes on: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailJsonResultSupport.java ___________________________________________________________________ Added: svn:eol-style + native Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/faxtomail-ui-web/src/main/resources/struts.xml =================================================================== --- trunk/faxtomail-ui-web/src/main/resources/struts.xml 2014-06-12 16:12:04 UTC (rev 202) +++ trunk/faxtomail-ui-web/src/main/resources/struts.xml 2014-06-12 16:30:47 UTC (rev 203) @@ -35,6 +35,11 @@ <package name="faxtomail" namespace="/" extends="struts-default"> + <result-types> + <result-type name="faxtomail-json" default="false" + class="com.franciaflex.faxtomail.web.FaxToMailJsonResultSupport" /> + </result-types> + <interceptors> <interceptor name="faxToMailInterceptor" class="com.franciaflex.faxtomail.web.FaxToMailInterceptor"/> <interceptor name="loginInterceptor" class="com.franciaflex.faxtomail.web.FaxToMailLoginInterceptor" /> Modified: trunk/faxtomail-ui-web/src/main/webapp/WEB-INF/decorators.xml =================================================================== --- trunk/faxtomail-ui-web/src/main/webapp/WEB-INF/decorators.xml 2014-06-12 16:12:04 UTC (rev 202) +++ trunk/faxtomail-ui-web/src/main/webapp/WEB-INF/decorators.xml 2014-06-12 16:30:47 UTC (rev 203) @@ -1,10 +1,10 @@ <!-- #%L - Extranet ENC-AHI :: Web + FaxToMail :: Web $Id$ $HeadURL$ %% - Copyright (C) 2013 Ministère des Affaires sociales et de la Santé + Copyright (C) 2014 Franciaflex, Code Lutin %% This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -24,13 +24,10 @@ <decorators defaultdir="/WEB-INF/decorators"> <excludes> <pattern>/css/*</pattern> - <pattern>/img/*</pattern> + <pattern>/js/*</pattern> + <pattern>*-json*</pattern> </excludes> - <!--<decorator name="layout-login" page="layout-login.jsp">--> - <!--<pattern>/authentication/login*</pattern>--> - <!--</decorator>--> - <decorator name="layout" page="layout.jsp"> <pattern>/*</pattern> </decorator>