--- /dev/null
+/*******************************************************************************
+ * Copyright
+ * 2013 axYus - www.axyus.com
+ * 2013 P.Renier - paul.renier@axyus.com
+ *
+ * This file is part of XEMELIOS.
+ *
+ * XEMELIOS is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XEMELIOS 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with XEMELIOS; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *******************************************************************************/
+package fr.gouv.finances.dgfip.xemelios.web2.navigate;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLEncoder;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
+
+import fr.gouv.finances.dgfip.xemelios.common.NetAccess;
+import fr.gouv.finances.dgfip.xemelios.data.DataImpl;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.web2.app.XemeliosHelper;
+import fr.gouv.finances.dgfip.xemelios.web2.utils.StdDispatchAction;
+
+/**
+ * @author paul.renier
+ *
+ */
+public class PjAtlasAction extends StdDispatchAction {
+
+ @Override
+ protected ActionForward unspecified(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
+ final DataImpl dataLayer = DataLayerManager.getImplementation();
+ final String urlPjAtlasParameter = dataLayer.getParameterValue("url.pj.atlas");
+ final String contRep = request.getParameter("contRep");
+ final String docId = request.getParameter("docId");
+
+ // ----------- controle --------------
+ if (StringUtils.isBlank(urlPjAtlasParameter)) {
+ throw new RuntimeException("Atlas URL not set");
+ } else if (!this.isURL(urlPjAtlasParameter)) {
+ throw new RuntimeException("Parameter 'url.pj.atlas' is not an URL");
+ }
+
+ if (StringUtils.isBlank(contRep)) {
+ throw new IllegalArgumentException("Le paramètre contRep n'est pas spécifié");
+ } else if (contRep.length() != 2) {
+ throw new IllegalArgumentException(contRep + " n'est pas un compte de repositionnement valide !");
+ }
+
+ if (StringUtils.isBlank(docId)) {
+ throw new IllegalArgumentException("Le paramètre docId n'est pas spécifié");
+ } else if (docId.length() != 32) {
+ throw new IllegalArgumentException(docId + " n'est pas un Id de PJ valide !");
+ }
+
+ // -----------------------------------------------------
+
+ final String url = urlPjAtlasParameter + "?contRep=" + URLEncoder.encode(contRep, response.getCharacterEncoding()) + "&docId=" + URLEncoder.encode(docId, response.getCharacterEncoding()) + "&applyXsl=true";
+ final HttpClient client = NetAccess.getHttpClient(XemeliosHelper.getApplicationProperties());
+ final GetMethod method = new GetMethod(url);
+ final int httpRet = client.executeMethod(method);
+ final boolean atlasError = ((httpRet < 200) || (httpRet >= 300));
+
+ if (atlasError) {
+ request.setAttribute("httpResponseCode", httpRet);
+ request.setAttribute("httpResponseMessage", (httpRet == 404 ? "PJ introuvable" : method.getResponseBodyAsString()));
+ } else {
+ final ServletOutputStream sos = response.getOutputStream();
+ for (final Header head : method.getResponseHeaders()) {
+ response.setHeader(head.getName(), head.getValue());
+ }
+
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + docId + "\"");
+
+ final InputStream is = method.getResponseBodyAsStream();
+ try {
+ IOUtils.copy(is, sos);
+ } finally {
+ is.close();
+ }
+ }
+
+ return (atlasError ? mapping.findForward("atlas.error") : null);
+ }
+
+ private boolean isURL(final String url) {
+ boolean result = true;
+
+ try {
+ new URL(url);
+ } catch (final Exception e) {
+ result = false;
+ }
+
+ return result;
+ }
+}