1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.rest;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Date;
28  import java.util.Map;
29  import java.util.Map.Entry;
30  import java.util.TreeMap;
31  
32  import javax.ws.rs.Consumes;
33  import javax.ws.rs.GET;
34  import javax.ws.rs.POST;
35  import javax.ws.rs.Path;
36  
37  import org.efaps.admin.AppConfigHandler;
38  import org.efaps.update.FileType;
39  import org.efaps.update.Install;
40  import org.efaps.update.util.InstallationException;
41  import org.efaps.util.EFapsException;
42  import org.efaps.util.cache.CacheReloadException;
43  import org.glassfish.jersey.media.multipart.BodyPart;
44  import org.glassfish.jersey.media.multipart.BodyPartEntity;
45  import org.glassfish.jersey.media.multipart.MultiPart;
46  import org.glassfish.jersey.media.multipart.MultiPartMediaTypes;
47  
48  /**
49   * Rest API to update files in eFaps.
50   *
51   * @author The eFaps Team
52   * @version $Id$
53   */
54  @Path("/update")
55  public class Update
56      extends AbstractRest
57  {
58      /**
59       * Name of the folder inside the "official" temporary folder.
60       */
61      public static final String TMPFOLDERNAME = "eFapsUpdate";
62  
63      /**
64       * Called on post to consume files used to update.
65       *
66       * @param _multiPart Mulitpart containing the update files
67       */
68      @POST
69      @Consumes(MultiPartMediaTypes.MULTIPART_MIXED)
70      public void updateFromFile(final MultiPart _multiPart)
71      {
72          try {
73              if (hasAccess()) {
74                  AbstractRest.LOG.info("===Start of Update via REST===");
75                  File tmpfld = AppConfigHandler.get().getTempFolder();
76                  if (tmpfld == null) {
77                      final File temp = File.createTempFile("eFaps", ".tmp");
78                      tmpfld = temp.getParentFile();
79                      temp.delete();
80                  }
81                  final File updateFolder = new File(tmpfld, Update.TMPFOLDERNAME);
82                  if (!updateFolder.exists()) {
83                      updateFolder.mkdirs();
84                  }
85                  final File dateFolder = new File(updateFolder, ((Long) new Date().getTime()).toString());
86                  dateFolder.mkdirs();
87  
88                  final Map<File, FileType> files = new TreeMap<File, FileType>();
89                  for (final BodyPart part : _multiPart.getBodyParts()) {
90                      final BodyPartEntity entity = (BodyPartEntity) part.getEntity();
91                      final InputStream in = entity.getInputStream();
92  
93                      final File file = new File(dateFolder, part.getContentDisposition().getFileName());
94  
95                      final FileOutputStream out = new FileOutputStream(file);
96                      final byte[] buf = new byte[1024];
97                      int len;
98                      while ((len = in.read(buf)) > 0) {
99                          out.write(buf, 0, len);
100                     }
101                     out.close();
102                     in.close();
103 
104                     final String ending = file.getName().substring(file.getName().lastIndexOf(".") + 1);
105 
106                     final FileType filetype = FileType.getFileTypeByExensione(ending);
107 
108                     AbstractRest.LOG.info("= Receieved: '{}'", file.getName());
109                     if (filetype != null) {
110                         files.put(file, filetype);
111                     }
112                 }
113                 if (!files.isEmpty()) {
114                     final Install install = new Install(true);
115                     for (final Entry<File, FileType> entry : files.entrySet()) {
116                         AbstractRest.LOG.info("...Adding to Update: '{}' ", entry.getKey().getName());
117                         install.addFile(entry.getKey().toURI().toURL(), entry.getValue().getType());
118                     }
119                     install.updateLatest(null);
120                 }
121                 AbstractRest.LOG.info("===End of Update via REST===");
122             }
123         } catch (final IOException e) {
124             AbstractRest.LOG.error("IOException", e);
125         } catch (final InstallationException e) {
126             AbstractRest.LOG.error("InstallationException", e);
127         } catch (final CacheReloadException e) {
128             AbstractRest.LOG.error("CacheReloadException", e);
129         } catch (final EFapsException e) {
130             AbstractRest.LOG.error("EFapsException", e);
131         }
132     }
133 
134     /**
135      * @return not implemented.
136      */
137     @GET
138     public String test()
139     {
140         String ret = "";
141         try {
142             if (hasAccess()) {
143                 ret = "not implemented yet";
144             }
145         } catch (final CacheReloadException e) {
146             AbstractRest.LOG.error("CacheReloadException", e);
147         } catch (final EFapsException e) {
148             AbstractRest.LOG.error("EFapsException", e);
149         }
150         return ret;
151     }
152 }