package bzh.medek.server.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import bzh.medek.server.json.JsonResponse;
import bzh.medek.server.json.ping.JsonPingResp;

@Path(value = "/server")
public class PingService {

    private static final Logger LOGGER = Logger.getLogger(PingService.class);

    public PingService() {
    }

    /**
     *  /ping
     * 
     * @param servletContext
     * @return
     */
    @GET
    @Path(value = "/ping")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonResponse ping(@Context ServletContext servletContext) {
        String serverRev = "";
        String appVersion = "";
        InputStreamReader read = null;
        InputStream input = null;
        BufferedReader reader = null;
        try {
            input = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
            // server
            read = new InputStreamReader(input);
            reader = new BufferedReader(read);
            String line = reader.readLine();
            while (line != null) {
                if (line.contains("Implementation-Build: ")) {
                    serverRev = line.substring(line.indexOf(' ') + 1);
                }
                if (line.contains("Implementation-Version: ")) {
                    appVersion = line.substring(line.indexOf(' ') + 1);
                }
                line = reader.readLine();
            }
            reader.close();
            read.close();
            input.close();
        } catch (IOException e) {
            LOGGER.error("while getting info from manifest:", e);
        } catch (NullPointerException e) {
            LOGGER.error("while trying to read manifest:", e);
            return new JsonPingResp("dev", "test");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    LOGGER.error("while getting manifest file (closing bufferedreader) :", e);
                }
            }
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    LOGGER.error("while getting manifest file (closing inputstreamreader) :", e);
                }
            }
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    LOGGER.error("while getting manifest file (closing inputstream) :", e);
                }
            }
        }

        return new JsonPingResp(serverRev, appVersion);
    }

}

@@ -164,30 +164,123 @@
 vax.
-servlet.ServletContext
+ejb.Stateless;%0Aimport javax.servlet.ServletContext;%0Aimport javax.ws.rs.ApplicationPath;%0Aimport javax.ws.rs.Consumes
 ;%0Aim
@@ -351,24 +351,61 @@
 s.Produces;%0A
+import javax.ws.rs.core.Application;%0A
 import javax
@@ -591,30 +591,145 @@
 ;%0A%0A@
-Path(value = %22/server%22
+Stateless%0A@ApplicationPath(%22/services%22)%0A@Path(value = %22/ping%22)%0A@Consumes(MediaType.APPLICATION_JSON)%0A@Produces(MediaType.APPLICATION_JSON
 )%0Apu
@@ -751,16 +751,36 @@
 Service 
+extends Application 
 %7B%0A%0A    p
@@ -969,16 +969,16 @@
      */%0A
+
     @GET
@@ -982,77 +982,8 @@
 GET%0A
-    @Path(value = %22/ping%22)%0A    @Produces(MediaType.APPLICATION_JSON)%0A
     

update pingservice to be identical to user service