Search in sources :

Example 1 with UIException

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

the class CompositeWebUIRequestPart method sendUnknown.

@Override
public void sendUnknown(InputStream data, String contenttype, String contentDisposition) throws UIException {
    mime_type_out = contenttype;
    try {
        IOUtils.copy(data, body_out);
        body_out.flush();
    } catch (IOException e) {
        throw new UIException("Could not write data", e);
    }
}
Also used : UIException(org.collectionspace.csp.api.ui.UIException) IOException(java.io.IOException)

Example 2 with UIException

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

the class CompositeWebUIRequestPart method solidify.

public JSONObject solidify() throws JSONException, UIException {
    JSONObject out = new JSONObject();
    int status = 200;
    try {
        if (failure) {
            // Failed
            status = 400;
            if (exception != null)
                out.put("exception", ExceptionUtils.getFullStackTrace(exception));
        } else {
            // Success
            if (rpath != null) {
                // Redirect
                StringBuffer path = new StringBuffer();
                for (String part : rpath) {
                    if ("".equals(part))
                        continue;
                    path.append('/');
                    path.append(part);
                }
                boolean first = true;
                for (Map.Entry<String, String> e : rparams.entrySet()) {
                    path.append(first ? '?' : '&');
                    first = false;
                    path.append(URLEncoder.encode(e.getKey(), "UTF-8"));
                    path.append('=');
                    path.append(URLEncoder.encode(e.getValue(), "UTF-8"));
                }
                if (secondary_redirect)
                    status = set_status();
                else
                    status = 303;
                out.put("redirect", path.toString());
            } else {
                status = set_status();
            }
        }
        out.put("body", body_out.toString("UTF-8"));
        if (dataType != null) {
            if (dataType.equals("json")) {
                out.put("body", new JSONObject(body_out.toString("UTF-8")));
            }
        }
        if (body_out != null)
            body_out.close();
        out.put("mime", mime_type_out);
    } catch (IOException e) {
        throw new UIException("Could not send error", e);
    }
    out.put("status", status);
    return out;
}
Also used : JSONObject(org.json.JSONObject) UIException(org.collectionspace.csp.api.ui.UIException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with UIException

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

the class WebUIRequest method getPostBody.

@Override
public JSONObject getPostBody() throws UIException {
    JSONObject jsondata = new JSONObject();
    String jsonString = body;
    try {
        if (jsonString.length() > 0) {
            String[] data = jsonString.split("&");
            for (String item : data) {
                String[] itembits = item.split("=");
                jsondata.put(URLDecoder.decode(itembits[0], "UTF-8"), URLDecoder.decode(itembits[1], "UTF-8"));
            }
        }
    } catch (JSONException e) {
        throw new UIException("Cannot get request body, JSONException", e);
    } catch (UnsupportedEncodingException e) {
        throw new UIException("Cannot get request body, UnsupportedEncodingException", e);
    }
    return jsondata;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with UIException

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

the class WebUIRequest method solidify.

public void solidify(boolean close) throws UIException {
    try {
        // we write all the data.
        if (cacheMaxAgeSeconds <= 0) {
            /*
				 * By default, we disable caching for now (for IE). We probably
				 * want to be cleverer at some point. XXX
				 */
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Last-Modified", aWhileAgoAsExpectedByExpiresHeader());
            response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        } else {
            // Create a cache header per the timeout requested (usu. by the individual request handler)
            response.addHeader("Cache-Control", "max-age=" + Integer.toString(cacheMaxAgeSeconds));
        }
        /* End of cacheing stuff */
        if (out_data != null) {
            out = response.getWriter();
            out.print(out_data);
        } else if (out_binary_data != null) {
            out_stream = response.getOutputStream();
            out_stream.write(out_binary_data);
        } else if (out_input_stream != null) {
            out_stream = response.getOutputStream();
            IOUtils.copy(out_input_stream, out_stream);
        }
        if (solidified) {
            if (close) {
                close();
            }
            return;
        }
        solidified = true;
        if (failure) {
            // Failed
            int status = getStatus() == null ? 400 : getStatus();
            response.setStatus(status);
            // 
            if (out_data == null) {
                if (failing_exception != null) {
                    response.sendError(status, ExceptionUtils.getFullStackTrace(failing_exception));
                } else {
                    response.sendError(status, "No underlying exception");
                }
            } else {
                // 
                if (out_data.contains("isError")) {
                    setSuccessStatus();
                }
            }
        } else {
            // Success
            setSession();
            if (rpath != null) {
                // Redirect
                StringBuffer path = new StringBuffer();
                for (String part : rpath) {
                    if ("".equals(part))
                        continue;
                    path.append('/');
                    path.append(part);
                }
                boolean first = true;
                for (Map.Entry<String, String> e : rargs.entrySet()) {
                    path.append(first ? '?' : '&');
                    first = false;
                    path.append(URLEncoder.encode(e.getKey(), "UTF-8"));
                    path.append('=');
                    path.append(URLEncoder.encode(e.getValue(), "UTF-8"));
                }
                if (secondary_redirect)
                    setSuccessStatus();
                else
                    response.setStatus(303);
                response.setHeader("Location", path.toString());
            } else {
                setSuccessStatus();
            }
        }
        if (close)
            close();
    } catch (IOException e) {
        throw new UIException("Could not send error", e);
    } finally {
        // reset the out_data field for possible additional requests?
        out_data = null;
    }
}
Also used : UIException(org.collectionspace.csp.api.ui.UIException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with UIException

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

the class WebUIRequest method getTTYOutputter.

// NOTE No changes to solidified stuff can happen after you get the TTY outputter
@Override
public TTYOutputter getTTYOutputter() throws UIException {
    try {
        WebTTYOutputter tty = new WebTTYOutputter(response);
        solidify(false);
        out = tty.getWriter();
        return tty;
    } catch (IOException e) {
        throw new UIException("Cannot create response PrintWriter", e);
    }
}
Also used : UIException(org.collectionspace.csp.api.ui.UIException) IOException(java.io.IOException)

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