/*
 * The MIT License
 *
 * Copyright 2015 Ivar Grimstad (ivar.grimstad@gmail.com).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package eu.agilejava.snoopee.api;

import eu.agilejava.snoopee.SnoopEEClientRegistry;
import eu.agilejava.snoopee.SnoopEEConfig;
import java.util.Collection;
import javax.ejb.EJB;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import javax.ws.rs.core.Response;

/**
 *
 * @author Ivar Grimstad (ivar.grimstad@gmail.com)
 */
@Path("services")
public class ServicesResource {

   @EJB
   private SnoopEEClientRegistry snoopeeClientRegistry;

   @GET
   @Produces(APPLICATION_JSON)
   public Response all() {

      final Collection<SnoopEEConfig> serviceConfigs = snoopeeClientRegistry.getServiceConfigs();

      return Response.ok(new GenericEntity<Collection<SnoopEEConfig>>(serviceConfigs) {})
              .header("Access-Control-Allow-Origin", "*").build();
   }

   @GET
   @Produces(APPLICATION_JSON)
   @Path("{serviceId}")
   public Response lookup(@PathParam("serviceId") String serviceId) {

      return Response.ok(snoopeeClientRegistry.getClientConfig(serviceId)
              .orElseThrow(NotFoundException::new)).build();
   }
}

@@ -1336,16 +1336,43 @@
 jb.EJB;%0A
+import javax.ws.rs.DELETE;%0A
 import j
@@ -1421,24 +1421,73 @@
 dException;%0A
+import javax.ws.rs.POST;%0Aimport javax.ws.rs.PUT;%0A
 import javax
@@ -1558,16 +1558,49 @@
 oduces;%0A
+import javax.ws.rs.core.Context;%0A
 import j
@@ -1722,16 +1722,49 @@
 esponse;
+%0Aimport javax.ws.rs.core.UriInfo;
 %0A%0A/**%0A *
@@ -1874,16 +1874,60 @@
  %7B%0A%0A
+    @Context%0A    private UriInfo uriInfo;%0A%0A 
    @EJB%0A
    p
@@ -1922,16 +1922,17 @@
    @EJB%0A
+ 
    priva
@@ -1979,29 +1979,31 @@
 gistry;%0A%0A   
+ 
 @GET%0A
+ 
    @Produces
@@ -2021,16 +2021,17 @@
 N_JSON)%0A
+ 
    publi
@@ -2046,24 +2046,26 @@
 se all() %7B%0A%0A
+  
       final 
@@ -2151,16 +2151,18 @@
 igs();%0A%0A
+  
       re
@@ -2244,11 +2244,22 @@
 s) %7B
+%0A        
 %7D)%0A
+  
     
@@ -2328,19 +2328,21 @@
 %0A   
+ 
 %7D%0A%0A
+ 
    @GET%0A
    @
@@ -2337,16 +2337,17 @@
    @GET%0A
+ 
    @Prod
@@ -2369,16 +2369,17 @@
 N_JSON)%0A
+ 
    @Path
@@ -2397,16 +2397,17 @@
 d%7D%22)%0A   
+ 
 public R
@@ -2462,24 +2462,26 @@
 rviceId) %7B%0A%0A
+  
       return
@@ -2556,16 +2556,18 @@
         
+ 
+ 
 .orElseT
@@ -2608,12 +2608,878 @@
 d();%0A   
-%7D
+ %7D%0A%0A    @POST%0A    public Response register(final SnoopEEConfig serviceConfig) %7B%0A        snoopeeClientRegistry.register(serviceConfig);%0A        return Response.created(uriInfo.getAbsolutePathBuilder().segment(serviceConfig.getServiceName()).build()).build();%0A    %7D%0A%0A    @PUT%0A    @Path(%22%7BserviceName%7D%22)%0A    public Response updateStatus(@PathParam(%22serviceName%22) String serviceName, final SnoopEEConfig serviceConfig) %7B%0A        if (serviceConfig != null) %7B%0A            snoopeeClientRegistry.register(serviceConfig);%0A        %7D else %7B%0A            snoopeeClientRegistry.deRegister(serviceName);%0A        %7D%0A        return Response.ok().build();%0A    %7D%0A%0A    @DELETE%0A    @Path(%22%7BserviceName%7D%22)%0A    public Response deRegister(@PathParam(%22serviceName%22) String serviceName) %7B%0A        snoopeeClientRegistry.deRegister(serviceName);%0A        return Response.accepted().build();%0A    %7D%0A
 %0A%7D%0A

ADD REST api for service registration and health heartbeats