Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Commit 70d292f

Browse files
committed
REST server/client
1 parent 8f3f37b commit 70d292f

File tree

13 files changed

+214
-0
lines changed

13 files changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="bin" path="src"/>
4+
<classpathentry kind="src" output="bin_test" path="test"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
6+
<classpathentry kind="con" path="aQute.bnd.classpath.container"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/generated/
2+
/bin/
3+
/bin_test/
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>osgi.enroute.trains.rest.provider</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>bndtools.core.bndbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>bndtools.core.bndnature</nature>
22+
</natures>
23+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.8
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.8
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# OSGI ENROUTE TRAINS REST PROVIDER BUNDLE
3+
#
4+
5+
6+
Bundle-Description: \
7+
A bundle with a provider. Notice that this provider exports the API package. \
8+
It also provides a JUnit test and it can be run standalone. \
9+
\
10+
${warning;Please update this Bundle-Description in osgi.enroute.trains.rest.provider/bnd.bnd}
11+
12+
13+
-buildpath: \
14+
osgi.enroute.base.api;version=1.0,\
15+
osgi.enroute.trains.api;version=latest,\
16+
biz.aQute.bndlib;packages=*
17+
18+
-testpath: \
19+
osgi.enroute.junit.wrapper;version=4.12
20+
21+
-includeresource: {readme.md}
22+
23+
24+
-runrequires: \
25+
osgi.identity;filter:='(osgi.identity=osgi.enroute.trains.rest.provider)'
26+
27+
-runbundles: \
28+
${error;Resolve first}
29+
-sub: *.bnd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Export-Package: \
2+
osgi.enroute.trains.cloud.api,\
3+
osgi.enroute.trains.rest.client,\
4+
org.osgi.dto,\
5+
osgi.enroute.dto.api
6+
7+
Conditional-Package: aQute.lib*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# OSGI ENROUTE TRAINS REST PROVIDER
2+
3+
${Bundle-Description}
4+
5+
## Example
6+
7+
## Configuration
8+
9+
Pid: osgi.enroute.trains.rest
10+
11+
Field Type Description
12+
13+
14+
## References
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-dsannotations: *
2+
-metatypeannotations: *
3+
Bundle-Version: 1.0.0.${tstamp}
4+
Private-Package: osgi.enroute.trains.rest.provider
5+
Service-Component: *
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package osgi.enroute.trains.rest.client;
2+
3+
import java.io.InputStream;
4+
import java.io.UnsupportedEncodingException;
5+
import java.net.URI;
6+
import java.net.URL;
7+
import java.net.URLEncoder;
8+
import java.util.List;
9+
10+
import org.osgi.service.component.annotations.Reference;
11+
12+
import aQute.lib.converter.TypeReference;
13+
import aQute.lib.json.Decoder;
14+
import aQute.lib.json.JSONCodec;
15+
import osgi.enroute.dto.api.DTOs;
16+
import osgi.enroute.trains.cloud.api.Observation;
17+
18+
public class TrackClient {
19+
static JSONCodec codec = new JSONCodec();
20+
static TypeReference<List<Observation>> LISTOBSERVATIONS = new TypeReference<List<Observation>>() {};
21+
final URI base;
22+
23+
@Reference
24+
DTOs dtos;
25+
26+
public TrackClient(URI base) {
27+
this.base = base;
28+
}
29+
30+
public boolean blocked(String segment, String reason, boolean blocked) throws Exception {
31+
return (Boolean) send( "blocked", segment, reason, blocked).get();
32+
}
33+
34+
Decoder send( Object ... params ) throws Exception {
35+
StringBuilder sb = new StringBuilder();
36+
String del = base.toString();
37+
if ( !del.endsWith("/"))
38+
sb.append("/");
39+
40+
for ( Object p : params ) {
41+
sb.append(del).append( encode(p));
42+
del = "/";
43+
}
44+
InputStream inputStream = new URL(sb.toString()).openConnection().getInputStream();
45+
return codec.dec().from(inputStream);
46+
}
47+
48+
49+
private String encode(Object blocked) throws UnsupportedEncodingException {
50+
return URLEncoder.encode( ""+blocked, "UTF-8");
51+
}
52+
53+
public List<Observation> getRecentObservations(long time) throws Exception {
54+
return send("observations", time).get(LISTOBSERVATIONS);
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@org.osgi.annotation.versioning.Version("1.0.0")
2+
package osgi.enroute.trains.rest.client;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package osgi.enroute.trains.rest.provider;
2+
3+
import java.util.List;
4+
5+
import org.osgi.service.component.annotations.Component;
6+
import org.osgi.service.component.annotations.Reference;
7+
8+
import osgi.enroute.rest.api.REST;
9+
import osgi.enroute.trains.cloud.api.Observation;
10+
import osgi.enroute.trains.cloud.api.TrackForSegment;
11+
12+
/**
13+
*
14+
*/
15+
@Component(name = "osgi.enroute.trains.rest", immediate=true)
16+
public class RestImpl implements REST {
17+
18+
@Reference
19+
TrackForSegment ts;
20+
public List<Observation> getObservations(long time) {
21+
return ts.getRecentObservations(time);
22+
}
23+
24+
public boolean getBlocked(String segment, String reason,boolean blocked) {
25+
ts.blocked(segment, reason, blocked);
26+
return true;
27+
}
28+
}

osgi.enroute.trains.rest.provider/test/.gitignore

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package osgi.enroute.trains.rest.provider;
2+
3+
import java.net.URI;
4+
5+
import junit.framework.TestCase;
6+
import osgi.enroute.trains.rest.client.TrackClient;
7+
8+
/*
9+
*
10+
*
11+
*
12+
*/
13+
14+
public class RestImplTest extends TestCase {
15+
16+
public void testX() throws Exception {
17+
TrackClient tc = new TrackClient(new URI("http://localhost:8080/rest/"));
18+
tc.blocked("A06", "WTF", true);
19+
}
20+
21+
public void testY() throws Exception {
22+
TrackClient tc = new TrackClient(new URI("http://localhost:8080/rest/"));
23+
while(true)
24+
System.out.println(tc.getRecentObservations(0));
25+
}
26+
}

0 commit comments

Comments
 (0)