/** * Checks if a subject is allowed to call method X on resource Y. * * @param subjectid subject id * @param resourceName resource name (type) * @param httpMethod HTTP method name * @return true if allowed */ public boolean isAllowedTo(String subjectid, String resourceName, String httpMethod) { boolean allow = false; if (subjectid != null && !StringUtils.isBlank(resourceName) && !StringUtils.isBlank(httpMethod)) { if (getResourcePermissions().isEmpty()) { // Default policy is "deny all". Returning true here would make it "allow all". return false; } if (getResourcePermissions().containsKey(subjectid) && getResourcePermissions().get(subjectid).containsKey(resourceName)) { // subject-specific permissions have precedence over wildcard permissions // i.e. only the permissions for that subjectid are checked, other permissions are ignored allow = isAllowed(subjectid, resourceName, httpMethod); } else { allow = isAllowed(subjectid, resourceName, httpMethod) || isAllowed(subjectid, ALLOW_ALL, httpMethod) || isAllowed(ALLOW_ALL, resourceName, httpMethod) || isAllowed(ALLOW_ALL, ALLOW_ALL, httpMethod); } } boolean isRootApp = StringUtils.equals(App.id(Config.APP_NAME_NS), getId()); boolean isRootAppAccessAllowed = Config.getConfigBoolean("clients_can_access_root_app", !Config.IN_PRODUCTION); return isRootApp ? (isRootAppAccessAllowed && allow) : allow; }
/** @return true if asynchronous caching is enabled. */ private boolean isAsyncEnabled() { return Config.getConfigBoolean("hc.async_enabled", false); }
/** * A converter that uses http://openexchangerates.org. * * @author Alex Bogdanovski [[email protected]] */ @Singleton public class OXRCurrencyConverter implements CurrencyConverter { private static final Logger logger = LoggerFactory.getLogger(OXRCurrencyConverter.class); private static final String FXRATES_KEY = "fxrates"; private static final long REFRESH_AFTER = 24 * 60 * 60 * 1000; // 24 hours in ms private static final String SERVICE_URL = "http://openexchangerates.org/api/latest.json?app_id=" .concat(Config.getConfigParam("openx_api_key", "")); private DAO dao; /** * Default constructor. * * @param dao dao */ @Inject public OXRCurrencyConverter(DAO dao) { this.dao = dao; } @Override public Double convertCurrency(Number amount, String from, String to) { if (amount == null || StringUtils.isBlank(from) || StringUtils.isBlank(to)) { return 0.0; } Sysprop s = dao.read(FXRATES_KEY); if (s == null) { s = fetchFxRatesJSON(); } else if ((Utils.timestamp() - s.getTimestamp()) > REFRESH_AFTER) { // lazy refresh fx rates Para.asyncExecute( new Runnable() { public void run() { fetchFxRatesJSON(); } }); } double ratio = 1.0; if (s.hasProperty(from) && s.hasProperty(to)) { Double f = NumberUtils.toDouble(s.getProperty(from).toString(), 1.0); Double t = NumberUtils.toDouble(s.getProperty(to).toString(), 1.0); ratio = t / f; } return amount.doubleValue() * ratio; } @SuppressWarnings("unchecked") private Sysprop fetchFxRatesJSON() { Map<String, Object> map = new HashMap<String, Object>(); Sysprop s = new Sysprop(); ObjectReader reader = ParaObjectUtils.getJsonReader(Map.class); try { CloseableHttpClient http = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(SERVICE_URL); HttpResponse res = http.execute(httpGet); HttpEntity entity = res.getEntity(); if (entity != null && Utils.isJsonType(entity.getContentType().getValue())) { JsonNode jsonNode = reader.readTree(entity.getContent()); if (jsonNode != null) { JsonNode rates = jsonNode.get("rates"); if (rates != null) { map = reader.treeToValue(rates, Map.class); s.setId(FXRATES_KEY); s.setProperties(map); // s.addProperty("fetched", Utils.formatDate("dd MM yyyy HH:mm", Locale.UK)); dao.create(s); } } EntityUtils.consume(entity); } logger.debug("Fetched rates from OpenExchange for {}.", new Date().toString()); } catch (Exception e) { logger.error("TimerTask failed: {}", e); } return s; } }