|
@@ -1,160 +1,161 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.server; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
5 |
|
- |
import org.litesoft.logger.*; |
6 |
|
- |
import org.litesoft.reports.*; |
7 |
|
- |
import org.litesoft.uispecification.*; |
8 |
|
- |
|
9 |
|
- |
import javax.servlet.*; |
10 |
|
- |
import javax.servlet.http.*; |
11 |
|
- |
import java.io.*; |
12 |
|
- |
|
13 |
|
- |
public class ReportServlet extends HttpServlet { |
14 |
|
- |
protected static Logger logger = LoggerFactory.getLogger( ReportServlet.class ); |
15 |
|
- |
|
16 |
|
- |
private static final long serialVersionUID = 1L; |
17 |
|
- |
|
18 |
|
- |
private PrintWriter responseInit( HttpServletResponse pResponse, String pTitle ) |
19 |
|
- |
throws IOException { |
20 |
|
- |
pResponse.setContentType( "text/html" ); |
21 |
|
- |
PrintWriter out = pResponse.getWriter(); |
22 |
|
- |
out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" ); |
23 |
|
- |
out.println( "<html>" ); |
24 |
|
- |
out.println( " <head>" ); |
25 |
|
- |
out.println( " <title>" + pTitle + "</title>" ); |
26 |
|
- |
out.println( " <link rel=\"stylesheet\" type=\"text/css\" href=\"../../common/litesoft_reporting.css\" />" ); |
27 |
|
- |
out.println( " </head>" ); |
28 |
|
- |
out.println( " <body>" ); |
29 |
|
- |
return out; |
30 |
|
- |
} |
31 |
|
- |
|
32 |
|
- |
private void responseFini( PrintWriter pOut ) { |
33 |
|
- |
pOut.println( " </body>" ); |
34 |
|
- |
pOut.println( "</html>" ); |
35 |
|
- |
pOut.flush(); |
36 |
|
- |
pOut.close(); |
37 |
|
- |
} |
38 |
|
- |
|
39 |
|
- |
/** |
40 |
|
- |
* The doPost method of the servlet. |
41 |
|
- |
*/ |
42 |
|
- |
@Override |
43 |
|
- |
public void doPost( HttpServletRequest pRequest, HttpServletResponse pResponse ) |
44 |
|
- |
throws ServletException, IOException { |
45 |
|
- |
PrintWriter out = responseInit( pResponse, "Report" ); |
46 |
|
- |
out.println( " Post Not Supported" ); |
47 |
|
- |
responseFini( out ); |
48 |
|
- |
} |
49 |
|
- |
|
50 |
|
- |
/** |
51 |
|
- |
* The doGet method of the servlet. |
52 |
|
- |
*/ |
53 |
|
- |
@Override |
54 |
|
- |
public void doGet( HttpServletRequest pRequest, HttpServletResponse pResponse ) |
55 |
|
- |
throws ServletException, IOException { |
56 |
|
- |
String zPathInfo = Strings.deNull( pRequest.getPathInfo() ); |
57 |
|
- |
|
58 |
|
- |
ServletSessionHelper.chkInitialized(); |
59 |
|
- |
ServletSessionHelper.callInit( pRequest.getSession( true ) ); |
60 |
|
- |
try { |
61 |
|
- |
PrintWriter out; |
62 |
|
- |
|
63 |
|
- |
ReportParamPair zPair = extractReportParamPair( zPathInfo ); |
64 |
|
- |
|
65 |
|
- |
String zReportID = (zPair != null) ? zPair.getReportID() : ""; |
66 |
|
- |
|
67 |
|
- |
String zTitle = "Report: " + zReportID; |
68 |
|
- |
|
69 |
|
- |
ServerReportFactory zReportFactory = ServerReportFactory.Registry.getFactory( zReportID ); |
70 |
|
- |
if ( zReportFactory == null ) { |
71 |
|
- |
out = responseInit( pResponse, zTitle ); |
72 |
|
- |
out.println( " No Report for ReportID: " + zReportID ); |
73 |
|
- |
} else { |
74 |
|
- |
ServerReport zReport; |
75 |
|
- |
try { |
76 |
|
- |
zReport = zReportFactory.create( zPair.getParams() ); |
77 |
|
- |
zTitle = zReport.pageTitle(); |
78 |
|
- |
} |
79 |
|
- |
catch ( RuntimeException e ) { |
80 |
|
- |
out = responseInit( pResponse, zTitle ); |
81 |
|
- |
e.printStackTrace( out ); |
82 |
|
- |
responseFini( out ); |
83 |
|
- |
return; |
84 |
|
- |
} |
85 |
|
- |
out = responseInit( pResponse, zTitle ); |
86 |
|
- |
try { |
87 |
|
- |
zReport.render( out ); |
88 |
|
- |
} |
89 |
|
- |
catch ( RuntimeException e ) { |
90 |
|
- |
e.printStackTrace( out ); |
91 |
|
- |
} |
92 |
|
- |
} |
93 |
|
- |
responseFini( out ); |
94 |
|
- |
} |
95 |
|
- |
finally { |
96 |
|
- |
ServletSessionHelper.callFini(); |
97 |
|
- |
} |
98 |
|
- |
} |
99 |
|
- |
|
100 |
|
- |
private ReportParamPair extractReportParamPair( String pPathInfo ) { |
101 |
|
- |
if ( pPathInfo.startsWith( "/" ) && pPathInfo.endsWith( ".html" ) ) { |
102 |
|
- |
int zAt = pPathInfo.lastIndexOf( '/' ); |
103 |
|
- |
if ( zAt != -1 ) { |
104 |
|
- |
String zParams = pPathInfo.substring( 1, zAt ).trim(); |
105 |
|
- |
String zReportID = dropWhen( pPathInfo.substring( zAt + 1, pPathInfo.length() - 5 ).trim() ); |
106 |
|
- |
// x-1234-12-12-12-12 |
107 |
|
- |
// 123456789012345678 |
108 |
|
- |
if ( zReportID != null ) { |
109 |
|
- |
return new ReportParamPair( zReportID, UriFragmentIdParams.fromParamsString( zParams ) ); |
110 |
|
- |
} |
111 |
|
- |
} |
112 |
|
- |
} |
113 |
|
- |
return null; |
114 |
|
- |
} |
115 |
|
- |
|
116 |
|
- |
private String dropWhen( String pReportID ) { |
117 |
|
- |
// x-1234-12-12-12-12 |
118 |
|
- |
// 123456789012345678 |
119 |
|
- |
int zWhenDashSepAt = pReportID.length() - 17; |
120 |
|
- |
if ( (zWhenDashSepAt > 0) && validWhenTail( pReportID.substring( zWhenDashSepAt ) ) ) { |
121 |
|
- |
return pReportID.substring( 0, zWhenDashSepAt ); |
122 |
|
- |
} |
123 |
|
- |
return null; |
124 |
|
- |
} |
125 |
|
- |
|
126 |
|
- |
private boolean validWhenTail( String zTail ) { |
127 |
|
- |
// ............... -1234-12-12-12-12 |
128 |
|
- |
String zPattern = "-9999-99-99-99-99"; |
129 |
|
- |
for ( int i = 0; i < zPattern.length(); i++ ) { |
130 |
|
- |
char zPatternChar = zPattern.charAt( i ); |
131 |
|
- |
char zTailChar = zTail.charAt( i ); |
132 |
|
- |
if ( zPatternChar == '9' ) { |
133 |
|
- |
if ( !Characters.isNumeric( zTailChar ) ) { |
134 |
|
- |
return false; |
135 |
|
- |
} |
136 |
|
- |
} else if ( zPatternChar != zTailChar ) { |
137 |
|
- |
return false; |
138 |
|
- |
} |
139 |
|
- |
} |
140 |
|
- |
return true; |
141 |
|
- |
} |
142 |
|
- |
|
143 |
|
- |
private static class ReportParamPair { |
144 |
|
- |
private String mReportID; |
145 |
|
- |
private UriFragmentIdParams mParams; |
146 |
|
- |
|
147 |
|
- |
private ReportParamPair( String pReportID, UriFragmentIdParams pParams ) { |
148 |
|
- |
mReportID = pReportID; |
149 |
|
- |
mParams = pParams; |
150 |
|
- |
} |
151 |
|
- |
|
152 |
|
- |
public String getReportID() { |
153 |
|
- |
return mReportID; |
154 |
|
- |
} |
155 |
|
- |
|
156 |
|
- |
public UriFragmentIdParams getParams() { |
157 |
|
- |
return mParams; |
158 |
|
- |
} |
159 |
|
- |
} |
160 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.server; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.typeutils.*; |
|
6 |
+ |
import org.litesoft.logger.*; |
|
7 |
+ |
import org.litesoft.reports.*; |
|
8 |
+ |
import org.litesoft.uispecification.*; |
|
9 |
+ |
|
|
10 |
+ |
import javax.servlet.*; |
|
11 |
+ |
import javax.servlet.http.*; |
|
12 |
+ |
import java.io.*; |
|
13 |
+ |
|
|
14 |
+ |
public class ReportServlet extends HttpServlet { |
|
15 |
+ |
protected static Logger logger = LoggerFactory.getLogger( ReportServlet.class ); |
|
16 |
+ |
|
|
17 |
+ |
private static final long serialVersionUID = 1L; |
|
18 |
+ |
|
|
19 |
+ |
private PrintWriter responseInit( HttpServletResponse pResponse, String pTitle ) |
|
20 |
+ |
throws IOException { |
|
21 |
+ |
pResponse.setContentType( "text/html" ); |
|
22 |
+ |
PrintWriter out = pResponse.getWriter(); |
|
23 |
+ |
out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" ); |
|
24 |
+ |
out.println( "<html>" ); |
|
25 |
+ |
out.println( " <head>" ); |
|
26 |
+ |
out.println( " <title>" + pTitle + "</title>" ); |
|
27 |
+ |
out.println( " <link rel=\"stylesheet\" type=\"text/css\" href=\"../../common/litesoft_reporting.css\" />" ); |
|
28 |
+ |
out.println( " </head>" ); |
|
29 |
+ |
out.println( " <body>" ); |
|
30 |
+ |
return out; |
|
31 |
+ |
} |
|
32 |
+ |
|
|
33 |
+ |
private void responseFini( PrintWriter pOut ) { |
|
34 |
+ |
pOut.println( " </body>" ); |
|
35 |
+ |
pOut.println( "</html>" ); |
|
36 |
+ |
pOut.flush(); |
|
37 |
+ |
pOut.close(); |
|
38 |
+ |
} |
|
39 |
+ |
|
|
40 |
+ |
/** |
|
41 |
+ |
* The doPost method of the servlet. |
|
42 |
+ |
*/ |
|
43 |
+ |
@Override |
|
44 |
+ |
public void doPost( HttpServletRequest pRequest, HttpServletResponse pResponse ) |
|
45 |
+ |
throws ServletException, IOException { |
|
46 |
+ |
PrintWriter out = responseInit( pResponse, "Report" ); |
|
47 |
+ |
out.println( " Post Not Supported" ); |
|
48 |
+ |
responseFini( out ); |
|
49 |
+ |
} |
|
50 |
+ |
|
|
51 |
+ |
/** |
|
52 |
+ |
* The doGet method of the servlet. |
|
53 |
+ |
*/ |
|
54 |
+ |
@Override |
|
55 |
+ |
public void doGet( HttpServletRequest pRequest, HttpServletResponse pResponse ) |
|
56 |
+ |
throws ServletException, IOException { |
|
57 |
+ |
String zPathInfo = ConstrainTo.notNull( pRequest.getPathInfo() ); |
|
58 |
+ |
|
|
59 |
+ |
ServletSessionHelper.chkInitialized(); |
|
60 |
+ |
ServletSessionHelper.callInit( pRequest.getSession( true ) ); |
|
61 |
+ |
try { |
|
62 |
+ |
PrintWriter out; |
|
63 |
+ |
|
|
64 |
+ |
ReportParamPair zPair = extractReportParamPair( zPathInfo ); |
|
65 |
+ |
|
|
66 |
+ |
String zReportID = (zPair != null) ? zPair.getReportID() : ""; |
|
67 |
+ |
|
|
68 |
+ |
String zTitle = "Report: " + zReportID; |
|
69 |
+ |
|
|
70 |
+ |
ServerReportFactory zReportFactory = ServerReportFactory.Registry.getFactory( zReportID ); |
|
71 |
+ |
if ( zReportFactory == null ) { |
|
72 |
+ |
out = responseInit( pResponse, zTitle ); |
|
73 |
+ |
out.println( " No Report for ReportID: " + zReportID ); |
|
74 |
+ |
} else { |
|
75 |
+ |
ServerReport zReport; |
|
76 |
+ |
try { |
|
77 |
+ |
zReport = zReportFactory.create( zPair.getParams() ); |
|
78 |
+ |
zTitle = zReport.pageTitle(); |
|
79 |
+ |
} |
|
80 |
+ |
catch ( RuntimeException e ) { |
|
81 |
+ |
out = responseInit( pResponse, zTitle ); |
|
82 |
+ |
e.printStackTrace( out ); |
|
83 |
+ |
responseFini( out ); |
|
84 |
+ |
return; |
|
85 |
+ |
} |
|
86 |
+ |
out = responseInit( pResponse, zTitle ); |
|
87 |
+ |
try { |
|
88 |
+ |
zReport.render( out ); |
|
89 |
+ |
} |
|
90 |
+ |
catch ( RuntimeException e ) { |
|
91 |
+ |
e.printStackTrace( out ); |
|
92 |
+ |
} |
|
93 |
+ |
} |
|
94 |
+ |
responseFini( out ); |
|
95 |
+ |
} |
|
96 |
+ |
finally { |
|
97 |
+ |
ServletSessionHelper.callFini(); |
|
98 |
+ |
} |
|
99 |
+ |
} |
|
100 |
+ |
|
|
101 |
+ |
private ReportParamPair extractReportParamPair( String pPathInfo ) { |
|
102 |
+ |
if ( pPathInfo.startsWith( "/" ) && pPathInfo.endsWith( ".html" ) ) { |
|
103 |
+ |
int zAt = pPathInfo.lastIndexOf( '/' ); |
|
104 |
+ |
if ( zAt != -1 ) { |
|
105 |
+ |
String zParams = pPathInfo.substring( 1, zAt ).trim(); |
|
106 |
+ |
String zReportID = dropWhen( pPathInfo.substring( zAt + 1, pPathInfo.length() - 5 ).trim() ); |
|
107 |
+ |
// x-1234-12-12-12-12 |
|
108 |
+ |
// 123456789012345678 |
|
109 |
+ |
if ( zReportID != null ) { |
|
110 |
+ |
return new ReportParamPair( zReportID, UriFragmentIdParams.fromParamsString( zParams ) ); |
|
111 |
+ |
} |
|
112 |
+ |
} |
|
113 |
+ |
} |
|
114 |
+ |
return null; |
|
115 |
+ |
} |
|
116 |
+ |
|
|
117 |
+ |
private String dropWhen( String pReportID ) { |
|
118 |
+ |
// x-1234-12-12-12-12 |
|
119 |
+ |
// 123456789012345678 |
|
120 |
+ |
int zWhenDashSepAt = pReportID.length() - 17; |
|
121 |
+ |
if ( (zWhenDashSepAt > 0) && validWhenTail( pReportID.substring( zWhenDashSepAt ) ) ) { |
|
122 |
+ |
return pReportID.substring( 0, zWhenDashSepAt ); |
|
123 |
+ |
} |
|
124 |
+ |
return null; |
|
125 |
+ |
} |
|
126 |
+ |
|
|
127 |
+ |
private boolean validWhenTail( String zTail ) { |
|
128 |
+ |
// ............... -1234-12-12-12-12 |
|
129 |
+ |
String zPattern = "-9999-99-99-99-99"; |
|
130 |
+ |
for ( int i = 0; i < zPattern.length(); i++ ) { |
|
131 |
+ |
char zPatternChar = zPattern.charAt( i ); |
|
132 |
+ |
char zTailChar = zTail.charAt( i ); |
|
133 |
+ |
if ( zPatternChar == '9' ) { |
|
134 |
+ |
if ( !Characters.isNumeric( zTailChar ) ) { |
|
135 |
+ |
return false; |
|
136 |
+ |
} |
|
137 |
+ |
} else if ( zPatternChar != zTailChar ) { |
|
138 |
+ |
return false; |
|
139 |
+ |
} |
|
140 |
+ |
} |
|
141 |
+ |
return true; |
|
142 |
+ |
} |
|
143 |
+ |
|
|
144 |
+ |
private static class ReportParamPair { |
|
145 |
+ |
private String mReportID; |
|
146 |
+ |
private UriFragmentIdParams mParams; |
|
147 |
+ |
|
|
148 |
+ |
private ReportParamPair( String pReportID, UriFragmentIdParams pParams ) { |
|
149 |
+ |
mReportID = pReportID; |
|
150 |
+ |
mParams = pParams; |
|
151 |
+ |
} |
|
152 |
+ |
|
|
153 |
+ |
public String getReportID() { |
|
154 |
+ |
return mReportID; |
|
155 |
+ |
} |
|
156 |
+ |
|
|
157 |
+ |
public UriFragmentIdParams getParams() { |
|
158 |
+ |
return mParams; |
|
159 |
+ |
} |
|
160 |
+ |
} |
|
161 |
+ |
} |