View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  
35  package fr.paris.lutece.plugins.knowledge.rs;
36  import fr.paris.lutece.util.json.ErrorJsonResponse;
37  import fr.paris.lutece.portal.service.security.SecurityService;
38  import javax.ws.rs.GET;
39  import javax.ws.rs.POST;
40  import javax.ws.rs.Path;
41  import javax.ws.rs.Produces;
42  import javax.ws.rs.core.MediaType;
43  import javax.ws.rs.core.Response;
44  import fr.paris.lutece.plugins.knowledge.service.ChatService;
45  import org.glassfish.jersey.media.sse.EventOutput;
46  import org.glassfish.jersey.media.sse.OutboundEvent;
47  import org.glassfish.jersey.media.sse.SseFeature;
48  
49  import java.io.IOException;
50  import java.util.UUID;
51  import java.util.concurrent.CompletableFuture;
52  import java.util.concurrent.ConcurrentHashMap;
53  import javax.servlet.http.HttpServletRequest;
54  import javax.ws.rs.Consumes;
55  import javax.ws.rs.QueryParam;
56  import javax.ws.rs.core.Context;
57  
58  /**
59   * ProjectRest
60   */
61  @Path( Constants.API_PATH + Constants.VERSION_PATH + Constants.BOT_PATH )
62  public class BotRest
63  {
64      private static ConcurrentHashMap<String, EventOutput> sessions = new ConcurrentHashMap<>( );
65  
66      @POST
67      @Path( "/chat" )
68      @Consumes( MediaType.APPLICATION_JSON )
69      @Produces( MediaType.APPLICATION_JSON )
70      public Response postChat( RequestData data, @Context HttpServletRequest request )
71      {
72          if ( SecurityService.getInstance( ).getRegisteredUser( request ) == null )
73          {
74              return Response.status( Response.Status.UNAUTHORIZED ).entity( new ErrorJsonResponse( "Unauthorized" ) ).build( );
75          }
76  
77          String sessionId = UUID.randomUUID( ).toString( );
78          final EventOutput eventOutput = new EventOutput( );
79          sessions.put( sessionId, eventOutput );
80          CompletableFuture<Void> future = ChatService.run( request, data, eventOutput, sessionId );
81          future.whenComplete( ( result, ex ) -> {
82              try
83              {
84  
85                  OutboundEvent closeEvent = new OutboundEvent.Builder( ).name( "message" ).mediaType( MediaType.APPLICATION_JSON_TYPE )
86                          .data( String.class, "CLOSE" ).build( );
87                  eventOutput.write( closeEvent );
88                  eventOutput.close( );
89  
90              }
91              catch( IOException ioClose )
92              {
93                  throw new RuntimeException( "Error when closing the event output.", ioClose );
94              }
95          } );
96  
97          return Response.ok( ).entity( "{\"sessionId\":\"" + sessionId + "\"}" ).build( );
98      }
99  
100     @GET
101     @Path( "/chat/sse" )
102     @Produces( SseFeature.SERVER_SENT_EVENTS )
103     public EventOutput getChatEvents( @QueryParam( "sessionId" ) String sessionId, @Context HttpServletRequest request )
104     {
105         if ( SecurityService.getInstance( ).getRegisteredUser( request ) == null )
106         {
107             return null;
108         }
109         return sessions.get( sessionId );
110     }
111 
112 }