Search in sources :

Example 1 with WebUI

use of org.collectionspace.chain.csp.webui.main.WebUI in project application by collectionspace.

the class TenantServlet method register_csps.

/* Not in the constructor because errors during construction of servlets tend to get lost in a mess of startup.
	 * Better present it on first request.
	 */
protected void register_csps(String tenantId) throws IOException, DocumentException {
    if (!tenantCSPM.containsKey(tenantId)) {
        tenantCSPM.put(tenantId, new CSPManagerImpl());
    }
    tenantCSPM.get(tenantId).register(new CoreConfig());
    tenantCSPM.get(tenantId).register(new FileStorage());
    tenantCSPM.get(tenantId).register(new ServicesStorageGenerator());
    tenantCSPM.get(tenantId).register(new WebUI());
    tenantCSPM.get(tenantId).register(new Spec());
}
Also used : ServicesStorageGenerator(org.collectionspace.chain.csp.persistence.services.ServicesStorageGenerator) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) CoreConfig(org.collectionspace.chain.csp.inner.CoreConfig) FileStorage(org.collectionspace.chain.csp.persistence.file.FileStorage) CSPManagerImpl(org.collectionspace.csp.container.impl.CSPManagerImpl) Spec(org.collectionspace.chain.csp.schema.Spec)

Example 2 with WebUI

use of org.collectionspace.chain.csp.webui.main.WebUI 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 3 with WebUI

use of org.collectionspace.chain.csp.webui.main.WebUI 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 4 with WebUI

use of org.collectionspace.chain.csp.webui.main.WebUI in project application by collectionspace.

the class TenantServlet method serviceWTenant.

/**
 * If you know the tenant id then
 * Check for global issues and if not run set up and then test if this is a init request, a composite request or a single request and
 * do the right thing
 * @param tenantid
 * @param pathparts
 * @param initcheck
 * @param servlet_request
 * @param servlet_response
 * @throws ServletException
 * @throws IOException
 * @throws BadRequestException
 * @throws UnauthorizedException
 */
