1111import javax .ws .rs .core .Context ;
1212import javax .ws .rs .core .Response ;
1313import java .io .IOException ;
14+ import java .nio .file .Files ;
1415import java .util .HashMap ;
1516import java .util .Map ;
1617
1718import static java .util .Objects .requireNonNull ;
1819
20+ @ Path ("/" )
1921public class Api {
2022 private final java .nio .file .Path dir ;
2123 private final long CHUNK_SIZE ;
@@ -28,17 +30,20 @@ public class Api {
2830
2931 @ Path ("/{topic}/{username}" )
3032 @ POST
31- public void subscribe (@ PathParam ("topic" ) String topic ,
33+ public Response subscribe (@ PathParam ("topic" ) String topic ,
3234 @ PathParam ("username" ) String username ) throws IOException {
3335 ensureTopic (topic ).subscribe (username );
36+ return Response .ok ().build ();
3437 }
3538
3639 private Queue ensureTopic (@ PathParam ("topic" ) String topic ) throws IOException {
3740 Queue q ;
3841 synchronized (topics ) {
3942 q = topics .get (topic );
4043 if (q == null ) {
41- q = new Queue (dir , CHUNK_SIZE );
44+ java .nio .file .Path p = dir .resolve (topic );
45+ Files .createDirectory (p );
46+ q = new Queue (p , CHUNK_SIZE );
4247 topics .put (topic , q );
4348 }
4449 }
@@ -47,26 +52,28 @@ private Queue ensureTopic(@PathParam("topic") String topic) throws IOException {
4752
4853 @ Path ("/{topic}/{username}" )
4954 @ DELETE
50- public void unsubscribe (@ PathParam ("topic" ) String topic ,
55+ public Response unsubscribe (@ PathParam ("topic" ) String topic ,
5156 @ PathParam ("username" ) String username ) throws IOException {
5257 Queue q ;
5358 synchronized (topics ) {
5459 q = topics .get (topic );
5560 }
5661
5762 if (q == null ) {
58- throw new ClientErrorException (Response .Status .NOT_FOUND );
63+ return Response . status (Response .Status .NOT_FOUND ). build ( );
5964 } else {
6065 q .unsubscribe (username );
66+ return Response .ok ().build ();
6167 }
6268 }
6369
6470 @ Path ("/{topic}" )
6571 @ POST
66- public void publish (@ PathParam ("topic" ) String topic ,
72+ public Response publish (@ PathParam ("topic" ) String topic ,
6773 @ Context Request request ) throws IOException {
6874 ensureTopic (topic ).post (request .getInputStream ());
6975 request .getInputStream ().close ();
76+ return Response .ok ().build ();
7077 }
7178
7279 @ Path ("/{topic}/{username}" )
0 commit comments