Search in sources :

Example 56 with JSONObject

use of org.json.simple.JSONObject in project jaggery by wso2.

the class JaggeryDeployerManager method addSecurityConstraints.

private static void addSecurityConstraints(Context context, JSONObject obj) {
    JSONArray arr = (JSONArray) obj.get(JaggeryCoreConstants.JaggeryConfigParams.SECURITY_CONSTRAINTS);
    if (arr != null) {
        for (Object anArr : arr) {
            JSONObject o = (JSONObject) anArr;
            SecurityConstraint securityConstraint = new SecurityConstraint();
            if (((JSONObject) o.get(JaggeryCoreConstants.JaggeryConfigParams.SECURITY_CONSTRAINT)).get(JaggeryCoreConstants.JaggeryConfigParams.WEB_RESOURCE_COLLECTION) != null) {
                JSONObject resCollection = (JSONObject) ((JSONObject) o.get(JaggeryCoreConstants.JaggeryConfigParams.SECURITY_CONSTRAINT)).get(JaggeryCoreConstants.JaggeryConfigParams.WEB_RESOURCE_COLLECTION);
                SecurityCollection secCollection = new SecurityCollection();
                secCollection.setName((String) resCollection.get(JaggeryCoreConstants.JaggeryConfigParams.WEB_RES_NAME));
                JSONArray arrPattern = (JSONArray) resCollection.get(JaggeryCoreConstants.JaggeryConfigParams.URL_PATTERNS);
                for (Object anArrPattern : arrPattern) {
                    secCollection.addPattern((String) anArrPattern);
                }
                JSONArray methods = (JSONArray) resCollection.get(JaggeryCoreConstants.JaggeryConfigParams.HTTP_METHODS);
                if (methods != null) {
                    for (Object method : methods) {
                        secCollection.addMethod((String) method);
                    }
                }
                securityConstraint.addCollection(secCollection);
            }
            if (((JSONObject) o.get(JaggeryCoreConstants.JaggeryConfigParams.SECURITY_CONSTRAINT)).get(JaggeryCoreConstants.JaggeryConfigParams.AUTH_ROLES) != null) {
                JSONArray roles = (JSONArray) ((JSONObject) o.get(JaggeryCoreConstants.JaggeryConfigParams.SECURITY_CONSTRAINT)).get(JaggeryCoreConstants.JaggeryConfigParams.AUTH_ROLES);
                for (Object role : roles) {
                    securityConstraint.addAuthRole((String) role);
                }
                securityConstraint.setAuthConstraint(true);
            }
            if (((JSONObject) o.get(JaggeryCoreConstants.JaggeryConfigParams.SECURITY_CONSTRAINT)).get(JaggeryCoreConstants.JaggeryConfigParams.USER_DATA_CONSTRAINT) != null) {
                JSONObject userDataConstraint = (JSONObject) ((JSONObject) o.get(JaggeryCoreConstants.JaggeryConfigParams.SECURITY_CONSTRAINT)).get(JaggeryCoreConstants.JaggeryConfigParams.USER_DATA_CONSTRAINT);
                String transportGuarantee = (String) userDataConstraint.get(JaggeryCoreConstants.JaggeryConfigParams.TRANSPORT_GUARANTEE);
                securityConstraint.setUserConstraint(transportGuarantee);
            }
            context.addConstraint(securityConstraint);
        }
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject)

Example 57 with JSONObject

use of org.json.simple.JSONObject in project jaggery by wso2.

the class JaggeryDeployerManager method readJaggeryConfig.

private static JSONObject readJaggeryConfig(Context context, Path appBase) {
    String content = null;
    String path = null;
    if (context.getDocBase().contains(WAR_EXTENSION)) {
        try {
            if (!appBase.endsWith("/")) {
                path = appBase + File.separator + context.getDocBase();
            } else {
                path = appBase + context.getDocBase();
            }
            ZipFile zip = new ZipFile(path);
            for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                if (entry.getName().toLowerCase().contains(JAGGERY_CONF)) {
                    InputStream inputStream = zip.getInputStream(entry);
                    content = IOUtils.toString(inputStream);
                }
            }
        } catch (IOException e) {
            log.error("Error occuered when the accessing the jaggery.conf file of " + context.getPath().substring(1), e);
        }
    } else {
        File file = new File(appBase + context.getPath() + File.separator + JAGGERY_CONF);
        try {
            content = FileUtils.readFileToString(file);
        } catch (IOException e) {
            log.error("IOException is thrown when accessing the jaggery.conf file of " + context.getPath().substring(1), e);
        }
    }
    JSONObject jaggeryConfig = null;
    try {
        JSONParser jp = new JSONParser();
        jaggeryConfig = (JSONObject) jp.parse(content);
    } catch (ParseException e) {
        log.error("Error in parsing the jaggery.conf file", e);
    }
    return jaggeryConfig;
}
Also used : ZipFile(java.util.zip.ZipFile) JSONObject(org.json.simple.JSONObject) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 58 with JSONObject

