-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSourceWrappedRequestDumpFilter.java
More file actions
1235 lines (1103 loc) · 40.8 KB
/
DataSourceWrappedRequestDumpFilter.java
File metadata and controls
1235 lines (1103 loc) · 40.8 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ATTENZIONE: quando si modifica questo programma in Eclipse, spuntare in
* "Window->Preferences->Java->Code Style->Formatter->Edit->Off/On Eags->Enable off/on tags"
* per evitare che facendo la formattazione automatica si scombinino
* gli incolonnamenti nel sorgente.
*/
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.Part;
import javax.sql.DataSource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicNameValuePair;
import com.google.common.collect.Iterables;
import com.google.common.collect.ObjectArrays;
/**
* This class is for recording http request in a database table, whose DDL is at the
* end of this file. Use it as a filter in a tomcat webapp, including the following
* lines in web.xml, adapting them to your needs.
*
<filter>
<filter-name>requestDumper</filter-name>
<filter-class>FULLY.QUALIFIED.NAME.OF.DataSourceWrappedRequestDumpFilter</filter-class>
<init-param>
<description>Abilita o disabilita il dump delle richieste su DBMS</description>
<param-name>isDumpActive</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<description>
Nome del javax.sql.DataSource dove stanno le tabelle su cui devono essere
ammucchiate le richieste, definito nel tag "resource-ref" del web.xml.
</description>
<param-name>dataSourceJndiName</param-name>
<param-value>java:comp/env/jdbc/YOUR-RESOURCE-NAME</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>requestDumper</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*
* @author gluppi 19 March 2015
*/
public class DataSourceWrappedRequestDumpFilter implements Filter
{
/*
* Safe copy/paste class reference to self.
*/
private static final Class currentClass = new Object() { }.getClass().getEnclosingClass();
/**
* Nome dell'attibuto del ServletContext contenente lo stato di attivazione del filtro.
*/
public static final String CTX_ENABLED_ATT_NAME = currentClass.getName() + ".enabled" ;
/**
* Nome del parametro, da utilizzare nella configurazione del filtro, che
* indica se in fase di bootstrap della webapp il dump delle richieste deve
* essere attivo.
*/
public static final String INIT_PARAM_DUMP_ACTIVE = "isDumpActive";
/**
* Nome del parametro, da utilizzare nella configurazione del filtro, che
* indica come si chiama il DataSource contenente le tabelle su cui
* ammucchiare le richieste. Il valore del parametro e` di tipo Stringa.
* es.: "java:comp/env/jdbc/nomeDelDataSource"
*/
public static final String INIT_PARAM_DATASOURCE_JNDINAME = "dataSourceJndiName";
/**
* Flag che indica se effettuare il dump oppure no. Quando false i dettagli
* della request non vengono salvati sulle tabelle di dump.
*/
private boolean isDumpActive = true;
/**
* Valore di startup del parametro di configurazione INIT_PARAM_DUMP_ACTIVE.
*/
private boolean initialIsDumpActive;
/**
* The logger for this class.
*/
private Logger logger = Logger.getLogger(getClass()
.getName());
/**
* Nome del DataSource.
*/
private String dataSourceName = null;
/**
* Il DataSource.
*/
private DataSource dataSource = null;
/**
* Dimensione delle colonne della tabella di dump
*/
private int queryStringColumnSize;
private int requestUriColumnSize;
private int servletPathColumnSize;
private int idColumnSize;
/**
*
*/
@Override
public void destroy()
{
// TODO: bisognerebbe togliere dal ServletContext l'attributo CTX_ENABLED_ATT_NAME, ma non vi abbiamo accesso.
this.dataSource = null;
this.dataSourceName = null;
}
/**
*
*/
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException
{
/*
* Questo filtro può trattare solo richieste di tipo HttpServletRequest.
* Se l'attuale richiesta non lo e`, proseguo con l'elaborazione dei filtri successivi.
*/
if (!(servletRequest instanceof HttpServletRequest))
{
chain.doFilter(servletRequest, servletResponse);
return;
}
setActiveStatusUponContextAttribute(servletRequest);
/*
* Se il filtro non e` attivo, proseguiamo nella catena.
*/
if (!getIsDumpActive())
{
chain.doFilter(servletRequest, servletResponse);
return;
}
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
PersistedRequestWrapper persistedRequest = null;
try
{
persistedRequest = new PersistedRequestWrapper(httpServletRequest, dataSource);
}
catch (SQLException ex)
{
logger.severe("Request not persisted!");
throw new ServletException(ex);
}
chain.doFilter(persistedRequest, servletResponse);
}
/**
*
*/
@Override
public void init(FilterConfig config) throws ServletException
{
/*
* Acquisizione dei parametri di inizializzazione di questo filtro.
*/
{
String active = config.getInitParameter(INIT_PARAM_DUMP_ACTIVE);
setIsDumpActive((active == null) ? false : new Boolean(active));
initialIsDumpActive = getIsDumpActive();
}
this.dataSourceName = config.getInitParameter(INIT_PARAM_DATASOURCE_JNDINAME);
if (dataSourceName == null || dataSourceName.trim().length() == 0)
{
throw new ServletException(getClass().getName() + ": il parametro "
+ INIT_PARAM_DATASOURCE_JNDINAME + " non e` valido.");
}
/*
* Inserimento nel ServletContext dell'attributo per l'abilitazione/disabilitazione
* del filtro.
*/
{
ServletContext context = config.getServletContext();
context.setAttribute(CTX_ENABLED_ATT_NAME, getIsDumpActive());
}
/*
* Acquisizione del DataSource.
*/
try
{
Context initCtx = new InitialContext();
this.dataSource = (DataSource) initCtx.lookup(dataSourceName);
}
catch (NamingException ex)
{
StringBuffer buffer = new StringBuffer();
buffer.append("Error initializing datasource ");
buffer.append(dataSourceName);
logger.severe(buffer.toString());
throw new ServletException(ex);
}
/*
* Determinazione della lunghezza dei campi varchar passibili di troncamento.
*/
try
{
Connection conn = null;
try
{
conn = dataSource.getConnection();
this.idColumnSize = getColumnSize(conn, "HTTP_REQUESTS", "ID");
this.queryStringColumnSize = getColumnSize(conn, "HTTP_REQUESTS", "QUERY_STRING");
this.requestUriColumnSize = getColumnSize(conn, "HTTP_REQUESTS", "REQUEST_URI");
this.servletPathColumnSize = getColumnSize(conn, "HTTP_REQUESTS", "SERVLET_PATH");
}
catch (SQLException ex)
{
throw new ServletException(ex);
}
finally
{
if (!conn.isClosed())
{
conn.close();
}
}
}
catch (SQLException ex)
{
throw new ServletException(ex);
}
}
/**
* Imposta se fare il dump delle richieste o no.
*
* @param isDumpActive
*/
protected void setIsDumpActive(boolean isDumpActive)
{
this.isDumpActive = isDumpActive;
}
/**
* @return True se il dump delle richieste è attivo, False se il dump è
* inattivo.
*/
public Boolean getIsDumpActive()
{
return isDumpActive;
}
/**
* Permette di attivare il dump delle richieste, eventualmente disattivato.
*/
public void activate()
{
setIsDumpActive(true);
logger.info("HTTP Request dump is now ACTIVE.");
}
/**
* Permette di disattivare il dump delle richieste, senza dover intervenire
* sulla configurazione della webapp.
*/
public void deactivate()
{
setIsDumpActive(false);
logger.info("HTTP Request dump is now INACTIVE.");
}
/**
* Il filtro potrebbe essere stato disattivato impostanto a False l'attributo
* del ServletContext CTX_ENABLED_ATT_NAME, quindi leggiamo tale attributo
* per decidere poi cosa fare in base al suo valore.
* Se l'attributo del ServletContext e` sparito o e` stato sovrascritto
* con un attributo non Boolean, il filtro viene reimpostato nella condizione iniziale
* definita nella configurazione della webapp.
*
* @param request
*/
private void setActiveStatusUponContextAttribute(ServletRequest request)
{
{
ServletContext context = request.getServletContext();
Object attribute = context.getAttribute(CTX_ENABLED_ATT_NAME);
if (attribute != null && attribute instanceof Boolean)
{
boolean active = (Boolean) attribute;
if (active)
{
if (!getIsDumpActive())
{
activate();
}
}
else
{
if (getIsDumpActive())
{
deactivate();
}
}
}
else
{
if (initialIsDumpActive)
{
if (!getIsDumpActive())
{
activate();
}
}
else
{
if (getIsDumpActive())
{
deactivate();
}
}
}
}
}
/**
* @param conn
* @return la dimensione della colonna "columnName" della tabella
* "tableName"
* @throws SQLException
*/
private int getColumnSize(Connection conn, String tableName, String columnName) throws SQLException
{
int size = 0;
DatabaseMetaData dbMetaData = conn.getMetaData();
ResultSet rs = dbMetaData.getColumns(null, null, tableName, columnName);
size = rs.next() ? rs.getInt("COLUMN_SIZE") : 0;
rs.close();
return size;
}
/**
* Rappresenta una ServletRequest salvata su db.
*/
private final class PersistedRequestWrapper extends HttpServletRequestWrapper
{
private Integer id;
private DataSource dataSource;
private byte[] buffer = null;
private Map<String, String[]> parametersMap = null;
private Logger logger = Logger.getLogger(getClass().getName());
/**
* @param request
* @param dataSource
* @throws IOException
* @throws SQLException
*/
protected PersistedRequestWrapper(HttpServletRequest request,
DataSource dataSource) throws IOException, SQLException
{
this(request);
this.dataSource = dataSource;
this.id = persist();
this.parametersMap = getInternalParameterMap();
}
/**
* Questa classe può essere istanziata solamente passando
* al costruttore i necessari parametri.
* @param req
*/
private PersistedRequestWrapper(HttpServletRequest req)
{
super(req);
}
public String getIdAsString()
{
return (getId() == null) ? null : getId().toString();
}
public Integer getId()
{
return this.id;
}
/**
* @return true solo se si tratta di una request di tipo multipart/form-data
*/
public boolean getIsMultipartFormData()
{
boolean isMultipart = false;
if (getContentType() != null)
{
String contentType = ContentType.parse(getContentType()).getMimeType();
isMultipart = contentType.equals(ContentType.MULTIPART_FORM_DATA.getMimeType());
}
return isMultipart;
}
/**
* @return true solo se si tratta di una request di tipo application/form-url-encoded
*/
public boolean getIsApplicationFormUrlEncoded()
{
boolean isApplicationFormUrlEncoded = false;
if (getContentType() != null)
{
String contentType = ContentType.parse(getContentType()).getMimeType();
isApplicationFormUrlEncoded = contentType.equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
}
return isApplicationFormUrlEncoded;
}
/**
* @return La stringa usata come deliminatore nel body per le richieste multipart.
*/
public String getBoundary()
{
String boundary = null;
if (getIsMultipartFormData())
{
String tokens[] = getContentType().toLowerCase().split(";");
boundary = tokens[1].split("=")[1];
}
return boundary;
}
@Override
public String getParameter(String name)
{
String[] params = this.parametersMap.get(name);
return params != null && params.length > 0 ? params[0] : null;
}
@Override
public String[] getParameterValues(String name)
{
return parametersMap.get(name);
}
@Override
public Enumeration<String> getParameterNames()
{
return Collections.enumeration(parametersMap.keySet());
}
@Override
public Map<String, String[]> getParameterMap()
{
return this.parametersMap;
}
/**
* Legge dal DB il body della richiesta precedentemente salvata e
* lo restituisce sotto forma di input stream.
*/
@Override
public ServletInputStream getInputStream() throws IOException
{
// @formatter:off
String sql =
"SELECT body AS body " +
"FROM http_requests " +
"WHERE day = TO_CHAR(CURRENT_DATE, 'YYYYMMDD') " +
"AND id = ?";
// @formatter:on
Connection conn = null;
PreparedStatement stat = null;
ResultSet result = null;
InputStream input = null;
byte[] bytes = new byte[0];
try
{
try
{
conn = dataSource.getConnection();
stat = conn.prepareStatement(sql);
stat.setInt(1, getId());
result = stat.executeQuery();
if (result.next())
{
Clob content = result.getClob("body");
input = content.getAsciiStream();
bytes = IOUtils.toByteArray(input);
}
stat.close();
conn.close();
input.close();
}
catch (SQLException ex)
{
StringBuffer buffer = new StringBuffer();
buffer.append("Error retrieving request. Values are: ");
buffer.append("id=" + id.toString() + ",");
logger.severe(buffer.toString());
throw ex;
}
finally
{
if (stat != null && !stat.isClosed())
stat.close();
if (conn != null && !conn.isClosed())
conn.close();
if (input != null)
input.close();
}
}
catch (SQLException ex)
{
throw new IOException(ex);
}
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
return new BufferedServletInputStream(is);
}
@Override
public BufferedReader getReader() throws IOException
{
return new BufferedReader(new InputStreamReader(getInputStream()));
}
/**
* @see javax.servlet.http.HttpServletRequestWrapper#getParts()
*/
@Override
public Collection<Part> getParts() throws IllegalStateException,
IOException, ServletException
{
// TODO Auto-generated method stub: da adattare!
Collection<Part> parts = super.getParts();
for (Part part : parts)
{
String name = part.getName();
}
return parts;
}
private Map<String, String[]> getInternalParameterMap()
{
Iterable<NameValuePair> params = URLEncodedUtils.parse(
getQueryString(), Charset.forName("UTF8"));
try
{
if (getIsApplicationFormUrlEncoded())
{
List<NameValuePair> postParams = URLEncodedUtils.parse(
IOUtils.toString(getReader()),
Charset.forName("UTF8"));
params = Iterables.concat(params, postParams);
}
else if (getIsMultipartFormData())
{
List<NameValuePair> parameterList = new ArrayList<NameValuePair>();
try
{
RequestContext context = new ServletRequestContext(this);
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new ServletFileUpload(factory);
List<FileItem> multipartItems = upload
.parseRequest(context);
parameterList = parseItems(multipartItems);
}
catch (FileUploadException ex)
{
throw new IOException(ex);
}
params = Iterables.concat(params, parameterList);
}
}
catch (IOException e)
{
throw new IllegalStateException(e);
}
Map<String, String[]> result = toMap(params);
return result;
}
private List<NameValuePair> parseItems(List<FileItem> multipartItems)
{
List<NameValuePair> parameterList = new ArrayList<NameValuePair>();
for (FileItem multipartItem : multipartItems)
{
String fieldName = multipartItem.getFieldName();
String value = multipartItem.isFormField() ? multipartItem.getString() : getFileValue(multipartItem);
if (value != null)
{
NameValuePair pair = new BasicNameValuePair(fieldName, value);
parameterList.add(pair);
}
}
return parameterList;
}
private String getFileValue(FileItem item)
{
StringBuffer buffer = null;
if (item.getSize() > 0)
{
String fileName = item.getName();
String contentType = item.getContentType();
String content = item.getString();
Long size = item.getSize();
buffer = new StringBuffer();
buffer.append("{");
buffer.append("fileName:");
buffer.append(getQuotedString(fileName));
buffer.append(",");
buffer.append("contentType:");
buffer.append(getQuotedString(contentType));
buffer.append(",");
buffer.append("size:");
buffer.append(size.toString());
buffer.append(",");
buffer.append("content:");
buffer.append(getQuotedString(content));
buffer.append("}");
}
return (buffer == null) ? null : buffer.toString();
}
private Map<String, String[]> toMap(Iterable<NameValuePair> body)
{
Map<String, String[]> result = new LinkedHashMap<String, String[]>();
for (NameValuePair e : body)
{
String key = e.getName();
String value = e.getValue();
if (result.containsKey(key))
{
String[] newValue = ObjectArrays.concat(result.get(key), value);
result.remove(key);
result.put(key, newValue);
}
else
{
result.put(key, new String[]
{ value });
}
}
return result;
}
/**
* Salva la request, assegnendole una chiave primaria.
* @return
* @throws IOException
* @throws SQLException
*/
private Integer persist() throws IOException, SQLException
{
Integer id = null;
Connection conn = null;
try
{
int insertedRow = 0;
conn = dataSource.getConnection();
while (insertedRow == 0)
{
id = getNextId(conn);
insertedRow = insertRow(conn, id);
}
conn.close();
}
catch (SQLException ex)
{
StringBuffer buffer = new StringBuffer();
buffer.append("Error inserting request. Values are: ");
buffer.append("id=" + id + ",");
logger.severe(buffer.toString());
throw ex;
}
finally
{
if (conn != null && !conn.isClosed())
conn.close();
}
return id;
}
/**
* Inserisce il record della request.
* @param conn
* @param id
* @return
* @throws SQLException
* @throws IOException
*/
private int insertRow(Connection conn, int id) throws SQLException, IOException
{
// @formatter:off
String sql =
"INSERT INTO http_requests" +
"(" +
" id," + /* 1 */
" date_header," + /* 2 */
" method," + /* 3 */
" context_path," + /* 4 */
" path_info," + /* 5 */
" path_translated," + /* 6 */
" query_string," + /* 7 */
" request_uri," + /* 8 */
" servlet_path," + /* 9 */
" authentication_type," + /* 10 */
" remote_user," + /* 11 */
" sid," + /* 12 */
" sid_is_from_cookie," + /* 13 */
" sid_is_from_url," + /* 14 */
" sid_is_valid," + /* 15 */
" content_type," + /* 16 */
" content_length," + /* 17 */
" character_encoding," + /* 18 */
" dispatcher_type," + /* 19 */
" locale," + /* 20 */
" local_address," + /* 21 */
" local_name," + /* 22 */
" local_port," + /* 23 */
" remote_address," + /* 24 */
" remote_host," + /* 25 */
" remote_port," + /* 26 */
" scheme_name," + /* 27 */
" server_name," + /* 28 */
" server_port," + /* 29 */
" protocol," + /* 30 */
" channel_is_secure," + /* 31 */
" async_is_supported," + /* 32 */
" async_is_started," + /* 33 */
" headers," + /* 34 */
" cookies," + /* 35 */
" body" + /* 36 */
")" +
"VALUES" +
"(" +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?," +
" ?" +
")" ;
// @formatter:on
int insertedRow = 0;
PreparedStatement stat = null;
OutputStream output = null;
InputStream input = null;
try
{
stat = conn.prepareStatement(sql);
Clob headers = conn.createClob();
headers.setString(1, getHeaders());
Clob cookies = conn.createClob();
cookies.setString(1, getCookiesString());
Clob body = conn.createClob();
output = body.setAsciiStream(1);
input = super.getInputStream();
IOUtils.copy(input, output);
output.flush();
// @formatter:off
stat.setInt ( 1, id) ;
stat.setDate ( 2, getDateHeaderAsDate());
stat.setString( 3, getMethod()) ;
stat.setString( 4, getContextPath()) ;
stat.setString( 5, getPathInfo()) ;
stat.setString( 6, getPathTranslated()) ;
stat.setString( 7, trimToSize(getQueryString(), queryStringColumnSize)) ;
stat.setString( 8, trimToSize(getRequestURI(), requestUriColumnSize)) ;
stat.setString( 9, trimToSize(getServletPath(), servletPathColumnSize)) ;
stat.setString(10, getAuthType()) ;
stat.setString(11, getRemoteUser()) ;
stat.setString(12, getRequestedSessionId()) ;
stat.setInt (13, boolToInt(isRequestedSessionIdFromCookie())) ;
stat.setInt (14, boolToInt(isRequestedSessionIdFromURL())) ;
stat.setInt (15, boolToInt(isRequestedSessionIdValid())) ;
stat.setString(16, getContentType()) ;
if (getContentLength() != -1)
{
stat.setInt (17, getContentLength()) ;
}
else
{
stat.setNull(17, Types.NUMERIC) ;
}
stat.setString(18, getCharacterEncoding()) ;
stat.setString(19, getDispatcherType().name()) ;
stat.setString(20, getLocale().getDisplayName()) ;
stat.setString(21, getLocalAddr()) ;
stat.setString(22, getLocalName()) ;
stat.setInt (23, getLocalPort()) ;
stat.setString(24, getRemoteAddr()) ;
stat.setString(25, getRemoteHost()) ;
stat.setInt (26, getRemotePort()) ;
stat.setString(27, getScheme()) ;
stat.setString(28, getServerName()) ;
stat.setInt (29, getServerPort()) ;
stat.setString(30, getProtocol()) ;
stat.setInt (31, boolToInt(isSecure())) ;
stat.setInt (32, boolToInt(isAsyncSupported())) ;
stat.setInt (33, boolToInt(isAsyncStarted())) ;
stat.setClob (34, headers);
stat.setClob (35, cookies);
stat.setClob (36, body);
// @formatter:on
insertedRow = stat.executeUpdate();
stat.close();
output.close();
input.close();
}
catch (SQLIntegrityConstraintViolationException ex)
{
/*
* Dal momento che la tabella non ha né chiavi esterne né
* vincoli UNIQUE, la violazione di integrità in questo punto
* può essere solamente una chiave primaria doppia.
* Ignoriamo l'eccezione perchè ritentiamo l'inserimento
* con un'altra chiave primaria.
* Potrebbe succedere che il programma entri in un ciclo
* infinito se l'id assegnato al record raggiunge il valore massimo
* inseribile nel campo. In tal caso rilanciamo l'eccezione.
*/
String maxValue = String.format("%0" + idColumnSize + "d", 0).replace("0", "9");
if (id == Integer.parseInt(maxValue))
{
throw new SQLIntegrityConstraintViolationException("id max value reached! You need to increase the size of the id column in table http_requests and restart the webapp.");
}
}
finally
{
if (stat != null && !stat.isClosed())
stat.close();
if (output != null)
output.close();
if (input != null)
input.close();
}
return insertedRow;
}
/**
* @param conn
* @return Il prossimo id da utilizzare per l'inserimento della request.
* @throws Exception
*/
private synchronized int getNextId(Connection conn) throws SQLException
{
// @formatter:off
String sql =
"SELECT MAX(id)+1 AS nextid " +
"FROM http_requests " +
"WHERE day = TO_CHAR(CURRENT_DATE, 'YYYYMMDD')" ;
// @formatter:on
int nextId = 0;
Statement stat = null;
ResultSet result = null;
try
{
stat = conn.createStatement();
result = stat.executeQuery(sql);
if (result.next())
{
nextId = (result.getInt("nextid") == 0) ? 1 : result
.getInt("nextid");