rsanchez
2014-11-18 1c00a09ff5753e785b0dbf1467075be30bc37029
#396 fix - Changes on logging system, FileSaver.js and Enum management
for Hibernate
5 files added
3 files modified
1 files renamed
changed files
securis/pom.xml patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/common/CodedEnum.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/common/LicenseStatusType.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/common/PackStatusType.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/common/PersistentEnumUserType.java patch | view | blame | history
securis/src/main/webapp/WEB-INF/classes/logging.properties patch | view | blame | history
securis/src/main/webapp/WEB-INF/log4j2.xml patch | view | blame | history
securis/src/main/webapp/WEB-INF/web.xml patch | view | blame | history
securis/src/main/webapp/js/vendor/FileSaver.js patch | view | blame | history
securis/pom.xml
....@@ -33,11 +33,6 @@
3333 <version>3.0.9.Final</version>
3434 </dependency>
3535 <dependency>
36
- <groupId>org.slf4j</groupId>
37
- <artifactId>slf4j-log4j12</artifactId>
38
- <version>1.7.7</version>
39
- </dependency>
40
- <dependency>
4136 <groupId>net.curisit</groupId>
4237 <artifactId>commons-curis</artifactId>
4338 <version>0.0.1-SNAPSHOT</version>
....@@ -72,6 +67,11 @@
7267 <artifactId>resteasy-jackson2-provider</artifactId>
7368 <version>3.0.9.Final</version>
7469 </dependency>
70
+ <dependency>
71
+ <groupId>org.apache.logging.log4j</groupId>
72
+ <artifactId>log4j-web</artifactId>
73
+ <version>2.1</version>
74
+ </dependency>
7575 </dependencies>
7676 <build>
7777 <plugins>
securis/src/main/java/net/curisit/securis/db/common/CodedEnum.java
....@@ -0,0 +1,7 @@
1
+package net.curisit.securis.db.common;
2
+
3
+public interface CodedEnum {
4
+
5
+ public String getCode();
6
+
7
+}
securis/src/main/java/net/curisit/securis/db/common/LicenseStatusType.java
....@@ -0,0 +1,12 @@
1
+package net.curisit.securis.db.common;
2
+
3
+import net.curisit.securis.db.LicenseStatus;
4
+
5
+public class LicenseStatusType extends PersistentEnumUserType<LicenseStatus> {
6
+
7
+ @Override
8
+ public Class<LicenseStatus> returnedClass() {
9
+ return LicenseStatus.class;
10
+ }
11
+
12
+}
securis/src/main/java/net/curisit/securis/db/common/PackStatusType.java
....@@ -0,0 +1,12 @@
1
+package net.curisit.securis.db.common;
2
+
3
+import net.curisit.securis.db.PackStatus;
4
+
5
+public class PackStatusType extends PersistentEnumUserType<PackStatus> {
6
+
7
+ @Override
8
+ public Class<PackStatus> returnedClass() {
9
+ return PackStatus.class;
10
+ }
11
+
12
+}
securis/src/main/java/net/curisit/securis/db/common/PersistentEnumUserType.java
....@@ -0,0 +1,76 @@
1
+package net.curisit.securis.db.common;
2
+
3
+import java.io.Serializable;
4
+import java.sql.PreparedStatement;
5
+import java.sql.ResultSet;
6
+import java.sql.SQLException;
7
+import java.sql.Types;
8
+
9
+import org.hibernate.HibernateException;
10
+import org.hibernate.engine.spi.SessionImplementor;
11
+import org.hibernate.usertype.UserType;
12
+
13
+public abstract class PersistentEnumUserType<T extends CodedEnum> implements UserType {
14
+
15
+ @Override
16
+ public Object assemble(Serializable cached, Object owner) throws HibernateException {
17
+ return cached;
18
+ }
19
+
20
+ @Override
21
+ public Object deepCopy(Object value) throws HibernateException {
22
+ return value;
23
+ }
24
+
25
+ @Override
26
+ public Serializable disassemble(Object value) throws HibernateException {
27
+ return (Serializable) value;
28
+ }
29
+
30
+ @Override
31
+ public boolean equals(Object x, Object y) throws HibernateException {
32
+ return x == y;
33
+ }
34
+
35
+ @Override
36
+ public int hashCode(Object x) throws HibernateException {
37
+ return x == null ? 0 : x.hashCode();
38
+ }
39
+
40
+ @Override
41
+ public boolean isMutable() {
42
+ return false;
43
+ }
44
+
45
+ @Override
46
+ public Object replace(Object original, Object target, Object owner) throws HibernateException {
47
+ return original;
48
+ }
49
+
50
+ @Override
51
+ public abstract Class<T> returnedClass();
52
+
53
+ @Override
54
+ public int[] sqlTypes() {
55
+ return new int[] {
56
+ Types.VARCHAR
57
+ };
58
+ }
59
+
60
+ @Override
61
+ public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
62
+ String code = rs.getString(names[0]);
63
+ for (CodedEnum en : returnedClass().getEnumConstants()) {
64
+ if (en.getCode().equals(code)) {
65
+ return en;
66
+ }
67
+ }
68
+ return null;
69
+ }
70
+
71
+ @Override
72
+ public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
73
+ st.setString(index, value == null ? null : ((CodedEnum) value).getCode());
74
+ }
75
+
76
+}
securis/src/main/webapp/WEB-INF/classes/logging.properties
....@@ -1 +1,3 @@
1
+org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
2
+org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
13 org.apache.catalina.level=INFO
securis/src/main/resources/log4j2.xml
similarity index 87%rename from securis/src/main/resources/log4j2.xmlrename to securis/src/main/webapp/WEB-INF/log4j2.xml
....@@ -3,8 +3,8 @@
33 <Appenders>
44
55 <RollingFile name="defaultFile"
6
- fileName="${sys:user.home}/.SeCuris/logs/securis-server.log" append="true"
7
- filePattern="${sys:user.home}/.SeCuris/logs/securis-server-%d{yyyy-MM-dd-HH}.log.gz">
6
+ fileName="${sys:catalina.home}/logs/securis-server.log" append="true"
7
+ filePattern="${sys:catalina.home}/logs/securis-server-%d{yyyy-MM-dd-HH}.log.gz">
88 <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
99 <Policies>
1010 <TimeBasedTriggeringPolicy interval="1"
securis/src/main/webapp/WEB-INF/web.xml
....@@ -47,10 +47,7 @@
4747 <filter-class>
4848 org.jboss.resteasy.plugins.server.servlet.FilterDispatcher
4949 </filter-class>
50
- <init-param>
51
- <param-name>javax.ws.rs.Application</param-name>
52
- <param-value>net.curisit.securis.RestServicesApplication</param-value>
53
- </init-param>
50
+
5451 </filter>
5552
5653 <filter-mapping>
securis/src/main/webapp/js/vendor/FileSaver.js
....@@ -0,0 +1,245 @@
1
+/* FileSaver.js
2
+ * A saveAs() FileSaver implementation.
3
+ * 2014-08-29
4
+ *
5
+ * By Eli Grey, http://eligrey.com
6
+ * License: X11/MIT
7
+ * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
8
+ */
9
+
10
+/*global self */
11
+/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
12
+
13
+/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
14
+
15
+var saveAs = saveAs
16
+ // IE 10+ (native saveAs)
17
+ || (typeof navigator !== "undefined" &&
18
+ navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
19
+ // Everyone else
20
+ || (function(view) {
21
+ "use strict";
22
+ // IE <10 is explicitly unsupported
23
+ if (typeof navigator !== "undefined" &&
24
+ /MSIE [1-9]\./.test(navigator.userAgent)) {
25
+ return;
26
+ }
27
+ var
28
+ doc = view.document
29
+ // only get URL when necessary in case Blob.js hasn't overridden it yet
30
+ , get_URL = function() {
31
+ return view.URL || view.webkitURL || view;
32
+ }
33
+ , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
34
+ , can_use_save_link = "download" in save_link
35
+ , click = function(node) {
36
+ var event = doc.createEvent("MouseEvents");
37
+ event.initMouseEvent(
38
+ "click", true, false, view, 0, 0, 0, 0, 0
39
+ , false, false, false, false, 0, null
40
+ );
41
+ node.dispatchEvent(event);
42
+ }
43
+ , webkit_req_fs = view.webkitRequestFileSystem
44
+ , req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem
45
+ , throw_outside = function(ex) {
46
+ (view.setImmediate || view.setTimeout)(function() {
47
+ throw ex;
48
+ }, 0);
49
+ }
50
+ , force_saveable_type = "application/octet-stream"
51
+ , fs_min_size = 0
52
+ // See https://code.google.com/p/chromium/issues/detail?id=375297#c7 for
53
+ // the reasoning behind the timeout and revocation flow
54
+ , arbitrary_revoke_timeout = 10
55
+ , revoke = function(file) {
56
+ var revoker = function() {
57
+ if (typeof file === "string") { // file is an object URL
58
+ get_URL().revokeObjectURL(file);
59
+ } else { // file is a File
60
+ file.remove();
61
+ }
62
+ };
63
+ if (view.chrome) {
64
+ revoker();
65
+ } else {
66
+ setTimeout(revoker, arbitrary_revoke_timeout);
67
+ }
68
+ }
69
+ , dispatch = function(filesaver, event_types, event) {
70
+ event_types = [].concat(event_types);
71
+ var i = event_types.length;
72
+ while (i--) {
73
+ var listener = filesaver["on" + event_types[i]];
74
+ if (typeof listener === "function") {
75
+ try {
76
+ listener.call(filesaver, event || filesaver);
77
+ } catch (ex) {
78
+ throw_outside(ex);
79
+ }
80
+ }
81
+ }
82
+ }
83
+ , FileSaver = function(blob, name) {
84
+ // First try a.download, then web filesystem, then object URLs
85
+ var
86
+ filesaver = this
87
+ , type = blob.type
88
+ , blob_changed = false
89
+ , object_url
90
+ , target_view
91
+ , dispatch_all = function() {
92
+ dispatch(filesaver, "writestart progress write writeend".split(" "));
93
+ }
94
+ // on any filesys errors revert to saving with object URLs
95
+ , fs_error = function() {
96
+ // don't create more object URLs than needed
97
+ if (blob_changed || !object_url) {
98
+ object_url = get_URL().createObjectURL(blob);
99
+ }
100
+ if (target_view) {
101
+ target_view.location.href = object_url;
102
+ } else {
103
+ var new_tab = view.open(object_url, "_blank");
104
+ if (new_tab == undefined && typeof safari !== "undefined") {
105
+ //Apple do not allow window.open, see http://bit.ly/1kZffRI
106
+ view.location.href = object_url
107
+ }
108
+ }
109
+ filesaver.readyState = filesaver.DONE;
110
+ dispatch_all();
111
+ revoke(object_url);
112
+ }
113
+ , abortable = function(func) {
114
+ return function() {
115
+ if (filesaver.readyState !== filesaver.DONE) {
116
+ return func.apply(this, arguments);
117
+ }
118
+ };
119
+ }
120
+ , create_if_not_found = {create: true, exclusive: false}
121
+ , slice
122
+ ;
123
+ filesaver.readyState = filesaver.INIT;
124
+ if (!name) {
125
+ name = "download";
126
+ }
127
+ if (can_use_save_link) {
128
+ //blob = new Blob([ blob ], { type : 'application/json' });
129
+ //object_url = (window.URL || window.webkitURL).createObjectURL( blob );
130
+ object_url = get_URL().createObjectURL(blob);
131
+ save_link.href = object_url;
132
+ save_link.download = name;
133
+ click(save_link);
134
+ filesaver.readyState = filesaver.DONE;
135
+ dispatch_all();
136
+ revoke(object_url);
137
+ return;
138
+ }
139
+ // Object and web filesystem URLs have a problem saving in Google Chrome when
140
+ // viewed in a tab, so I force save with application/octet-stream
141
+ // http://code.google.com/p/chromium/issues/detail?id=91158
142
+ // Update: Google errantly closed 91158, I submitted it again:
143
+ // https://code.google.com/p/chromium/issues/detail?id=389642
144
+ if (view.chrome && type && type !== force_saveable_type) {
145
+ slice = blob.slice || blob.webkitSlice;
146
+ blob = slice.call(blob, 0, blob.size, force_saveable_type);
147
+ blob_changed = true;
148
+ }
149
+ // Since I can't be sure that the guessed media type will trigger a download
150
+ // in WebKit, I append .download to the filename.
151
+ // https://bugs.webkit.org/show_bug.cgi?id=65440
152
+ if (webkit_req_fs && name !== "download") {
153
+ name += ".download";
154
+ }
155
+ if (type === force_saveable_type || webkit_req_fs) {
156
+ target_view = view;
157
+ }
158
+ if (!req_fs) {
159
+ fs_error();
160
+ return;
161
+ }
162
+ fs_min_size += blob.size;
163
+ req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) {
164
+ fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) {
165
+ var save = function() {
166
+ dir.getFile(name, create_if_not_found, abortable(function(file) {
167
+ file.createWriter(abortable(function(writer) {
168
+ writer.onwriteend = function(event) {
169
+ target_view.location.href = file.toURL();
170
+ filesaver.readyState = filesaver.DONE;
171
+ dispatch(filesaver, "writeend", event);
172
+ revoke(file);
173
+ };
174
+ writer.onerror = function() {
175
+ var error = writer.error;
176
+ if (error.code !== error.ABORT_ERR) {
177
+ fs_error();
178
+ }
179
+ };
180
+ "writestart progress write abort".split(" ").forEach(function(event) {
181
+ writer["on" + event] = filesaver["on" + event];
182
+ });
183
+ writer.write(blob);
184
+ filesaver.abort = function() {
185
+ writer.abort();
186
+ filesaver.readyState = filesaver.DONE;
187
+ };
188
+ filesaver.readyState = filesaver.WRITING;
189
+ }), fs_error);
190
+ }), fs_error);
191
+ };
192
+ dir.getFile(name, {create: false}, abortable(function(file) {
193
+ // delete file if it already exists
194
+ file.remove();
195
+ save();
196
+ }), abortable(function(ex) {
197
+ if (ex.code === ex.NOT_FOUND_ERR) {
198
+ save();
199
+ } else {
200
+ fs_error();
201
+ }
202
+ }));
203
+ }), fs_error);
204
+ }), fs_error);
205
+ }
206
+ , FS_proto = FileSaver.prototype
207
+ , saveAs = function(blob, name) {
208
+ return new FileSaver(blob, name);
209
+ }
210
+ ;
211
+ FS_proto.abort = function() {
212
+ var filesaver = this;
213
+ filesaver.readyState = filesaver.DONE;
214
+ dispatch(filesaver, "abort");
215
+ };
216
+ FS_proto.readyState = FS_proto.INIT = 0;
217
+ FS_proto.WRITING = 1;
218
+ FS_proto.DONE = 2;
219
+
220
+ FS_proto.error =
221
+ FS_proto.onwritestart =
222
+ FS_proto.onprogress =
223
+ FS_proto.onwrite =
224
+ FS_proto.onabort =
225
+ FS_proto.onerror =
226
+ FS_proto.onwriteend =
227
+ null;
228
+
229
+ return saveAs;
230
+}(
231
+ typeof self !== "undefined" && self
232
+ || typeof window !== "undefined" && window
233
+ || this.content
234
+));
235
+// `self` is undefined in Firefox for Android content script context
236
+// while `this` is nsIContentFrameMessageManager
237
+// with an attribute `content` that corresponds to the window
238
+
239
+if (typeof module !== "undefined" && module !== null) {
240
+ module.exports = saveAs;
241
+} else if ((typeof define !== "undefined" && define !== null) && (define.amd != null)) {
242
+ define([], function() {
243
+ return saveAs;
244
+ });
245
+}