UploadManager.java
4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package it.softecspa.fileproxy.proxyservices.manager;
import it.softecspa.fileproxy.proxyservices.request.UploadRequestType;
import it.softecspa.fileproxy.proxyservices.response.UploadResponseType;
import it.softecspa.fileproxy.services.common.CheckerException;
import it.softecspa.fileproxy.services.common.ManagerException;
import it.softecspa.fileproxy.services.common.ResponseOutcome;
import it.softecspa.kahuna.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class UploadManager extends AbstractHttpFileProxyManager<UploadRequestType, UploadResponseType> {
private File file_to_upload;
@Override
protected void validateRequest() throws CheckerException, ManagerException {
// Non si possono sovrascrivere le cartelle
checkRequestMandatory("fileName");
checkWildecard(request.getPath());
String path = normalizePath(request.getPath());
checkRequestIfNullSetDefault("overwrite", Boolean.FALSE);
checkRequestIfNullSetDefault("append", Boolean.FALSE);
checkRequestIfNullSetDefault("bufferLength", 4096);
checkWildecard(request.getFileName());
file_to_upload = new File (path, request.getFileName());
if (file_to_upload.exists()) {
if (!request.getOverwrite().booleanValue() && !request.getAppend().booleanValue()) {
log.warn("File just not exist: '"+file_to_upload.getAbsolutePath()+"', overwrite or append are FALSE");
throw new CheckerException(ResponseOutcome.FILE_JUST_EXIST);
}
if (file_to_upload.isDirectory()) {
log.warn("File just not exist: '"+file_to_upload.getAbsolutePath()+"', but is a directory");
throw new CheckerException(ResponseOutcome.FILE_IS_DIRECTORY);
}
}
}
@Override
protected void doService(UploadRequestType request, UploadResponseType response) throws ManagerException {
log.info("Request UPLOAD file '"+ file_to_upload.getAbsolutePath()+"'"
+ (request.getOverwrite().booleanValue()?" (overwrite = TRUE)":"")
+ (request.getAppend().booleanValue()?" (append = TRUE)":"")
+ " (bufferLength = "+request.getBufferLength()+") ");
// Prendo input stream e lo salvo su un file (prima temporaneo)
File file_temp = new File(file_to_upload.getAbsoluteFile()+".tmp");
// Provo a cancellare il file temporaneo
file_temp.delete();
if (request.getAppend().booleanValue()) {
// Devo fare una copia del file per farlo diventara temporaneo
if (file_to_upload.exists()) {
// ...naturalmente se il file esite!
try {
log.info("Creating temporay copy of file '"+ file_to_upload.getAbsolutePath()+"'");
file_to_upload.copyTo(file_temp);
} catch (IOException e) {
throw new ManagerException(new CheckerException(ResponseOutcome.ERROR_COPY_FILE_TEMP));
}
}
}
InputStream input = request.takeInputStream();
OutputStream output = null;
int BUFFER_LENGTH = request.getBufferLength();
try {
output = new FileOutputStream(file_temp, request.getAppend().booleanValue());
byte[] buffer = new byte[BUFFER_LENGTH];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
output.flush();
}
} catch (FileNotFoundException e) {
log.error("File '"+file_temp.getAbsolutePath()+"' not found");
throw new ManagerException(new CheckerException(ResponseOutcome.FILE_NOT_FOUND));
} catch (IOException e) {
log.error("IOException writing file '"+file_temp.getAbsolutePath()+"'", e);
throw new ManagerException(e);
} finally {
if (output!=null) {
try {
output.close();
} catch (IOException e) {
log.error("IOException close file '"+file_temp.getAbsolutePath()+"'",e);
}
}
}
log.info("Copy completed, rename temp file to '"+ file_to_upload.getAbsolutePath()+"'");
if (request.getOverwrite().booleanValue()) {
if (file_to_upload.exists() && !file_to_upload.delete()) {
log.info("Impossibility to delete file '"+ file_to_upload.getAbsolutePath()+"' with override flag");
}
} else if (request.getAppend().booleanValue()) {
if (file_to_upload.exists() && !file_to_upload.delete()) {
log.info("Impossibility to delete old file '"+ file_to_upload.getAbsolutePath()+"' with append flag");
}
}
file_temp.renameTo(file_to_upload);
// Risposta POSITIVA
new ResponseBuilder(response).setReturn(ResponseOutcome.OK);
}
}