Example usage for javax.xml.bind JAXBException getCause

List of usage examples for javax.xml.bind JAXBException getCause

Introduction

In this page you can find the example usage for javax.xml.bind JAXBException getCause.

Prototype

@Override
    public Throwable getCause() 

Source Link

Usage

From source file:de.tu_dortmund.ub.api.paaa.PaaaEndpoint.java

private void sendRequestError(HttpServletResponse httpServletResponse, RequestError requestError,
        String format) {//from  ww  w . ja va2  s  . c o  m

    ObjectMapper mapper = new ObjectMapper();

    httpServletResponse.setHeader("WWW-Authentificate", "Bearer");
    httpServletResponse.setHeader("WWW-Authentificate", "Bearer realm=\"PAAA\"");
    httpServletResponse.setContentType("application/json");

    try {

        // XML-Ausgabe mit JAXB
        if (format.equals("xml")) {

            try {

                JAXBContext context = JAXBContext.newInstance(RequestError.class);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

                // Write to HttpResponse
                httpServletResponse.setContentType("application/xml;charset=UTF-8");
                m.marshal(requestError, httpServletResponse.getWriter());
            } catch (JAXBException e) {
                this.logger.error(e.getMessage(), e.getCause());
                httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "Internal Server Error: Error while rendering the results.");
            }
        }

        // JSON-Ausgabe mit Jackson
        if (format.equals("json")) {

            httpServletResponse.setContentType("application/json;charset=UTF-8");
            mapper.writeValue(httpServletResponse.getWriter(), requestError);
        }
    } catch (Exception e) {

        e.printStackTrace();
    }
}

From source file:de.tu_dortmund.ub.api.paaa.PaaaEndpoint.java

/**
 *
 * @param httpServletRequest/*from www  .  ja v  a  2 s  . co  m*/
 * @param httpServletResponse
 * @throws IOException
 */
private void authorize(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        String format) throws IOException {

    ObjectMapper mapper = new ObjectMapper();

    // Error handling mit suppress_response_codes=true
    if (httpServletRequest.getParameter("suppress_response_codes") != null) {
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
    }
    // Error handling mit suppress_response_codes=false (=default)
    else {
        httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    }

    // Json fr Response body
    RequestError requestError = new RequestError();
    requestError.setError(
            this.config.getProperty("error." + Integer.toString(HttpServletResponse.SC_UNAUTHORIZED)));
    requestError.setCode(HttpServletResponse.SC_UNAUTHORIZED);
    requestError.setDescription(this.config
            .getProperty("error." + Integer.toString(HttpServletResponse.SC_UNAUTHORIZED) + ".description"));
    requestError.setErrorUri(
            this.config.getProperty("error." + Integer.toString(HttpServletResponse.SC_UNAUTHORIZED) + ".uri"));

    // XML-Ausgabe mit JAXB
    if (format.equals("xml")) {

        try {

            JAXBContext context = JAXBContext.newInstance(RequestError.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            // Write to HttpResponse
            httpServletResponse.setContentType("application/xml;charset=UTF-8");
            m.marshal(requestError, httpServletResponse.getWriter());

        } catch (JAXBException e) {
            this.logger.error(e.getMessage(), e.getCause());
            httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    "Internal Server Error: Error while rendering the results.");
        }
    }

    // JSON-Ausgabe mit Jackson
    if (format.equals("json")) {

        httpServletResponse.setContentType("application/json;charset=UTF-8");
        mapper.writeValue(httpServletResponse.getWriter(), requestError);
    }
}

From source file:de.tu_dortmund.ub.api.paia.core.PaiaCoreEndpoint.java

/**
 *
 * @param httpServletRequest//from   w  w  w. j av  a  2s  .  com
 * @param httpServletResponse
 * @throws IOException
 */
