// Path: jwe/callout/src/main/java/com/apigee/utils/TemplateString.java
// public class TemplateString {
//     private final Pattern tmpltPattern =
//         Pattern.compile("\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE);
//     public ArrayList<String> variableNames;
//     public String template;
//     private void injectDollar(int position) {
//         template =
//             template.substring(0, position) +
//             "$" + template.substring(position, template.length());
//     }
//     public TemplateString(String s) {
//         this.variableNames = new ArrayList<String>();
//         examineString(s);
//     }
// 
//     private void examineString(String input) {
//         this.template = input;
//         int x = 0;
//         Matcher m = tmpltPattern.matcher(input);
//         while (m.find()) {
//             variableNames.add(m.group(1));
//             injectDollar(m.start() + x++);
//         }
//     }
// }


import com.apigee.flow.execution.ExecutionContext;
import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.IOIntensive;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.IOException;
import java.io.InputStream;
import org.jose4j.jwk.JsonWebKey;
import org.jose4j.jwe.JsonWebEncryption;
import org.jose4j.jwe.KeyManagementAlgorithmIdentifiers;
import org.jose4j.jwe.ContentEncryptionAlgorithmIdentifiers;
import org.apache.commons.ssl.PKCS8Key;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.text.StrSubstitutor;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.KeyFactory;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import com.apigee.utils.TemplateString;
        this.properties = m;
    }

    private static InputStream getResourceAsStream(String resourceName)
      throws IOException {
        // forcibly prepend a slash
        if (!resourceName.startsWith("/")) {
            resourceName = "/" + resourceName;
        }
        if (!resourceName.startsWith("/resources")) {
            resourceName = "/resources" + resourceName;
        }
        InputStream in = JweEncryptorCallout.class.getResourceAsStream(resourceName);

        if (in == null) {
            throw new IOException("resource \"" + resourceName + "\" not found");
        }

        return in;
    }

    private String getPlainText(MessageContext msgCtxt) throws Exception {
        String plaintext = (String) this.properties.get("plaintext");
        if (plaintext == null || plaintext.equals("")) {
            throw new IllegalStateException("plaintext is not specified or is empty.");
        }
        plaintext = resolvePropertyValue(plaintext, msgCtxt);
        if (plaintext == null || plaintext.equals("")) {
            throw new IllegalStateException("plaintext is null or empty.");
        }
// Path: jwe/callout/src/main/java/com/apigee/utils/TemplateString.java
// public class TemplateString {
//     private final Pattern tmpltPattern =
//         Pattern.compile("\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE);
//     public ArrayList<String> variableNames;
//     public String template;
//     private void injectDollar(int position) {
//         template =
//             template.substring(0, position) +
//             "$" + template.substring(position, template.length());
//     }
//     public TemplateString(String s) {
//         this.variableNames = new ArrayList<String>();
//         examineString(s);
//     }
// 
//     private void examineString(String input) {
//         this.template = input;
//         int x = 0;
//         Matcher m = tmpltPattern.matcher(input);
//         while (m.find()) {
//             variableNames.add(m.group(1));
//             injectDollar(m.start() + x++);
//         }
//     }
// }


// Path: jwe/callout/src/main/java/com/apigee/callout/jwe/JweEncryptorCallout.java
import com.apigee.flow.execution.ExecutionContext;
import com.apigee.flow.execution.ExecutionResult;
import com.apigee.flow.execution.IOIntensive;
import com.apigee.flow.execution.spi.Execution;
import com.apigee.flow.message.MessageContext;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.IOException;
import java.io.InputStream;
import org.jose4j.jwk.JsonWebKey;
import org.jose4j.jwe.JsonWebEncryption;
import org.jose4j.jwe.KeyManagementAlgorithmIdentifiers;
import org.jose4j.jwe.ContentEncryptionAlgorithmIdentifiers;
import org.apache.commons.ssl.PKCS8Key;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.text.StrSubstitutor;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.KeyFactory;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import com.apigee.utils.TemplateString;

        this.properties = m;
    }

    private static InputStream getResourceAsStream(String resourceName)
      throws IOException {
        // forcibly prepend a slash
        if (!resourceName.startsWith("/")) {
            resourceName = "/" + resourceName;
        }
        if (!resourceName.startsWith("/resources")) {
            resourceName = "/resources" + resourceName;
        }
        InputStream in = JweEncryptorCallout.class.getResourceAsStream(resourceName);

        if (in == null) {
            throw new IOException("resource \"" + resourceName + "\" not found");
        }

        return in;
    }

    private String getPlainText(MessageContext msgCtxt) throws Exception {
        String plaintext = (String) this.properties.get("plaintext");
        if (plaintext == null || plaintext.equals("")) {
            throw new IllegalStateException("plaintext is not specified or is empty.");
        }
        plaintext = resolvePropertyValue(plaintext, msgCtxt);
        if (plaintext == null || plaintext.equals("")) {
            throw new IllegalStateException("plaintext is null or empty.");
        }
        return plaintext;