1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
50
51
52
53
54 @Path("/update")
55 public class Update
56 extends AbstractRest
57 {
58
59
60
61 public static final String TMPFOLDERNAME = "eFapsUpdate";
62
63
64
65
66
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
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 }