/*
 * Copyright 2018 NEOautus Ltd. (http://neoautus.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.lucidj.artifactdeployer;

import org.lucidj.api.core.Artifact;
import org.lucidj.api.core.ArtifactDeployer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;

@Component (immediate = true, publicFactory = false)
@Instantiate
@Provides (specifications = HttpServlet.class)
public class DeploySvc extends HttpServlet
{
    @Requires
    private ArtifactDeployer artifactDeployer;

    @ServiceProperty (name="@service.path")
    String service = "/deploy";

    @Override
    public void doGet (HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        resp.setContentType ("text/plain");
        PrintWriter out = resp.getWriter ();

        if (req.getQueryString () == null)
        {
            // Just the service returns true indicating it exists
            out.write ("true\n");
            return;
        }

        out.write ("### Hello from " + this.getClass ().getName () + " ###\n");
        out.write ("Artifact: " + req.getQueryString () + "\n");

        Artifact artifact = artifactDeployer.getArtifact (req.getQueryString ());

        if (artifact == null)
        {
            try
            {
                artifact = artifactDeployer.installArtifact (req.getQueryString ());
                out.write ("Artifact installed: " + artifact.toString ());
                artifact.update ();
            }
            catch (Exception e)
            {
                out.write ("Exception deploying file: " + e.getMessage ());
            }
        }
        else
        {
            out.write ("Update artifact: " + artifact.toString ());
            artifact.update ();
        }
    }
}

// EOF

@@ -720,32 +720,89 @@
 rtifactDeployer;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport javax.s
@@ -1445,16 +1445,98 @@
 rvlet%0A%7B%0A
+    private final static Logger log = LoggerFactory.getLogger (DeploySvc.class);%0A%0A
     @Req
@@ -1893,25 +1893,24 @@
 ter ();%0A
-%0A
         
 if (req.
@@ -1905,33 +1905,80 @@
     
-if (req.getQueryString ()
+String artifact_file = req.getQueryString ();%0A%0A        if (artifact_file
  == 
@@ -2242,39 +2242,90 @@
 %22 + 
-req.getQueryString () + %22%5Cn%22);%0A
+artifact_file + %22%5Cn%22);%0A%0A        log.info (%22Install requested: %7B%7D%22, artifact_file);
 %0A   
@@ -2379,37 +2379,29 @@
 tifact (
-req.getQueryString ()
+artifact_file
 );%0A%0A    
@@ -2527,37 +2527,29 @@
 tifact (
-req.getQueryString ()
+artifact_file
 );%0A     
@@ -2622,44 +2622,8 @@
 ));%0A
-                artifact.update ();%0A
     

Remove stray update().