Search in sources :

Example 76 with JAXBException

use of javax.xml.bind.JAXBException in project opennms by OpenNMS.

the class ValidatingMessageBodyReader method readFrom.

@Override
public T readFrom(final Class<T> clazz, final Type type, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> parameters, final InputStream stream) throws IOException, WebApplicationException {
    LOG.debug("readFrom: {}/{}/{}", clazz.getSimpleName(), type, mediaType);
    JAXBContext jaxbContext = null;
    final ContextResolver<JAXBContext> resolver = providers.getContextResolver(JAXBContext.class, mediaType);
    try {
        if (resolver != null) {
            jaxbContext = resolver.getContext(clazz);
        }
        if (jaxbContext == null) {
            jaxbContext = JAXBContext.newInstance(clazz);
        }
        return JaxbUtils.unmarshal(clazz, new InputSource(stream), jaxbContext);
    } catch (final JAXBException e) {
        LOG.warn("An error occurred while unmarshaling a {} object", clazz.getSimpleName(), e);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : InputSource(org.xml.sax.InputSource) WebApplicationException(javax.ws.rs.WebApplicationException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 77 with JAXBException

use of javax.xml.bind.JAXBException in project opennms by OpenNMS.

the class HypericAckProcessor method parseHypericAlerts.

/**
     * <p>parseHypericAlerts</p>
     *
     * @param reader a {@link java.io.Reader} object.
     * @return a {@link java.util.List} object.
     * @throws javax.xml.bind.JAXBException if any.
     * @throws javax.xml.stream.XMLStreamException if any.
     */
public static List<HypericAlertStatus> parseHypericAlerts(Reader reader) throws JAXBException, XMLStreamException {
    List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>();
    // Instantiate a JAXB context to parse the alert status
    JAXBContext context = JAXBContext.newInstance(new Class[] { HypericAlertStatuses.class, HypericAlertStatus.class });
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLEventReader xmler = xmlif.createXMLEventReader(reader);
    EventFilter filter = new EventFilter() {

        @Override
        public boolean accept(XMLEvent event) {
            return event.isStartElement();
        }
    };
    XMLEventReader xmlfer = xmlif.createFilteredReader(xmler, filter);
    // Read up until the beginning of the root element
    StartElement startElement = (StartElement) xmlfer.nextEvent();
    // Fetch the root element name for {@link HypericAlertStatus} objects
    String rootElementName = context.createJAXBIntrospector().getElementName(new HypericAlertStatuses()).getLocalPart();
    if (rootElementName.equals(startElement.getName().getLocalPart())) {
        Unmarshaller unmarshaller = context.createUnmarshaller();
        // Use StAX to pull parse the incoming alert statuses
        while (xmlfer.peek() != null) {
            Object object = unmarshaller.unmarshal(xmler);
            if (object instanceof HypericAlertStatus) {
                HypericAlertStatus alertStatus = (HypericAlertStatus) object;
                retval.add(alertStatus);
            }
        }
    } else {
        // Try to pull in the HTTP response to give the user a better idea of what went wrong
        StringBuffer errorContent = new StringBuffer();
        LineNumberReader lineReader = new LineNumberReader(reader);
        try {
            String line;
            while (true) {
                line = lineReader.readLine();
                if (line == null) {
                    break;
                } else {
                    errorContent.append(line.trim());
                }
            }
        } catch (IOException e) {
            errorContent.append("Exception while trying to print out message content: " + e.getMessage());
        }
        // Throw an exception and include the erroneous HTTP response in the exception text
        throw new JAXBException("Found wrong root element in Hyperic XML document, expected: \"" + rootElementName + "\", found \"" + startElement.getName().getLocalPart() + "\"\n" + errorContent.toString());
    }
    return retval;
}
Also used : JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) EventFilter(javax.xml.stream.EventFilter) LineNumberReader(java.io.LineNumberReader) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 78 with JAXBException

use of javax.xml.bind.JAXBException in project OpenAM by OpenRock.

the class ExportMetaData method runWSFedExportMeta.

private void runWSFedExportMeta() throws CLIException {
    PrintWriter pw = null;
    String out = (isWebBase) ? "web" : metadata;
    Object[] objs = { out };
    Object[] objs2 = { entityID, realm };
    try {
        WSFederationMetaManager metaManager = new WSFederationMetaManager(ssoToken);
        FederationElement federation = metaManager.getEntityDescriptor(realm, entityID);
        if (federation == null) {
            throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-entity-descriptor-not-exist"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
        }
        String xmlstr = WSFederationMetaUtils.convertJAXBToString(federation);
        xmlstr = WSFederationMetaSecurityUtils.formatBase64BinaryElement(xmlstr);
        if (isWebBase) {
            getOutputWriter().printlnMessage(xmlstr);
        } else {
            pw = new PrintWriter(new FileWriter(metadata));
            pw.print(xmlstr);
        }
        getOutputWriter().printlnMessage(MessageFormat.format(getResourceString("export-entity-export-descriptor-succeeded"), objs));
    } catch (WSFederationMetaException e) {
        debugError("ExportMetaData.runExportMeta", e);
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IOException e) {
        debugError("ExportMetaData.runExportMeta", e);
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (JAXBException e) {
        debugWarning("ExportMetaData.runExportMeta", e);
        throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-invalid_descriptor"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IllegalArgumentException e) {
        debugWarning("ExportMetaData.runExportMeta", e);
        throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-invalid_descriptor"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
}
Also used : WSFederationMetaManager(com.sun.identity.wsfederation.meta.WSFederationMetaManager) FileWriter(java.io.FileWriter) JAXBException(javax.xml.bind.JAXBException) CLIException(com.sun.identity.cli.CLIException) WSFederationMetaException(com.sun.identity.wsfederation.meta.WSFederationMetaException) IOException(java.io.IOException) FederationElement(com.sun.identity.wsfederation.jaxb.wsfederation.FederationElement) PrintWriter(java.io.PrintWriter)

Example 79 with JAXBException

use of javax.xml.bind.JAXBException in project OpenAM by OpenRock.

the class ExportMetaData method runIDFFExportMeta.

private void runIDFFExportMeta() throws CLIException {
    PrintWriter pw = null;
    String out = (isWebBase) ? "web" : metadata;
    Object[] objs = { out };
    Object[] objs2 = { entityID, realm };
    try {
        IDFFMetaManager metaManager = new IDFFMetaManager(ssoToken);
        com.sun.identity.liberty.ws.meta.jaxb.EntityDescriptorElement descriptor = metaManager.getEntityDescriptor(realm, entityID);
        if (descriptor == null) {
            throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-entity-descriptor-not-exist"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
        }
        String xmlstr = IDFFMetaUtils.convertJAXBToString(descriptor);
        xmlstr = SAML2MetaSecurityUtils.formatBase64BinaryElement(xmlstr);
        if (isWebBase) {
            getOutputWriter().printlnMessage(xmlstr);
        } else {
            pw = new PrintWriter(new FileWriter(metadata));
            pw.print(xmlstr);
        }
        getOutputWriter().printlnMessage(MessageFormat.format(getResourceString("export-entity-export-descriptor-succeeded"), objs));
    } catch (IDFFMetaException e) {
        debugError("ExportMetaData.runIDFFExportMeta", e);
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IOException e) {
        debugError("ExportMetaData.runIDFFExportMeta", e);
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (JAXBException e) {
        debugWarning("ExportMetaData.runIDFFExportMeta", e);
        throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-invalid_descriptor"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IllegalArgumentException e) {
        debugWarning("ExportMetaData.runExportMeta", e);
        throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-invalid_descriptor"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
}
Also used : IDFFMetaException(com.sun.identity.federation.meta.IDFFMetaException) FileWriter(java.io.FileWriter) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) IDFFMetaManager(com.sun.identity.federation.meta.IDFFMetaManager) CLIException(com.sun.identity.cli.CLIException) PrintWriter(java.io.PrintWriter)

Example 80 with JAXBException

use of javax.xml.bind.JAXBException in project OpenAM by OpenRock.

the class ExportMetaData method runIDFFExportExtended.

private void runIDFFExportExtended() throws CLIException {
    OutputStream os = null;
    String out = (isWebBase) ? "web" : extendedData;
    Object[] objs = { out };
    Object[] objs2 = { entityID, realm };
    try {
        IDFFMetaManager metaManager = new IDFFMetaManager(ssoToken);
        com.sun.identity.federation.jaxb.entityconfig.EntityConfigElement config = metaManager.getEntityConfig(realm, entityID);
        if (config == null) {
            throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-entity-config-not-exist"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
        }
        String xmlString = IDFFMetaUtils.convertJAXBToString(config);
        if (isWebBase) {
            getOutputWriter().printlnMessage(xmlString);
        } else {
            os = new FileOutputStream(extendedData);
            os.write(xmlString.getBytes());
        }
        getOutputWriter().printlnMessage(MessageFormat.format(getResourceString("export-entity-export-config-succeeded"), objs));
    } catch (IDFFMetaException e) {
        debugWarning("ExportMetaData.runIDFFExportExtended", e);
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IOException e) {
        debugWarning("ExportMetaData.runIDFFExportExtended", e);
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (JAXBException e) {
        debugWarning("ExportMetaData.runIDFFExportExtended", e);
        throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IllegalArgumentException e) {
        debugWarning("ExportMetaData.runIDFFExportExtended", e);
        throw new CLIException(MessageFormat.format(getResourceString("export-entity-exception-invalid-config"), objs2), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : IDFFMetaException(com.sun.identity.federation.meta.IDFFMetaException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) IDFFMetaManager(com.sun.identity.federation.meta.IDFFMetaManager) FileOutputStream(java.io.FileOutputStream) CLIException(com.sun.identity.cli.CLIException)

Aggregations

JAXBException (javax.xml.bind.JAXBException)402 JAXBContext (javax.xml.bind.JAXBContext)126 IOException (java.io.IOException)93 Unmarshaller (javax.xml.bind.Unmarshaller)91 Marshaller (javax.xml.bind.Marshaller)69 ArrayList (java.util.ArrayList)38 StringWriter (java.io.StringWriter)35 List (java.util.List)35 Map (java.util.Map)33 SAXException (org.xml.sax.SAXException)32 File (java.io.File)29 InputStream (java.io.InputStream)29 AMConsoleException (com.sun.identity.console.base.model.AMConsoleException)28 HashSet (java.util.HashSet)28 JAXBElement (javax.xml.bind.JAXBElement)24 XMLStreamException (javax.xml.stream.XMLStreamException)23 SAML2MetaException (com.sun.identity.saml2.meta.SAML2MetaException)22 StringReader (java.io.StringReader)21 HashMap (java.util.HashMap)21 SAML2MetaManager (com.sun.identity.saml2.meta.SAML2MetaManager)20