Search in sources :

Example 1 with UIMapping

use of org.collectionspace.chain.csp.webui.external.UIMapping in project application by collectionspace.

the class NullResolver method serveSingle.

/**
 * Return a single request from the cspace-ui servlet context
 * @param tenant
 * @param sub
 * @param sc
 * @throws UIException
 * @throws IOException
 */
private void serveSingle(String tenant, CompositeWebUIRequestPart sub, ServletContext sc) throws UIException, IOException {
    // Setup our request object
    UI web = tenantCSPM.get(tenant).getUI("web");
    WebUI webui = (WebUI) web;
    UIMapping[] allmappings = webui.getAllMappings();
    ConfigRoot root = tenantCSPM.get(tenant).getConfigRoot();
    Spec spec = (Spec) root.getRoot(Spec.SPEC_ROOT);
    // this doesn't seem to work yet
    Boolean doMetaConfig = true;
    String[] pathbits = sub.getPrincipalPath();
    String pathinfo = StringUtils.join(pathbits, "/");
    String path = pathinfo;
    Map<String, Object> validmap = doMapping(pathinfo, allmappings, spec);
    if (null != validmap) {
        path = (String) validmap.get("File");
        if (((UIMapping) validmap.get("map")).hasMetaConfig() && doMetaConfig) {
            InputStream is = getFixedContent(sc, path, tenant);
            StringWriter writer = new StringWriter();
            IOUtils.copy(is, writer, "UTF-8");
            String theString = writer.toString();
            /*
				SAXReader reader = new SAXReader();
				reader.setEntityResolver(new NullResolver());
				Document xmlfile = null;

				ByteArrayOutputStream dump = new ByteArrayOutputStream();
				xmlfile = reader.read(new TeeInputStream(is, dump));
			//	log.info(dump.toString("UTF-8"));
			//	log.info(xmlfile.asXML());<tr></tr<.>
			 
			 TODO I want this to work... it just doesn't yet as I haven\'t had time to work on it
			 this is all about replace items in the title etc of the universal files so they become more specific
*/
            for (String metafield : ((UIMapping) validmap.get("map")).getAllMetaConfigs()) {
                String rtext = ((UIMapping) validmap.get("map")).getMetaConfig(metafield).parseValue((Record) validmap.get("record"), (Instance) validmap.get("instance"));
                // try as json
                String regex = "\"" + metafield + "\": \"[^\"]*\"";
                String replacement = "\"" + metafield + "\": \"" + rtext + "\"";
                theString = theString.replaceFirst(regex, replacement);
                // try as xml
                String regex2 = "<" + metafield + ">[^<]*</" + metafield + ">";
                String replacement2 = "<" + metafield + ">" + rtext + "</" + metafield + ">";
                theString = theString.replaceFirst(regex2, replacement2);
            }
            InputStream is2 = new ByteArrayInputStream(theString.getBytes("UTF-8"));
            serveExternalContent(sub, sc, is2, path);
            is.close();
            return;
        }
    }
    if (pathbits[1].equals("bundle")) {
        if (serverCreateMergedExternalContent(sub, sc, pathinfo, tenant)) {
            return;
        }
    }
    if (serverFixedExternalContent(sub, sc)) {
        return;
    }
}
Also used : ConfigRoot(org.collectionspace.chain.csp.config.ConfigRoot) TeeInputStream(org.apache.commons.io.input.TeeInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UIMapping(org.collectionspace.chain.csp.webui.external.UIMapping) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) UI(org.collectionspace.csp.api.ui.UI) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONObject(org.json.JSONObject) Spec(org.collectionspace.chain.csp.schema.Spec)

Example 2 with UIMapping

use of org.collectionspace.chain.csp.webui.external.UIMapping in project application by collectionspace.

the class NullResolver method serveSingle.

/**
 * abstraction for returning a single item.
 * probably could be simplified with other serveSingle function
 * @param tenant
 * @param servlet_request
 * @param servlet_response
 * @param pathinfo
 * @param pathbits
 * @param sc
 * @throws IOException
 * @throws BadRequestException
 * @throws UnsupportedEncodingException
 */
