package com.thinkbiganalytics.feedmgr.service;

/*-
 * #%L
 * thinkbig-feed-manager-controller
 * %%
 * Copyright (C) 2017 ThinkBig Analytics
 * %%
 * 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.
 * #L%
 */

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.config.server.encryption.EncryptionController;
import org.springframework.cloud.config.server.encryption.TextEncryptorLocator;
import org.springframework.http.MediaType;

public class EncryptionService {

    @Autowired
    private TextEncryptorLocator encryptor;

    private String encryptedPrefix = "{cipher}";

    public boolean isEncrypted(String str) {
        return StringUtils.startsWith(str, encryptedPrefix);
    }

    public String encrypt(String str) {
        String encrypted;
        if (str != null && !isEncrypted(str)) {
            encrypted = encrypt(str, MediaType.TEXT_PLAIN);
            if (!StringUtils.startsWith(encrypted, encryptedPrefix)) {
                encrypted = encryptedPrefix + encrypted;
            }
        } else {
            encrypted = str;
        }
        return encrypted;
    }

    private String encrypt(String unencrypted, MediaType mediaType) {
        EncryptionController ec = new EncryptionController(encryptor);
        return ec.encrypt(unencrypted, mediaType);
    }

    public String decrypt(String str) {
        String decrypted = null;
        if (str != null) {
            if (!StringUtils.startsWith(str, encryptedPrefix)) {
                str = encryptedPrefix + str;
            }
            decrypted = decrypt(str, MediaType.TEXT_PLAIN);
        }
        return decrypted;
    }

    private String decrypt(String encrypted, MediaType mediaType) {
        EncryptionController ec = new EncryptionController(encryptor);
        return ec.decrypt(encrypted, mediaType);
    }
}

@@ -708,16 +708,17 @@
 L%25%0A */%0A%0A
+%0A
 import o
@@ -784,178 +784,31 @@
 ork.
-beans.factory.annotation.Autowired;%0Aimport org.springframework.cloud.config.server.encryption.EncryptionController;%0Aimport org.springframework.cloud.config.server
+security.crypto
 .encrypt
 ion.
@@ -803,19 +803,16 @@
 .encrypt
-ion
 .TextEnc
@@ -821,59 +821,39 @@
 ptor
-Locator;%0Aimport org.springframework.http.MediaType;
+;%0A%0Aimport javax.inject.Inject;%0A
 %0A%0Apu
@@ -893,29 +893,18 @@
    @
-Autowired%0A    private
+Inject%0A   
  Tex
@@ -917,15 +917,8 @@
 ptor
-Locator
  enc
@@ -976,16 +976,17 @@
 her%7D%22;%0A%0A
+%0A
     publ
@@ -1146,32 +1146,39 @@
 String encrypted
+ = null
 ;%0A        if (st
@@ -1175,27 +1175,43 @@
     if (
-str != null
+StringUtils.isNotBlank(str)
  && !isE
@@ -1262,34 +1262,22 @@
 rypt
-(str, MediaType.TEXT_PLAIN
+or.encrypt(str
 );%0A 
@@ -1510,207 +1510,8 @@
  %7D%0A%0A
-    private String encrypt(String unencrypted, MediaType mediaType) %7B%0A        EncryptionController ec = new EncryptionController(encryptor);%0A        return ec.encrypt(unencrypted, mediaType);%0A    %7D%0A%0A
     
@@ -1558,56 +1558,39 @@
     
+if (
 String
- decrypted = null;%0A        if (str != null
+Utils.isNotBlank(str)
 ) %7B%0A
@@ -1597,33 +1597,32 @@
             if (
-!
 StringUtils.star
@@ -1674,16 +1674,45 @@
    str =
+ StringUtils.removeStart(str,
  encrypt
@@ -1723,14 +1723,9 @@
 efix
- + str
+)
 ;%0A  
@@ -1752,291 +1752,95 @@
     
-decrypted = decrypt(str, MediaType.TEXT_PLAIN);%0A        %7D%0A        return decrypted;%0A    %7D%0A%0A    private String decrypt(String encrypted, MediaType mediaType) %7B%0A        EncryptionController ec = new EncryptionController(encryptor);%0A        return ec.decrypt(encrypted, mediaType);
+return encryptor.decrypt(str);%0A        %7D else %7B%0A            return str;%0A        %7D
 %0A    %7D%0A
+%0A
 %7D%0A

remove need for EncryptionController and inject and use the TextEncryptor directly