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);
}
}
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;
}
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;
}
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;
}
}
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);
}
}
Aggregations