private void serveSingle(String tenant, HttpServletRequest servlet_request, HttpServletResponse servlet_response, String pathinfo, String[] pathbits, ServletContext sc) throws IOException, BadRequestException, UnsupportedEncodingException {
    // should we redirect this url or just do the normal stuff
    // Setup our request object
    UI web = tenantCSPM.get(tenant).getUI("web");
    WebUI webui = (WebUI) web;
    UIMapping[] allmappings = webui.getAllMappings();
    ConfigRoot root = tenantCSPM.get(tenant).getConfigRoot();
    Spec spec = (Spec) root.getRoot(Spec.SPEC_ROOT);
    // this doesn't seem to work yet
    Boolean doMetaConfig = true;
    String path = pathinfo;
    Map<String, Object> validmap = doMapping(pathinfo, allmappings, spec);
    if (null != validmap) {
        path = (String) validmap.get("File");
        if (((UIMapping) validmap.get("map")).hasMetaConfig() && doMetaConfig) {
            InputStream is = getFixedContent(sc, path, tenant);
            StringWriter writer = new StringWriter();
            IOUtils.copy(is, writer, "UTF-8");
            String theString = writer.toString();
            /*
				SAXReader reader = new SAXReader();
				reader.setEntityResolver(new NullResolver());
				Document xmlfile = null;

				ByteArrayOutputStream dump = new ByteArrayOutputStream();
				xmlfile = reader.read(new TeeInputStream(is, dump));
			//	log.info(dump.toString("UTF-8"));
			//	log.info(xmlfile.asXML());<tr></tr<.>
			 TODO I want this to work... it just doesn't yet as I haven\'t had time to work on it
			 this is all about replace items in the title etc of the universal files so they become more specific
*/
            for (String metafield : ((UIMapping) validmap.get("map")).getAllMetaConfigs()) {
                String rtext = ((UIMapping) validmap.get("map")).getMetaConfig(metafield).parseValue((Record) validmap.get("record"), (Instance) validmap.get("instance"));
                // try as json
                String regex = "\"" + metafield + "\": \"[^\"]*\"";
                String replacement = "\"" + metafield + "\": \"" + rtext + "\"";
                theString = theString.replaceFirst(regex, replacement);
                // try as xml
                String regex2 = "<" + metafield + ">[^<]*</" + metafield + ">";
                String replacement2 = "<" + metafield + ">" + rtext + "</" + metafield + ">";
                theString = theString.replaceFirst(regex2, replacement2);
            }
            InputStream is2 = new ByteArrayInputStream(theString.getBytes("UTF-8"));
            serveContent(servlet_response, is2);
            is.close();
            return;
        }
    }
    if (pathbits[1].equals("bundle")) {
        if (serverCreateMergedExternalContent(servlet_request, servlet_response, sc, pathinfo, tenant)) {
            return;
        }
    }
    if (serverFixedExternalContent(servlet_request, servlet_response, sc, path, tenant)) {
        return;
    }
}
Also used : ConfigRoot(org.collectionspace.chain.csp.config.ConfigRoot) TeeInputStream(org.apache.commons.io.input.TeeInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UIMapping(org.collectionspace.chain.csp.webui.external.UIMapping) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) UI(org.collectionspace.csp.api.ui.UI) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONObject(org.json.JSONObject) Spec(org.collectionspace.chain.csp.schema.Spec)

Example 3 with UIMapping

use of org.collectionspace.chain.csp.webui.external.UIMapping in project application by collectionspace.

the class NullResolver method doMapping.

/**
 * Some simple mappings to help with the unfinished functionality to
 * allow changing of content on templates based on returned item type
 * @param pathinfo
 * @param allmappings
 * @param spec
 * @return
 */