private void authorize(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        String format, DocumentList documents) throws IOException {

    httpServletResponse.setHeader("Access-Control-Allow-Origin",
            config.getProperty("Access-Control-Allow-Origin"));
    httpServletResponse.setHeader("Cache-Control", config.getProperty("Cache-Control"));

    ObjectMapper mapper = new ObjectMapper();

    // Error handling mit suppress_response_codes=true
    if (httpServletRequest.getParameter("suppress_response_codes") != null) {
        httpServletResponse.setStatus(HttpServletResponse.SC_OK);
    }
    // Error handling mit suppress_response_codes=false (=default)
    else {
        httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    }

    // Json fr Response body
    RequestError requestError = new RequestError();
    requestError.setError(
            this.config.getProperty("error." + Integer.toString(HttpServletResponse.SC_UNAUTHORIZED)));
    requestError.setCode(HttpServletResponse.SC_UNAUTHORIZED);
    requestError.setDescription(this.config
            .getProperty("error." + Integer.toString(HttpServletResponse.SC_UNAUTHORIZED) + ".description"));
    requestError.setErrorUri(
            this.config.getProperty("error." + Integer.toString(HttpServletResponse.SC_UNAUTHORIZED) + ".uri"));

    // XML-Ausgabe mit JAXB
    if (format.equals("xml")) {

        try {

            JAXBContext context = JAXBContext.newInstance(RequestError.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            // Write to HttpResponse
            httpServletResponse.setContentType("application/xml;charset=UTF-8");
            m.marshal(requestError, httpServletResponse.getWriter());

        } catch (JAXBException e) {
            this.logger.error(e.getMessage(), e.getCause());
            httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    "Internal Server Error: Error while rendering the results.");
        }
    }

    // JSON-Ausgabe mit Jackson
    if (format.equals("json")) {

        httpServletResponse.setContentType("application/json;charset=UTF-8");
        mapper.writeValue(httpServletResponse.getWriter(), requestError);
    }

    // html > redirect zu "PAIA auth - login" mit redirect_url = "PAIA core - service"
    if (format.equals("html")) {

        httpServletResponse.setContentType("text/html;charset=UTF-8");

        if (documents != null) {
            // set Cookie with urlencoded DocumentList-JSON
            StringWriter stringWriter = new StringWriter();
            mapper.writeValue(stringWriter, documents);
            Cookie cookie = new Cookie("PaiaServiceDocumentList",
                    URLEncoder.encode(stringWriter.toString(), "UTF-8"));
            if (this.config.getProperty("service.cookie.domain") != null
                    && !this.config.getProperty("service.cookie.domain").equals("")) {
                cookie.setDomain(this.config.getProperty("service.cookie.domain"));
            }
            cookie.setMaxAge(-1);
            cookie.setPath("/");
            httpServletResponse.addCookie(cookie);
        }

        //String redirect_url = "http://" + httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort() + this.config.getProperty("service.endpoint.core") + httpServletRequest.getPathInfo();
        String redirect_url = this.config.getProperty("service.base_url")
                + this.config.getProperty("service.endpoint.core") + httpServletRequest.getPathInfo();
        if (httpServletRequest.getQueryString() != null && !httpServletRequest.getQueryString().equals("")) {
            redirect_url += "?" + httpServletRequest.getQueryString();
        }
        this.logger.info("redirect_url = " + redirect_url);

        //String login_url = "http://" + httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort() + this.config.getProperty("service.endpoint.auth") + "/login?redirect_url=" + redirect_url;
        String login_url = this.config.getProperty("service.base_url")
                + this.config.getProperty("service.endpoint.auth") + "/login?redirect_url=" + redirect_url;
        this.logger.info("login_url = " + login_url);

        httpServletResponse.sendRedirect(login_url);
    }
}

From source file:net.emotivecloud.scheduler.drp4one.DRP4OVF.java

private OCAComputeWrapper parseOcaCompute(String s) {
    OCAComputeWrapper rv = null;// w w  w .j a va2 s . co m
    StringBuilder cause = new StringBuilder();
    try {
        rv = OCAComputeWrapperFactory.parse(s);
    } catch (SAXException se) {
        throw new DRPOneException("XML Parsing error", se, StatusCodes.INTERNAL);
    } catch (JAXBException e) {
        if (e instanceof PropertyException) {
            cause.append("Access to property failed: " + e.getErrorCode());
        } else if (e instanceof MarshalException) {
            cause.append("Marshalling failed: " + e.getLocalizedMessage());
        } else if (e instanceof UnmarshalException) {
            cause.append("Unmarshalling failed: " + e.getCause());
        } else if (e instanceof ValidationException) {
            cause.append("XML Validation failed: " + e.getErrorCode());
        } else {
            cause.append("Unespected " + e.getErrorCode());
            cause.append(e.getClass().getName());
            cause.append(": ");
        }
        cause.append(e.getMessage());
        log.error(cause.toString());
        if (log.isTraceEnabled()) {
            log.trace(cause, e);
        }
        throw new DRPOneException(cause.toString(), e, StatusCodes.ONE_FAILURE);
    }
    return rv;
}

From source file:net.emotivecloud.scheduler.drp4one.DRP4OVF.java

