/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mifosplatform.template.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.mifosplatform.template.domain.Template;
import org.mifosplatform.template.domain.TemplateFunctions;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
@Service
public class TemplateMergeService {
// private final FromJsonHelper fromApiJsonHelper;
private Map<String, Object> scopes;
// @Autowired
// public TemplateMergeService(final FromJsonHelper fromApiJsonHelper) {
// this.fromApiJsonHelper = fromApiJsonHelper;
// }
public String compile(final Template template, final Map<String, Object> scopes) throws MalformedURLException, IOException {
this.scopes = scopes;
this.scopes.put("static", new TemplateFunctions());
final MustacheFactory mf = new DefaultMustacheFactory();
final Mustache mustache = mf.compile(new StringReader(template.getText()), template.getName());
final Map<String, Object> mappers = getCompiledMapFromMappers(template.getMappersAsMap());
this.scopes.putAll(mappers);
final StringWriter stringWriter = new StringWriter();
mustache.execute(stringWriter, this.scopes);
return stringWriter.toString();
}
private Map<String, Object> getCompiledMapFromMappers(final Map<String, String> data) {
final MustacheFactory mf = new DefaultMustacheFactory();
if (data != null) {
for (final Map.Entry<String, String> entry : data.entrySet()) {
final Mustache mappersMustache = mf.compile(new StringReader(entry.getValue()), "");
final StringWriter stringWriter = new StringWriter();
mappersMustache.execute(stringWriter, this.scopes);
String url = stringWriter.toString();
if (!url.startsWith("http")) {
url = this.scopes.get("BASE_URI") + url;
}
try {
this.scopes.put(entry.getKey(), getMapFromUrl(url));
} catch (final MalformedURLException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
return this.scopes;
}
@SuppressWarnings("unchecked")
private Map<String, Object> getMapFromUrl(final String url) throws MalformedURLException, IOException {
final HttpURLConnection connection = getConnection(url);
final String response = getStringFromInputStream(connection.getInputStream());
HashMap<String, Object> result = new HashMap<>();
if (connection.getContentType().equals("text/plain")) {
result.put("src", response);
} else {
result = new ObjectMapper().readValue(response, HashMap.class);
}
return result;
}
private HttpURLConnection getConnection(final String url) {
final String name = SecurityContextHolder.getContext().getAuthentication().getName();
final String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(name, password.toCharArray());
}
});
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
TrustModifier.relaxHostChecking(connection);
connection.setDoInput(true);
} catch (final MalformedURLException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} catch (final KeyManagementException e) {
e.printStackTrace();
} catch (final NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (final KeyStoreException e) {
e.printStackTrace();
}
return connection;
}
private static String getStringFromInputStream(final InputStream is) {
BufferedReader br = null;
final StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (final IOException e) {
// FIXME - HANDLE THIS CASE
} finally {
if (br != null) {
try {
br.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}