litesoft
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
package org.litesoft.sandbox.csapp.server;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
public class UploadSmallFileReflectorServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public static final int MAX_FILE_SIZE = 16 * 1024;
private static final char[] SEPARATOR_OPTIONS = {'-', '+', '=', '*'};
/**
* Upload uses POST.
*/
@Override
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
PrintWriter zPrintWriter = response.getWriter();
response.setContentType( "text/html" );
try
{
insureCharacterEncoding( request );
FileNameDataPair results = parse( request );
reflectSuccess( zPrintWriter, results );
}
catch ( Exception e )
{
reflectError( zPrintWriter, e );
}
finally
{
zPrintWriter.flush();
}
}
private void reflectSuccess( PrintWriter writer, FileNameDataPair results )
{
writer.println( "Success: " + results.getName() );
String separator = determineLineSeparator( results.getData() );
writer.println( separator );
writer.println( results.getData() );
writer.println( separator );
}
private void reflectError( PrintWriter writer, Exception exception )
{
// LOGGER.warn.log( exception );
writer.println( "Internal Error: " + exception.getMessage() );
exception.printStackTrace( writer );
}
private FileNameDataPair parse( HttpServletRequest request )
throws IOException, FileUploadException
{
// Check that we have a file upload request
if ( !ServletFileUpload.isMultipartContent( request ) )
{
throw new UnsupportedOperationException( "Not Multipart Content" );
}
FileNameDataPair results = null;
// Parse the request
for ( FileItemIterator iterator = new ServletFileUpload().getItemIterator( request ); iterator.hasNext(); )
{
FileItemStream item = iterator.next();
if ( item.isFormField() )
{
throw new UnsupportedOperationException( "Form Fields Not Supported" );
// LOGGER.warn.log( "Form field ", item.getFieldName(), " detected." );
}
String fileName = item.getName();
// LOGGER.debug.log( "Upload file name before clean: ", fileName );
fileName = justFileName( fileName );
if ( results != null )
{
throw new UnsupportedOperationException( "Expected One File, but found Two '" + results.getName() + "' AND '" + fileName + "'" );
}
results = new FileNameDataPair( fileName, item.openStream() ); // closes InputStream
}
if ( results == null )
{
throw new UnsupportedOperationException( "Expected One File, but None Found" );
}
validate( results.getData() );
return results;
}
private String justFileName( String pFileName )
{
pFileName = "/" + cleanFileName( pFileName );
return pFileName.substring( pFileName.lastIndexOf( '/' ) + 1 );
}
private String cleanFileName( String pFileName )
{
pFileName = deNull( pFileName ).trim().replace( '\\', '/' );
if ( (pFileName.length() >= 2) && Character.isLetter( pFileName.charAt( 0 ) ) && (':' == pFileName.charAt( 1 )) )
{
pFileName = pFileName.substring( 2 ).trim();
}
while ( pFileName.startsWith( "/" ) )
{
pFileName = pFileName.substring( 1 ).trim();
}
return pFileName;
}
private String determineLineSeparator( String pData )
{
for ( int blockSize = 8; true; blockSize += 8 )
{
for ( char option : SEPARATOR_OPTIONS )
{
StringBuilder sb = new StringBuilder( blockSize );
for ( int i = 0; i < blockSize; i++ )
{
sb.append( option );
}
if ( !pData.contains( sb ) )
{
return sb.toString(); // The Separator we'll use
}
}
}
}
private String deNull( String string )
{
return (string != null) ? string : "";
}
private void insureCharacterEncoding( HttpServletRequest pReq )
throws UnsupportedEncodingException
{
String zEnc = pReq.getCharacterEncoding();
if ( zEnc == null )
{
pReq.setCharacterEncoding( "UTF-8" );
}
else if ( !"UTF-8".equalsIgnoreCase( zEnc ) )
{
System.out.println( "UploadSmallFileReflectorServlet.insureCharacterEncoding: " + zEnc );
// LOGGER.debug.log( zEnc, " = pReq.getCharacterEncoding();" );
}
}
/**
* Obtain information on this servlet.
*
* @return String describing this servlet.
*/
@Override
public String getServletInfo()
{
return "Upload Small File Reflector servlet -- used to reflect a small file back to the Browser";
}
private static class FileNameDataPair
{
private String name, data;
public FileNameDataPair( String name, InputStream is )
throws IOException
{
this.name = name;
data = capture( is );
}
public String getName()
{
return name;
}
public String getData()
{
return data;
}
private String capture( InputStream is )
throws IOException
{
int maxRemaining = MAX_FILE_SIZE;
ByteArrayOutputStream os = new ByteArrayOutputStream( maxRemaining );
try
{
byte[] buffer = new byte[4096];
for ( int bytesRead; -1 != (bytesRead = is.read( buffer )); )
{
if ( bytesRead > 0 )
{
if ( (maxRemaining -= bytesRead) < 0 )
{
throw new UnsupportedOperationException( "File Too Large - Byte Count Exceeded: " + MAX_FILE_SIZE );
}
os.write( buffer, 0, bytesRead );
}
}
}
finally
{
try
{
is.close();
}
catch ( IOException e )
{
// Whatever
}
}
os.flush();
os.close();
return os.toString( "UTF-8" );
}
}
protected void validate( String pData )
{
for ( int i = 0; i < pData.length(); i++ )
{
char c = pData.charAt( i );
if ( (' ' <= c) && (c <= 126) )
{
continue;
}
if ( c == '\t' || c == '\n' || c == '\f' || c == '\r' )
{
continue;
}
throw new UnsupportedOperationException( "Only Supporting a limited subset of 7-bit Ascii" );
}
}
}
|
Revision |
Author |
Commited |
Message |
518
|
GeorgeS
|
Tue 27 Sep, 2011 04:06:12 +0000 |
|