private OCAComputeListWrapper parseOcaComputeList(String s) {
    OCAComputeListWrapper rv = null;//  www .j  a v a  2 s. co  m
    StringBuilder cause = new StringBuilder();
    try {
        rv = OCAComputeListWrapperFactory.parseList(s);
    } catch (SAXException se) {
        throw new DRPOneException("XML Parsing error", se, StatusCodes.INTERNAL);
    } catch (JAXBException e) {
        if (e instanceof PropertyException) {
            cause.append("Access to property failed: " + e.getErrorCode());
        } else if (e instanceof MarshalException) {
            cause.append("Marshalling failed: " + e.getLocalizedMessage());
        } else if (e instanceof UnmarshalException) {
            cause.append("Unmarshalling failed: " + e.getCause());
        } else if (e instanceof ValidationException) {
            cause.append("XML Validation failed: " + e.getErrorCode());
        } else {
            cause.append("Unespected " + e.getErrorCode());
            cause.append(e.getClass().getName());
            cause.append(": ");
        }
        cause.append(e.getMessage());
        log.error(cause.toString());
        if (log.isTraceEnabled()) {
            log.trace(cause, e);
        }
        throw new DRPOneException(cause.toString(), e, StatusCodes.ONE_FAILURE);
    }
    return rv;

}

From source file:net.emotivecloud.scheduler.drp4one.DRP4OVF.java

private OVFWrapper parse(String ovfXml) throws DRPOneException {
    OVFWrapper rv = null;//w w w  . ja  va2 s .  c o m
    StringBuilder cause = new StringBuilder();

    try {
        rv = OVFWrapperFactory.parse(ovfXml);
    } catch (JAXBException e) {
        if (e instanceof PropertyException) {
            cause.append("Access to property failed: " + e.getErrorCode());
        } else if (e instanceof MarshalException) {
            cause.append("Marshalling failed: " + e.getLocalizedMessage());
        } else if (e instanceof UnmarshalException) {
            cause.append("Unmarshalling failed: " + e.getCause());
        } else if (e instanceof ValidationException) {
            cause.append("XML Validation failed: " + e.getErrorCode());
        } else {
            cause.append("Unespected " + e.getErrorCode());
            cause.append(e.getClass().getName());
            cause.append(": ");
        }

        cause.append(e.getMessage());
        log.error(cause.toString());
        if (log.isTraceEnabled()) {
            log.trace(cause, e);
        }
        throw new DRPOneException(cause.toString(), e, StatusCodes.XML_PROBLEM);
    } catch (OVFException e) {
        cause.append("Problems parsing OVF file: ");
        cause.append(e.getMessage());
        log.error(cause.toString());
        if (log.isTraceEnabled()) {
            log.trace(cause, e);
        }
        throw new DRPOneException(cause.toString(), e, StatusCodes.XML_PROBLEM);
    }
    return rv;
}

From source file:org.jts.gui.importJSIDL.Import.java

public Import() {
    try {/*  w  w  w . j  a v a  2s.  co  m*/
        if (um == null) {
            JAXBContext jc = JAXBContext.newInstance("org.jts.jsidl.binding");
            um = jc.createUnmarshaller();
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(sourceSchema));
            um.setSchema(schema);
        }
    } catch (JAXBException jaxbe) {
        throw new GUIError(jaxbe.getCause());
    } catch (SAXException saxe) {
        throw new GUIError(saxe.getCause());
    }
}

From source file:org.kitodo.dataformat.access.MetsXmlElementAccess.java

/**
 * Reads METS from an InputStream. JAXB is used to parse the XML.
 * // ww  w .j a v  a 2 s  .  co  m
 * @param in
 *            InputStream to read from
 */
@Override
public Workpiece read(InputStream in) throws IOException {
    try {
        JAXBContext jc = JAXBContext.newInstance(Mets.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Mets mets = (Mets) unmarshaller.unmarshal(in);
        return new MetsXmlElementAccess(mets).workpiece;
    } catch (JAXBException e) {
        if (e.getCause() instanceof IOException) {
            throw (IOException) e.getCause();
        } else {
            throw new IOException(e.getMessage(), e);
        }
    }
}

From source file:org.kitodo.dataformat.access.MetsXmlElementAccess.java

/**
 * Writes the contents of this workpiece as a METS file into an output
 * stream./*from  w w w .  j  a v a  2  s . c  om*/
 * 
 * @param out
 *            writable output stream
 * @throws IOException
 *             if the output device has an error
 */
@Override
public void save(Workpiece workpiece, OutputStream out) throws IOException {
    try {
        JAXBContext context = JAXBContext.newInstance(Mets.class);
        Marshaller marshal = context.createMarshaller();
        marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshal.marshal(new MetsXmlElementAccess(workpiece).toMets(), out);
    } catch (JAXBException e) {
        if (e.getCause() instanceof IOException) {
            throw (IOException) e.getCause();
        } else {
            throw new IOException(e.getMessage(), e);
        }
    }
}

From source file:org.opennaas.extensions.network.test.DummyParserTest.java

public OutputStream saveNetworkDescriptor(NetworkTopology networkDescriptor, OutputStream stream) {

    try {//from w w  w .jav  a2s.c  o m
        JAXBContext context = JAXBContext.newInstance(NetworkTopology.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(networkDescriptor, stream);
    } catch (JAXBException e) {
        log.error(e.getMessage(), e.getCause());
    }
    return stream;

}