protected void serviceWTenant(String tenantid, List<String> pathparts, String initcheck, HttpServletRequest servlet_request, HttpServletResponse servlet_response) throws ServletException, IOException, BadRequestException, UnauthorizedException {
    if (locked_down != null) {
        // this ended up with a status 200 hmmmm not great so changed it to return a 400... hopefully that wont break anythign else
        servlet_response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Servlet is locked down in a hard fail because of fatal error: " + locked_down);
        // servlet_response.getWriter().append("Servlet is locked down in a hard fail because of fatal error: "+locked_down);
        return;
    }
    if (initcheck.equals("init")) {
        tenantCSPM.put(tenantid, new CSPManagerImpl());
        tenantInit.put(tenantid, false);
        setup(tenantid);
        ConfigFinder cfg = new ConfigFinder(getServletContext());
        try {
            InputSource cfg_stream = cfg.resolveEntity("-//CSPACE//ROOT", "cspace-config-" + tenantid + ".xml");
            String test = IOUtils.toString(cfg_stream.getByteStream());
            // servlet_response.sendError(HttpServletResponse.SC_BAD_REQUEST, "cspace-config re-loaded"+test);
            servlet_response.getWriter().append("cspace-config re-loaded" + test);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            servlet_response.sendError(HttpServletResponse.SC_BAD_REQUEST, "cspace-config re-loadedfailed");
        }
        return;
    }
    if (!tenantInit.containsKey(tenantid) || !tenantInit.get(tenantid)) {
        setup(tenantid);
    }
    if (locked_down != null) {
        // this ended up with a status 200 hmmmm not great so changed it to return a 400... hopefully that wont break anythign else
        servlet_response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Servlet is locked down in a hard fail because of fatal error: " + locked_down);
        // servlet_response.getWriter().append("Servlet is locked down in a hard fail because of fatal error: "+locked_down);
        return;
    }
    if (perhapsServeFixedContent(servlet_request, servlet_response)) {
        return;
    }
    // Setup our request object
    UI web = tenantCSPM.get(tenantid).getUI("web");
    if (!tenantUmbrella.containsKey(tenantid)) {
        synchronized (getClass()) {
            if (!tenantUmbrella.containsKey(tenantid)) {
                tenantUmbrella.put(tenantid, new WebUIUmbrella((WebUI) web));
            }
        }
    }
    try {
        ConfigRoot root = tenantCSPM.get(tenantid).getConfigRoot();
        Spec spec = (Spec) root.getRoot(Spec.SPEC_ROOT);
        WebUIRequest req = new WebUIRequest(tenantUmbrella.get(tenantid), servlet_request, servlet_response, spec.getAdminData().getCookieLife(), pathparts);
        if (is_composite(req)) {
            serve_composite(web, req);
        } else {
            web.serviceRequest(req);
            req.solidify(true);
        }
    } catch (UIException e) {
        throw new BadRequestException("UIException", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ConfigRoot(org.collectionspace.chain.csp.config.ConfigRoot) CSPManagerImpl(org.collectionspace.csp.container.impl.CSPManagerImpl) ConfigFinder(org.collectionspace.csp.helper.core.ConfigFinder) SAXException(org.xml.sax.SAXException) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) UI(org.collectionspace.csp.api.ui.UI) UIException(org.collectionspace.csp.api.ui.UIException) Spec(org.collectionspace.chain.csp.schema.Spec)

Example 5 with WebUI

use of org.collectionspace.chain.csp.webui.main.WebUI in project application by collectionspace.

the class NullResolver method serviceUIWTenant.

/**
 * UI specific logic to work out the file needed from the server based on the url
 * fromthe cspace-ui servlet context
 * @param tenant
 * @param servlet_request
 * @param servlet_response
 * @throws BadRequestException
 * @throws ConnectionException
 * @throws DocumentException
 * @throws IOException
 */
protected void serviceUIWTenant(String tenant, HttpServletRequest servlet_request, HttpServletResponse servlet_response) throws BadRequestException, ConnectionException, DocumentException, IOException {
    String pathinfo = servlet_request.getPathInfo();
    String[] pathbits = pathinfo.substring(1).split("/");
    String urlredirect = "/collectionspace/ui/" + tenant + "/html/index.html";
    // clean up bad links - hopefully these links are never found any more...
    if (pathinfo.equals("/html") || pathinfo.equals("/html/")) {
        servlet_response.sendRedirect(urlredirect);
    } else if (pathinfo.equals("/" + tenant) || pathinfo.equals("/" + tenant + "/html") || pathinfo.equals("/" + tenant + "/")) {
        servlet_response.sendRedirect(urlredirect);
    } else if (pathinfo.equals("/" + tenant + "/index.html") || pathinfo.equals("/" + tenant + "/html/")) {
        servlet_response.sendRedirect(urlredirect);
    } else if (pathinfo.equals("/" + tenant + "/html") || pathinfo.equals("/" + tenant + "/html/")) {
        servlet_response.sendRedirect(urlredirect);
    }
    ServletContext sc = null;
    sc = getServletContext().getContext("/cspace-ui");
    if (sc == null) {
        servlet_response.sendError(HttpServletResponse.SC_BAD_REQUEST, "missing servlet context cspace-ui");
    }
    // work out what to do with the item based on it's type
    if (pathbits[0].equals("css") || pathbits[0].equals("js") || pathbits[0].equals("lib") || pathbits[0].equals("images")) {
        String tenantposs = getTenantByCookie(servlet_request);
        if (serverFixedExternalContent(servlet_request, servlet_response, sc, pathinfo, tenantposs)) {
            return;
        }
    }
    if (pathbits[0].equals("bundle")) {
        String tenantposs = getTenantByCookie(servlet_request);
        if (serverCreateMergedExternalContent(servlet_request, servlet_response, sc, pathinfo, tenantposs)) {
            return;
        }
    }
    if (pathbits[0].equals("config") || pathbits[1].equals("config")) {
    // tenant = getTenantByCookie(servlet_request);
    }
    if (!tenantInit.containsKey(tenant) || !tenantInit.get(tenant)) {
        setup(tenant);
    }
    /**
     * Support composite requests in ui calls as well as app direct calls
     */
    if (is_composite(pathinfo)) {
        List<String> p = new ArrayList<String>();
        for (String part : servlet_request.getPathInfo().split("/")) {
            if ("".equals(part))
                continue;
            p.add(part);
        }
        p.remove(0);
        ConfigRoot root = tenantCSPM.get(tenant).getConfigRoot();
        Spec spec = (Spec) root.getRoot(Spec.SPEC_ROOT);
        WebUIRequest req;
        UI web = tenantCSPM.get(tenant).getUI("web");
        if (!tenantUmbrella.containsKey(tenant)) {
            synchronized (getClass()) {
                if (!tenantUmbrella.containsKey(tenant)) {
                    tenantUmbrella.put(tenant, new WebUIUmbrella((WebUI) web));
                }
            }
        }
        try {
            req = new WebUIRequest(tenantUmbrella.get(tenant), servlet_request, servlet_response, spec.getAdminData().getCookieLife(), p);
            serveComposite(tenant, req, sc);
        } catch (UIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        serveSingle(tenant, servlet_request, servlet_response, pathinfo, pathbits, sc);
    }
}
Also used : WebUI(org.collectionspace.chain.csp.webui.main.WebUI) ConfigRoot(org.collectionspace.chain.csp.config.ConfigRoot) WebUI(org.collectionspace.chain.csp.webui.main.WebUI) UI(org.collectionspace.csp.api.ui.UI) ArrayList(java.util.ArrayList) ServletContext(javax.servlet.ServletContext) UIException(org.collectionspace.csp.api.ui.UIException) Spec(org.collectionspace.chain.csp.schema.Spec)

Aggregations

Spec (org.collectionspace.chain.csp.schema.Spec)5 WebUI (org.collectionspace.chain.csp.webui.main.WebUI)5 ConfigRoot (org.collectionspace.chain.csp.config.ConfigRoot)4 UI (org.collectionspace.csp.api.ui.UI)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 StringWriter (java.io.StringWriter)2 TeeInputStream (org.apache.commons.io.input.TeeInputStream)2 UIMapping (org.collectionspace.chain.csp.webui.external.UIMapping)2 UIException (org.collectionspace.csp.api.ui.UIException)2 CSPManagerImpl (org.collectionspace.csp.container.impl.CSPManagerImpl)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 ServletContext (javax.servlet.ServletContext)1 CoreConfig (org.collectionspace.chain.csp.inner.CoreConfig)1 FileStorage (org.collectionspace.chain.csp.persistence.file.FileStorage)1 ServicesStorageGenerator (org.collectionspace.chain.csp.persistence.services.ServicesStorageGenerator)1 ConfigFinder (org.collectionspace.csp.helper.core.ConfigFinder)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1