use of org.json.simple.JSONObject in project jaggery by wso2.

the class JaggeryDeployerManager method addFilters.

private static void addFilters(Context ctx, JSONObject jaggeryConfig) {
    if (jaggeryConfig != null) {
        JSONArray arrFilters = (JSONArray) jaggeryConfig.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS);
        JSONArray arrFilterMappings = (JSONArray) jaggeryConfig.get(JaggeryCoreConstants.JaggeryConfigParams.FILTER_MAPPINGS);
        if (arrFilters != null) {
            for (Object filterObj : arrFilters) {
                JSONObject filter = (JSONObject) filterObj;
                String name = (String) filter.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_NAME);
                String clazz = (String) filter.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_CLASS);
                FilterDef filterDef = new FilterDef();
                filterDef.setFilterName(name);
                filterDef.setFilterClass(clazz);
                JSONArray arrParams = (JSONArray) filter.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_PARAMS);
                if (arrParams != null) {
                    for (Object paramObj : arrParams) {
                        JSONObject param = (JSONObject) paramObj;
                        String paramName = (String) param.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_PARAMS_NAME);
                        String paramValue = (String) param.get(JaggeryCoreConstants.JaggeryConfigParams.FILTERS_PARAMS_VALUE);
                        filterDef.addInitParameter(paramName, paramValue);
                    }
                }
                ctx.addFilterDef(filterDef);
            }
        }
        if (arrFilterMappings != null) {
            for (Object filterMappingObj : arrFilterMappings) {
                JSONObject mapping = (JSONObject) filterMappingObj;
                String name = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.FILTER_MAPPINGS_NAME);
                String url = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.FILTER_MAPPINGS_URL);
                FilterMap filterMapping = new FilterMap();
                filterMapping.setFilterName(name);
                filterMapping.addURLPattern(url);
                ctx.addFilterMap(filterMapping);
            }
        }
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject)

Example 59 with JSONObject

use of org.json.simple.JSONObject in project jaggery by wso2.

the class JaggeryDeployerManager method setLoginConfig.

