Search in sources :

Example 1 with ContentType

use of org.apache.olingo.commons.api.format.ContentType in project teiid by teiid.

the class ODataFilter method writeError.

static void writeError(ServletRequest request, TeiidProcessingException e, HttpServletResponse httpResponse, int statusCode) throws IOException {
    httpResponse.setStatus(statusCode);
    ContentType contentType = ContentType.parse(request.getContentType());
    PrintWriter writer = httpResponse.getWriter();
    // $NON-NLS-1$
    String code = e.getCode() == null ? "" : e.getCode();
    // $NON-NLS-1$
    String message = e.getMessage() == null ? "" : e.getMessage();
    if (contentType == null || contentType.isCompatible(ContentType.APPLICATION_JSON)) {
        httpResponse.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString());
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        writer.write("{ \"error\": { \"code\": \"" + StringEscapeUtils.escapeJson(code) + "\", \"message\": \"" + StringEscapeUtils.escapeJson(message) + "\" } }");
    } else {
        httpResponse.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_XML.toContentTypeString());
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        writer.write("<m:error xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"><m:code>" + StringEscapeUtils.escapeXml10(code) + "</m:code><m:message>" + StringEscapeUtils.escapeXml10(message) + "</m:message></m:error>");
    }
    writer.close();
}
Also used : ContentType(org.apache.olingo.commons.api.format.ContentType) PrintWriter(java.io.PrintWriter)

Example 2 with ContentType

use of org.apache.olingo.commons.api.format.ContentType in project teiid by teiid.

the class BaseQueryExecution method buildError.

protected TranslatorException buildError(BinaryWSProcedureExecution execution) {
    // do some error handling
    try {
        Blob blob = (Blob) execution.getOutputParameterValues().get(0);
        if (blob != null) {
            boolean json = false;
            // $NON-NLS-1$
            String contentTypeString = getHeader(execution, "Content-Type");
            if (contentTypeString != null) {
                ContentType contentType = ContentType.parse(contentTypeString);
                if (contentType != null && ContentType.APPLICATION_JSON.isCompatible(contentType)) {
                    json = true;
                }
            }
            ODataDeserializer parser = null;
            if (json) {
                parser = new JsonDeserializer(false);
            } else {
                // TODO: it may not be atom, it could just be xml/html
                parser = new AtomDeserializer();
            }
            ODataError error = parser.toError(blob.getBinaryStream());
            return new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17013, execution.getResponseCode(), error.getCode(), error.getMessage(), error.getInnerError()));
        }
        return new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17031, execution.getResponseCode()));
    } catch (Throwable t) {
        return new TranslatorException(t);
    }
}
Also used : Blob(java.sql.Blob) ContentType(org.apache.olingo.commons.api.format.ContentType) ODataDeserializer(org.apache.olingo.client.api.serialization.ODataDeserializer) TranslatorException(org.teiid.translator.TranslatorException) AtomDeserializer(org.apache.olingo.client.core.serialization.AtomDeserializer) ODataError(org.apache.olingo.commons.api.ex.ODataError) JsonDeserializer(org.apache.olingo.client.core.serialization.JsonDeserializer)

Aggregations

ContentType (org.apache.olingo.commons.api.format.ContentType)2 PrintWriter (java.io.PrintWriter)1 Blob (java.sql.Blob)1 ODataDeserializer (org.apache.olingo.client.api.serialization.ODataDeserializer)1 AtomDeserializer (org.apache.olingo.client.core.serialization.AtomDeserializer)1 JsonDeserializer (org.apache.olingo.client.core.serialization.JsonDeserializer)1 ODataError (org.apache.olingo.commons.api.ex.ODataError)1 TranslatorException (org.teiid.translator.TranslatorException)1