12
12
* limitations under the License.
13
13
*/
14
14
15
- package de .ubleipzig .iiif . dynamic .camel ;
15
+ package de .ubleipzig .dynamo .camel ;
16
16
17
- import static de .ubleipzig .iiif .dynamic .JsonLdProcessorUtils .toJsonLd ;
18
- import static de .ubleipzig .iiif .dynamic .QueryUtils .getQuery ;
19
17
import static de .ubleipzig .webanno .AbstractSerializer .serialize ;
20
18
import static java .net .URLEncoder .encode ;
21
19
import static org .apache .camel .Exchange .CONTENT_TYPE ;
22
20
import static org .apache .camel .Exchange .HTTP_CHARACTER_ENCODING ;
23
21
import static org .apache .camel .Exchange .HTTP_METHOD ;
22
+ import static org .apache .camel .Exchange .HTTP_QUERY ;
24
23
import static org .apache .camel .Exchange .HTTP_RESPONSE_CODE ;
25
24
import static org .apache .camel .LoggingLevel .INFO ;
26
25
27
26
import com .github .jsonldjava .core .JsonLdError ;
28
27
28
+ import de .ubleipzig .dynamo .JsonLdProcessorUtils ;
29
+ import de .ubleipzig .dynamo .QueryUtils ;
29
30
import de .ubleipzig .webanno .CollectionBuilder ;
30
31
import de .ubleipzig .webanno .ManifestBuilder ;
31
32
import de .ubleipzig .webanno .templates .AnnotationList ;
32
33
33
34
import java .io .IOException ;
35
+ import java .io .InputStream ;
34
36
import java .io .UncheckedIOException ;
37
+ import java .util .Hashtable ;
38
+ import java .util .Objects ;
39
+ import java .util .Properties ;
40
+
41
+ import javax .naming .Context ;
42
+ import javax .naming .InitialContext ;
35
43
36
44
import org .apache .camel .Exchange ;
37
45
import org .apache .camel .builder .RouteBuilder ;
38
- import org .apache .camel .component . properties . PropertiesComponent ;
46
+ import org .apache .camel .impl . JndiRegistry ;
39
47
import org .apache .camel .main .Main ;
40
48
import org .apache .camel .main .MainListenerSupport ;
41
49
import org .apache .camel .main .MainSupport ;
50
+ import org .apache .camel .util .IOHelper ;
42
51
import org .slf4j .Logger ;
43
52
import org .slf4j .LoggerFactory ;
53
+ import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
54
+ import org .springframework .data .redis .core .RedisTemplate ;
55
+ import org .springframework .data .redis .serializer .StringRedisSerializer ;
44
56
45
57
46
58
/**
50
62
*/
51
63
public class Dynamo {
52
64
65
+ private static final Logger LOGGER = LoggerFactory .getLogger (Dynamo .class );
66
+ private static final JedisConnectionFactory CONNECTION_FACTORY = new JedisConnectionFactory ();
67
+ private static RedisTemplate <String , String > redisTemplate ;
68
+
69
+ static {
70
+ CONNECTION_FACTORY .setHostName ("redis" );
71
+ CONNECTION_FACTORY .setPort (6379 );
72
+ CONNECTION_FACTORY .afterPropertiesSet ();
73
+ }
74
+
53
75
/**
54
76
* @param args String[]
55
77
* @throws Exception Exception
@@ -66,6 +88,13 @@ private void init() throws Exception {
66
88
final Main main = new Main ();
67
89
main .addRouteBuilder (new QueryRoute ());
68
90
main .addMainListener (new Events ());
91
+ final JndiRegistry registry = new JndiRegistry (createInitialContext ());
92
+ redisTemplate = new RedisTemplate <>();
93
+ redisTemplate .setConnectionFactory (CONNECTION_FACTORY );
94
+ redisTemplate .setDefaultSerializer (new StringRedisSerializer ());
95
+ redisTemplate .afterPropertiesSet ();
96
+ Objects .requireNonNull (registry ).bind ("redisTemplate" , redisTemplate );
97
+ main .setPropertyPlaceholderLocations ("file:${env:DYNAMO_HOME}/de.ubleipzig.dynamo.cfg" );
69
98
main .run ();
70
99
}
71
100
@@ -90,8 +119,6 @@ public void beforeStop(final MainSupport main) {
90
119
*/
91
120
public static class QueryRoute extends RouteBuilder {
92
121
93
- private static final Logger LOGGER = LoggerFactory .getLogger (Dynamo .class );
94
-
95
122
private static final String contentTypeNTriples = "application/n-triples" ;
96
123
private static final String contentTypeJsonLd = "application/ld+json" ;
97
124
private static final String contentTypeHTMLForm = "application/x-www-form-urlencoded" ;
@@ -106,10 +133,7 @@ public static class QueryRoute extends RouteBuilder {
106
133
*/
107
134
public void configure () {
108
135
109
- final PropertiesComponent pc = getContext ().getComponent ("properties" , PropertiesComponent .class );
110
- pc .setLocation ("classpath:application.properties" );
111
-
112
- from ("jetty:http://{{rest.host}}:{{rest.port}}{{rest.prefix}}?"
136
+ from ("jetty:http://{{dynamo.host}}:{{dynamo.port}}{{dynamo.prefix}}?"
113
137
+ "optionsEnabled=true&matchOnUriPrefix=true&sendServerVersion=false"
114
138
+ "&httpMethodRestrict=GET,OPTIONS" )
115
139
.routeId ("Sparqler" )
@@ -118,9 +142,24 @@ public void configure() {
118
142
.constant ("*" )
119
143
.choice ()
120
144
.when (header (HTTP_METHOD ).isEqualTo ("GET" ))
145
+ .to ("direct:redis-get" );
146
+ from ("direct:redis-get" )
147
+ .routeId ("RedisGet" )
148
+ .process (e -> {
149
+ final String httpquery = e .getIn ().getHeader (HTTP_QUERY ).toString ();
150
+ if (redisTemplate .opsForValue ().get (httpquery ) != null ) {
151
+ e .getIn ().setBody (redisTemplate .opsForValue ().get (httpquery ));
152
+ LOGGER .info ("Getting query result from Redis Cache" );
153
+ } else {
154
+ e .getIn ().setHeader (CONTENT_TYPE , EMPTY );
155
+ }
156
+ })
157
+ .choice ()
158
+ .when (header (CONTENT_TYPE ).isEqualTo (EMPTY ))
121
159
.to ("direct:sparql" );
122
160
from ("direct:sparql" )
123
161
.routeId ("SparqlerGet" )
162
+ .removeHeader (CONTENT_TYPE )
124
163
.choice ()
125
164
.when (header (SPARQL_QUERY ).isEqualTo ("meta" ))
126
165
.to ("direct:getMetaGraph" )
@@ -135,7 +174,7 @@ public void configure() {
135
174
.constant (contentTypeNTriples )
136
175
.process (e -> e .getIn ()
137
176
.setBody (sparqlConstruct (
138
- getQuery ("canvas-anno.sparql" , getV1 (e ), getV2 (e )))))
177
+ QueryUtils . getQuery ("canvas-anno.sparql" , getV1 (e ), getV2 (e )))))
139
178
.to ("http4:{{triplestore.baseUrl}}?useSystemProperties=true&bridgeEndpoint=true" )
140
179
.filter (header (HTTP_RESPONSE_CODE ).isEqualTo (200 ))
141
180
.setHeader (CONTENT_TYPE )
@@ -151,7 +190,7 @@ public void configure() {
151
190
.setHeader (HTTP_ACCEPT )
152
191
.constant (contentTypeNTriples )
153
192
.process (e -> e .getIn ()
154
- .setBody (sparqlConstruct (getQuery ("collection.sparql" ))))
193
+ .setBody (sparqlConstruct (QueryUtils . getQuery ("collection.sparql" ))))
155
194
.to ("http4:{{triplestore.baseUrl}}?useSystemProperties=true&bridgeEndpoint=true" )
156
195
.filter (header (HTTP_RESPONSE_CODE ).isEqualTo (200 ))
157
196
.setHeader (CONTENT_TYPE )
@@ -206,7 +245,8 @@ public void configure() {
206
245
.getBody ()
207
246
.toString ());
208
247
e .getIn ().setBody (builder .build ());
209
- });
248
+ })
249
+ .to ("direct:redis-put" );
210
250
from ("direct:buildCollection" )
211
251
.routeId ("CollectionBuilder" )
212
252
.removeHeader (HTTP_ACCEPT )
@@ -220,6 +260,18 @@ public void configure() {
220
260
.getBody ()
221
261
.toString ());
222
262
e .getIn ().setBody (builder .build ());
263
+ })
264
+ .to ("direct:redis-put" );
265
+ from ("direct:redis-put" )
266
+ .routeId ("RedisPut" )
267
+ .log (INFO , LOGGER , "Storing query result in Redis Cache" )
268
+ .process (e -> {
269
+ final String httpquery = e .getIn ().getHeader (HTTP_QUERY ).toString ();
270
+ final String body = e .getIn ().getBody ().toString ();
271
+ if (null == redisTemplate .opsForValue ().get (httpquery )) {
272
+ redisTemplate .opsForValue ().set (httpquery , body );
273
+ }
274
+ e .getIn ().setBody (redisTemplate .opsForValue ().get (httpquery ));
223
275
});
224
276
}
225
277
@@ -260,12 +312,31 @@ private void processJsonLdExchange(final Exchange e, final String contextUri, fi
260
312
final String body = e .getIn ().getBody ().toString ();
261
313
if ( body != null && !body .isEmpty ()) {
262
314
e .getIn ()
263
- .setBody (toJsonLd (e .getIn ()
264
- .getBody ()
265
- .toString (), contextUri , frameUri ));
315
+ .setBody (JsonLdProcessorUtils . toJsonLd (e .getIn ()
316
+ .getBody ()
317
+ .toString (), contextUri , frameUri ));
266
318
} else {
267
319
e .getIn ().setHeader (CONTENT_TYPE , EMPTY );
268
320
}
269
321
}
270
322
}
323
+
324
+
325
+ /**
326
+ *
327
+ * @return InitialContext Context
328
+ * @throws Exception Exception
329
+ */
330
+ private static Context createInitialContext () throws Exception {
331
+ final InputStream in = Dynamo .class
332
+ .getClassLoader ()
333
+ .getResourceAsStream ("jndi.properties" );
334
+ try {
335
+ final Properties properties = new Properties ();
336
+ properties .load (in );
337
+ return new InitialContext (new Hashtable <>(properties ));
338
+ } finally {
339
+ IOHelper .close (in );
340
+ }
341
+ }
271
342
}
0 commit comments