Search in sources :

Example 61 with UIException

use of org.collectionspace.csp.api.ui.UIException 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 62 with UIException

use of org.collectionspace.csp.api.ui.UIException 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)

Example 63 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class WebUIRequest method initRequest.

private void initRequest(UIUmbrella umbrella, HttpServletRequest request, HttpServletResponse response, List<String> p) throws IOException, UIException {
    this.request = request;
    this.response = response;
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();
        // Parse the request
        FileItemIterator iter;
        try {
            iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                // InputStream stream = item.openStream();
                if (item.isFormField()) {
                // System.out.println("Form field " + name + " with value "
                // + Streams.asString(stream) + " detected.");
                } else {
                    // System.out.println("File field " + name + " with file name "
                    // + item.getName() + " detected.");
                    // Process the input stream
                    contentHeaders = item.getHeaders();
                    uploadName = item.getName();
                    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                    if (item != null) {
                        InputStream stream = item.openStream();
                        IOUtils.copy(stream, byteOut);
                        new TeeInputStream(stream, byteOut);
                    }
                    bytebody = byteOut.toByteArray();
                }
            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        body = IOUtils.toString(request.getInputStream(), "UTF-8");
    }
    this.ppath = p.toArray(new String[0]);
    if (!(umbrella instanceof WebUIUmbrella))
        throw new UIException("Bad umbrella");
    this.umbrella = (WebUIUmbrella) umbrella;
    session = calculateSessionId();
}
Also used : FileItemStream(org.apache.commons.fileupload.FileItemStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) UIException(org.collectionspace.csp.api.ui.UIException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 64 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class UserDetailsDelete method store_delete.

private void store_delete(Storage storage, UIRequest request, String path) throws UIException {
    try {
        // Deleting a user needs to clear the userperms cache for safety.
        ResponseCache.clearCache(ResponseCache.USER_PERMS_CACHE);
        storage.deleteJSON(base + "/" + path);
    } catch (ExistException e) {
        throw new UIException("JSON Not found " + e, e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 65 with UIException

use of org.collectionspace.csp.api.ui.UIException in project application by collectionspace.

the class UserDetailsReset method doEmail.

private Boolean doEmail(String csid, String emailparam, Request in, JSONObject userdetails) throws UIException, JSONException {
    String token = createToken(csid);
    EmailData ed = spec.getEmailData();
    String[] recipients = new String[1];
    /* ABSTRACT EMAIL STUFF : WHERE do we get the content of emails from? cspace-config.xml */
    String messagebase = ed.getPasswordResetMessage();
    String link = ed.getBaseURL() + ed.getLoginUrl() + "?token=" + token + "&email=" + emailparam;
    String message = messagebase.replaceAll("\\{\\{link\\}\\}", link);
    String greeting = userdetails.getJSONObject("fields").getString("screenName");
    message = message.replaceAll("\\{\\{greeting\\}\\}", greeting);
    message = message.replaceAll("\\\\n", "\\\n");
    message = message.replaceAll("\\\\r", "\\\r");
    String SMTP_HOST_NAME = ed.getSMTPHost();
    String SMTP_PORT = ed.getSMTPPort();
    String subject = ed.getPasswordResetSubject();
    String from = ed.getFromAddress();
    if (ed.getToAddress().isEmpty()) {
        recipients[0] = emailparam;
    } else {
        recipients[0] = ed.getToAddress();
    }
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    boolean debug = false;
    Properties props = new Properties();
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", ed.doSMTPAuth());
    props.put("mail.debug", ed.doSMTPDebug());
    props.put("mail.smtp.port", SMTP_PORT);
    Session session = Session.getDefaultInstance(props);
    // XXX fix to allow authpassword /username
    session.setDebug(debug);
    Message msg = new MimeMessage(session);
    InternetAddress addressFrom;
    try {
        addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setText(message);
        Transport.send(msg);
    } catch (AddressException e) {
        throw new UIException("AddressException: " + e.getMessage());
    } catch (MessagingException e) {
        throw new UIException("MessagingException: " + e.getMessage());
    }
    return true;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) EmailData(org.collectionspace.chain.csp.schema.EmailData) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) UIException(org.collectionspace.csp.api.ui.UIException) UISession(org.collectionspace.csp.api.ui.UISession) Session(javax.mail.Session)

Aggregations

UIException (org.collectionspace.csp.api.ui.UIException)72 JSONObject (org.json.JSONObject)51 JSONException (org.json.JSONException)50 ExistException (org.collectionspace.csp.api.persistence.ExistException)39 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)39 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)39 JSONArray (org.json.JSONArray)19 IOException (java.io.IOException)10 Record (org.collectionspace.chain.csp.schema.Record)7 Instance (org.collectionspace.chain.csp.schema.Instance)6 ConfigException (org.collectionspace.chain.csp.config.ConfigException)5 Field (org.collectionspace.chain.csp.schema.Field)4 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)4 UIRequest (org.collectionspace.csp.api.ui.UIRequest)4 MessageDigest (java.security.MessageDigest)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)3 AdminData (org.collectionspace.chain.csp.schema.AdminData)3 Structure (org.collectionspace.chain.csp.schema.Structure)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2