private static void setLoginConfig(Context context, JSONObject obj) {
    JSONObject loginObj = (JSONObject) obj.get(JaggeryCoreConstants.JaggeryConfigParams.LOGIN_CONFIG);
    if (loginObj != null) {
        if (loginObj.get(JaggeryCoreConstants.JaggeryConfigParams.AUTH_METHOD).equals(JaggeryCoreConstants.JaggeryConfigParams.AUTH_METHOD_FORM)) {
            LoginConfig loginConfig = new LoginConfig();
            loginConfig.setAuthMethod(JaggeryCoreConstants.JaggeryConfigParams.AUTH_METHOD_FORM);
            loginConfig.setLoginPage((String) ((JSONObject) loginObj.get(JaggeryCoreConstants.JaggeryConfigParams.FORM_LOGIN_CONFIG)).get(JaggeryCoreConstants.JaggeryConfigParams.FORM_LOGIN_PAGE));
            loginConfig.setErrorPage((String) ((JSONObject) loginObj.get(JaggeryCoreConstants.JaggeryConfigParams.FORM_LOGIN_CONFIG)).get(JaggeryCoreConstants.JaggeryConfigParams.FORM_ERROR_PAGE));
            context.setLoginConfig(loginConfig);
        } else if (loginObj.get(JaggeryCoreConstants.JaggeryConfigParams.AUTH_METHOD).equals(JaggeryCoreConstants.JaggeryConfigParams.AUTH_METHOD_BASIC)) {
            LoginConfig loginConfig = new LoginConfig();
            loginConfig.setAuthMethod(JaggeryCoreConstants.JaggeryConfigParams.AUTH_METHOD_BASIC);
            context.setLoginConfig(loginConfig);
        }
    }
}
Also used : JSONObject(org.json.simple.JSONObject)

Example 60 with JSONObject

use of org.json.simple.JSONObject in project jaggery by wso2.

the class JaggeryDeployerManager method addUrlMappings.

private static void addUrlMappings(Context context, JSONObject obj) {
    Object test = context.getServletContext().getAttribute("org.jaggeryjs.serveFunction");
    JSONArray arr = null;
    if (test != null) {
        // URL mapping for progamticaly
        arr = new JSONArray();
        JSONObject urlJSONObj = new JSONObject();
        JSONObject pathJSONObj = new JSONObject();
        urlJSONObj.put("url", "/*");
        pathJSONObj.put("path", File.separator + INDEX_JAG);
        arr.add(urlJSONObj);
    } else {
        arr = (JSONArray) obj.get(JaggeryCoreConstants.JaggeryConfigParams.URL_MAPPINGS);
    }
    if (arr != null) {
        Map<String, Object> urlMappings = new HashMap<String, Object>();
        for (Object mapObj : arr) {
            JSONObject mapping = (JSONObject) mapObj;
            String url = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.URL_MAPPINGS_URL);
            String path = (String) mapping.get(JaggeryCoreConstants.JaggeryConfigParams.URL_MAPPINGS_PATH);
            if (url != null && path != null) {
                path = path.startsWith("/") ? path : "/" + path;
                FilterMap filterMap = new FilterMap();
                filterMap.setFilterName(JaggeryCoreConstants.JAGGERY_FILTER_NAME);
                filterMap.addURLPattern(url);
                context.addFilterMap(filterMap);
                if (url.equals("/")) {
                    urlMappings.put("/", path);
                    continue;
                }
                url = url.startsWith("/") ? url.substring(1) : url;
                List<String> parts = new ArrayList<String>(Arrays.asList(url.split("/", -1)));
                addMappings(urlMappings, parts, path);
            } else {
                log.error("Invalid url mapping in jaggery.conf url : " + url + ", path : " + path);
            }
        }
        context.getServletContext().setAttribute(CommonManager.JAGGERY_URLS_MAP, urlMappings);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject)

Aggregations

JSONObject (org.json.simple.JSONObject)961 Test (org.junit.Test)312 JSONArray (org.json.simple.JSONArray)232 JSONParser (org.json.simple.parser.JSONParser)164 Map (java.util.Map)97 HashMap (java.util.HashMap)91 IOException (java.io.IOException)84 ArrayList (java.util.ArrayList)75 ParseException (org.json.simple.parser.ParseException)60 HttpClient (org.apache.commons.httpclient.HttpClient)39 File (java.io.File)38 List (java.util.List)38 BlockchainTest (org.xel.BlockchainTest)34 GetMethod (org.apache.commons.httpclient.methods.GetMethod)30 HttpURLConnection (java.net.HttpURLConnection)27 Tuple (org.apache.storm.tuple.Tuple)23 Transaction (org.xel.Transaction)23 APICall (org.xel.http.APICall)23 InputStreamReader (java.io.InputStreamReader)22 WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)22