private Map<String, Object> doMapping(String pathinfo, UIMapping[] allmappings, Spec spec) {
    Map<String, Object> out = new HashMap<String, Object>();
    for (UIMapping map : allmappings) {
        out.put("map", map);
        out.put("isConfig", false);
        out.put("isRecord", false);
        out.put("instance", null);
        out.put("record", null);
        out.put("RecordFile", map.getFile());
        out.put("ConfigFile", "");
        out.put("File", "");
        if (map.hasUrl()) {
            if (map.getUrl().equals(pathinfo)) {
                return out;
            }
        } else if (map.hasType()) {
            for (Record r : spec.getAllRecords()) {
                if (r.isType(map.getType())) {
                    if (r.isType("authority")) {
                        for (Instance ins : r.getAllInstances()) {
                            // config
                            String tenantInstanceconfig = "/" + spec.getAdminData().getTenantName() + "/config/" + ins.getWebURL() + ".json";
                            String tenantAuthconfig = "/" + spec.getAdminData().getTenantName() + "/config/" + r.getWebURL() + ".json";
                            if (pathinfo.equals(tenantInstanceconfig)) {
                                out.put("instance", ins);
                                out.put("record", r);
                                out.put("isConfig", true);
                                out.put("ConfigFile", tenantAuthconfig);
                                out.put("File", tenantAuthconfig);
                                return out;
                            } else if (pathinfo.equals("/config/" + ins.getWebURL() + ".json")) {
                                out.put("instance", ins);
                                out.put("record", r);
                                out.put("isConfig", true);
                                out.put("ConfigFile", tenantAuthconfig);
                                out.put("File", tenantAuthconfig);
                                return out;
                            }
                            // record
                            if (pathinfo.equals("/" + spec.getAdminData().getTenantName() + "/" + ins.getUIURL())) {
                                out.put("record", r);
                                out.put("isRecord", true);
                                out.put("instance", ins);
                                out.put("File", map.getFile());
                                return out;
                            }
                            if (pathinfo.equals("/" + spec.getAdminData().getTenantName() + "/html/" + ins.getUIURL())) {
                                out.put("record", r);
                                out.put("isRecord", true);
                                out.put("instance", ins);
                                out.put("File", map.getFile());
                                return out;
                            }
                        }
                    }
                    String test = "/" + spec.getAdminData().getTenantName() + "/html/" + r.getUIURL();
                    if (pathinfo.equals(test)) {
                        out.put("record", r);
                        out.put("isRecord", true);
                        out.put("File", map.getFile());
                        return out;
                    }
                }
            }
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) Instance(org.collectionspace.chain.csp.schema.Instance) UIMapping(org.collectionspace.chain.csp.webui.external.UIMapping) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record)

Example 4 with UIMapping

use of org.collectionspace.chain.csp.webui.external.UIMapping in project application by collectionspace.

the class WebUI method configure.

@Override
public void configure(RuleSet rules) throws CSPDependencyException {
    /* MAIN/ui/web -> UI */
    rules.addRule(SECTIONED, new String[] { "ui", "web" }, SECTION_PREFIX + "web", null, new RuleTarget() {

        @Override
        public Object populate(Object parent, ReadOnlySection section) {
            ((CoreConfig) parent).setRoot(WEBUI_ROOT, WebUI.this);
            if (section.getValue("/tmp-schema-path") != null) {
                // XXX
                uispec_path = System.getProperty("java.io.tmpdir") + "/ju-cspace";
            // fix
            } else {
                uispec_path = (String) section.getValue("/schema-path");
            }
            login_dest = (String) section.getValue("/login-dest");
            login_failed_dest = (String) section.getValue("/login-failed-dest");
            front_page = (String) section.getValue("/front-page");
            find_page = (String) section.getValue("/find-page");
            return WebUI.this;
        }
    });
    /* MAIN/ui/web/mappings ->UI */
    rules.addRule(SECTION_PREFIX + "web", new String[] { "mappings", "map" }, SECTION_PREFIX + "uimapping", null, new RuleTarget() {

        @Override
        public Object populate(Object parent, ReadOnlySection section) {
            uiMapping = new UIMapping((WebUI) parent, section);
            addMapping(uiMapping);
            return uiMapping;
        }
    });
    rules.addRule(SECTION_PREFIX + "uimapping", new String[] { "configure", "meta" }, SECTION_PREFIX + "uimetamapping", null, new RuleTarget() {

        @Override
        public Object populate(Object parent, ReadOnlySection section) {
            UIMapping map = (UIMapping) parent;
            uiMeta = new UIMeta((UIMapping) parent, section);
            map.addMetaConfig(uiMeta);
            return uiMeta;
        }
    });
}
Also used : RuleTarget(org.collectionspace.chain.csp.config.RuleTarget) UIMapping(org.collectionspace.chain.csp.webui.external.UIMapping) ReadOnlySection(org.collectionspace.chain.csp.config.ReadOnlySection) UIMeta(org.collectionspace.chain.csp.webui.external.UIMeta)

Aggregations

UIMapping (org.collectionspace.chain.csp.webui.external.UIMapping)4 JSONObject (org.json.JSONObject)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 TeeInputStream (org.apache.commons.io.input.TeeInputStream)2 ConfigRoot (org.collectionspace.chain.csp.config.ConfigRoot)2 Spec (org.collectionspace.chain.csp.schema.Spec)2 WebUI (org.collectionspace.chain.csp.webui.main.WebUI)2 UI (org.collectionspace.csp.api.ui.UI)2 HashMap (java.util.HashMap)1 ReadOnlySection (org.collectionspace.chain.csp.config.ReadOnlySection)1 RuleTarget (org.collectionspace.chain.csp.config.RuleTarget)1 Instance (org.collectionspace.chain.csp.schema.Instance)1 Record (org.collectionspace.chain.csp.schema.Record)1 UIMeta (org.collectionspace.chain.csp.webui.external.UIMeta)1