raimund@2639: package de.intevation.flys.artifacts.services;
raimund@2639: 
christian@4656: import de.intevation.artifacts.CallMeta;
christian@4656: import de.intevation.artifacts.GlobalContext;
christian@4656: import de.intevation.artifacts.common.ArtifactNamespaceContext;
christian@4656: import de.intevation.artifacts.common.utils.FileTools;
christian@4656: import de.intevation.artifacts.common.utils.XMLUtils;
christian@4656: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
christian@4656: import de.intevation.flys.utils.FLYSUtils;
christian@4656: 
raimund@2639: import java.io.File;
raimund@2639: import java.io.FileOutputStream;
raimund@2639: import java.io.IOException;
raimund@2639: 
christian@4656: import org.apache.commons.codec.binary.Base64;
raimund@2639: import org.apache.log4j.Logger;
raimund@2639: import org.w3c.dom.Document;
raimund@2639: import org.w3c.dom.Element;
raimund@2639: 
raimund@2639: /**
raimund@2639:  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
raimund@2639:  */
raimund@2639: public class FileUploadService extends FLYSService {
raimund@2639: 
raimund@2639:     /** The logger used in this service.*/
raimund@2639:     private static Logger logger = Logger.getLogger(FileUploadService.class);
raimund@2639: 
raimund@2639:     /** XPath that points to the artifact uuid.*/
raimund@2639:     public static final String XPATH_ARTIFACT_UUID =
raimund@2639:         "/upload/artifact-uuid/text()";
raimund@2639: 
raimund@2639:     /** XPath that points to the base64 encoded data.*/
raimund@2639:     public static final String XPATH_DATA = "/upload/data/text()";
raimund@2639: 
raimund@2639:     public FileUploadService() {
raimund@2639:     }
raimund@2639: 
raimund@2639: 
raimund@2639:     @Override
raimund@2639:     protected Document doProcess(
raimund@2639:         Document data,
raimund@2639:         GlobalContext context,
raimund@2639:         CallMeta callMeta
raimund@2639:     ) {
raimund@2639:         logger.debug("FileUploadService.process");
raimund@2639: 
raimund@2639:         String uuid = extractUuid(data);
raimund@2639: 
raimund@2639:         byte[] fileData = extractData(data);
raimund@2639:         if (fileData != null) {
raimund@2639:             try {
raimund@2639:                 String shapePath = FLYSUtils.getXPathString(
christian@4656:                     FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR);
raimund@2639: 
raimund@2639:                 File artifactDir = FileTools.getDirectory(shapePath, uuid);
raimund@2639:                 FileOutputStream fos =
sascha@2641:                     new FileOutputStream(
sascha@2641:                     new File(artifactDir, "user-rgd.zip"));
sascha@2641:                 try {
sascha@2641:                     fos.write(fileData);
sascha@2641:                 }
sascha@2641:                 finally {
sascha@2641:                     fos.close();
sascha@2641:                 }
raimund@2639:             }
raimund@2639:             catch (IOException ioe) {
raimund@2639:                 logger.warn(ioe, ioe);
raimund@2639:             }
raimund@2639:         }
raimund@2639:         else {
sascha@2640:             logger.debug("No data in uploaded xml.");
raimund@2639:         }
raimund@2639: 
raimund@2639:         Document doc = XMLUtils.newDocument();
raimund@2639:         ElementCreator ec = new ElementCreator(doc, null, null);
raimund@2639:         Element resp = ec.create("response");
raimund@2639:         doc.appendChild(resp);
raimund@2639: 
raimund@2639:         return doc;
raimund@2639:     }
raimund@2639: 
raimund@2639: 
raimund@2639:     protected String extractUuid(Document data) {
raimund@2639:         return XMLUtils.xpathString(
raimund@2639:             data, XPATH_ARTIFACT_UUID, ArtifactNamespaceContext.INSTANCE);
raimund@2639:     }
raimund@2639: 
raimund@2639: 
raimund@2639:     protected byte[] extractData(Document data) {
raimund@2639:         String b64Data = XMLUtils.xpathString(
raimund@2639:             data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE);
raimund@2639: 
sascha@3084:         if (b64Data != null && b64Data.length() > 0) {
raimund@2639:            byte[] fileData = Base64.decodeBase64(b64Data);
raimund@2639:            return fileData;
raimund@2639:         }
raimund@2639:         return null;
raimund@2639:     }
raimund@2639: }
sascha@3083: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :