package io.corbel.resources.rem.plugin;

import io.corbel.resources.rem.*;
import io.corbel.resources.rem.ioc.RemImageIoc;
import io.corbel.resources.rem.ioc.RemImageIocNames;
import io.corbel.lib.config.ConfigurationHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

@Component
public class ImageRemPlugin extends RemPlugin {

    private static final Logger LOG = LoggerFactory.getLogger(ImageRemPlugin.class);
    private static final String IMAGE_CACHE_COLLECTION = "image.cache.collection";
    private static final String IMAGE_PATH = "image/*";
    private final String ARTIFACT_ID = "rem-image";

    @Override
    protected void init() {
        LOG.info("Initializing Image plugin.");
        super.init();
        ConfigurationHelper.setConfigurationNamespace(ARTIFACT_ID);
        context = new AnnotationConfigApplicationContext(RemImageIoc.class);
    }

    @Override
    protected void register(RemRegistry registry) {
        String cacheCollection = context.getEnvironment().getProperty(IMAGE_CACHE_COLLECTION);
        registerRem(RemImageIocNames.REM_GET,HttpMethod.GET, registry, cacheCollection);
        registerRem(RemImageIocNames.REM_PUT,HttpMethod.PUT, registry, cacheCollection);
        registerRem(RemImageIocNames.REM_DELETE,HttpMethod.DELETE, registry, cacheCollection);
    }

    private void registerRem(String remImageIocNames, HttpMethod httpMethod, RemRegistry registry, String cacheCollection) {
        ImageGetRem beanImageRem = (ImageGetRem) context.getBean(remImageIocNames, Rem.class);
        beanImageRem.setRemService(remService);
        registry.registerRem(beanImageRem, "^(?!" + cacheCollection + "$).*",
                MediaType.parseMediaType(IMAGE_PATH), httpMethod);
    }

    @Override
    protected String getArtifactName() {
        return ARTIFACT_ID;
    }

}

@@ -643,147 +643,8 @@
 s);%0A
-    private static final String IMAGE_CACHE_COLLECTION = %22image.cache.collection%22;%0A    private static final String IMAGE_PATH = %22image/*%22;%0A
     
@@ -1034,192 +1034,166 @@
     
-String cacheCollection = context.getEnvironment().getProperty(IMAGE_CACHE_COLLECTION);%0A        registerRem(RemImageIocNames.REM_GET,HttpMethod.GET, registry, cacheCollection
+ImageGetRem beanImageGetRem = (ImageGetRem) context.getBean(RemImageIocNames.REM_GET, Rem.class);%0A        beanImageGetRem.setRemService(remService
 );%0A
+%0A
         
 regi
@@ -1192,97 +1192,166 @@
     
-registerRem(RemImageIocNames.REM_PUT,HttpMethod.PUT, registry, cacheCollection
+ImagePutRem beanImagePutRem = (ImagePutRem) context.getBean(RemImageIocNames.REM_PUT, Rem.class);%0A        beanImagePutRem.setRemService(remService
 );%0A
+%0A
         
 regi
@@ -1346,27 +1346,84 @@
         
-regis
+ImageDeleteRem beanImageDeleteRem = (ImageDele
 te
-r
 Rem
+) context.getBean
 (RemImag
@@ -1447,174 +1447,159 @@
 ETE,
-HttpMethod.DELETE, registry, cacheCollection
+ Rem.class
 );%0A    
-%7D%0A%0A
     
-private void registerRem(String remImageIocNames, HttpMethod httpMethod, RemRegistry registry, String 
+beanImageDeleteRem.setRemService(remService);%0A        String cacheCollection = context.getEnvironment().getProperty(%22image.
 cache
-C
+.c
 olle
@@ -1603,19 +1603,20 @@
 llection
-) %7B
+%22);%0A
 %0A       
@@ -1612,36 +1612,45 @@
 );%0A%0A        
-ImageGetRem 
+registry.registerRem(
 beanImageRem
@@ -1650,119 +1650,269 @@
 mage
-Rem = (ImageGetRem) context.getBean(remImageIocNames, Rem.class);%0A        beanImageRem.setRemService(remService
+GetRem, %22%5E(?!%22 + cacheCollection + %22$).*%22,%0A                MediaType.parseMediaType(%22image/*%22), HttpMethod.GET);%0A        registry.registerRem(beanImagePutRem, %22%5E(?!%22 + cacheCollection + %22$).*%22,%0A                MediaType.parseMediaType(%22image/*%22), HttpMethod.PUT
 );%0A 
@@ -1948,16 +1948,22 @@
 eanImage
+Delete
 Rem, %22%5E(
@@ -2039,22 +2039,21 @@
 ype(
-IMAGE_PATH), h
+%22image/*%22), H
 ttpM
@@ -2057,16 +2057,23 @@
 tpMethod
+.DELETE
 );%0A    %7D
@@ -2165,9 +2165,8 @@
     %7D%0A%0A%7D
-%0A

revert possible bug