Search in sources :

Example 31 with JAXBException

use of javax.xml.bind.JAXBException in project platformlayer by platformlayer.

the class JdbcJobRepository method toXml.

private String toXml(Action action) throws RepositoryException {
    Object o;
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(action, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new RepositoryException("Error serializing action", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) RepositoryException(org.platformlayer.RepositoryException)

Example 32 with JAXBException

use of javax.xml.bind.JAXBException in project platformlayer by platformlayer.

the class MarshallerContextResolver method getContext.

@Override
public Marshaller getContext(Class<?> clazz) {
    if (clazz.equals(ManagedItemCollection.class)) {
    // OK
    } else if (ItemBase.class.isAssignableFrom(clazz)) {
    // OK
    } else {
        return null;
    }
    JAXBContext jaxbContext = jaxbContextHelper.getJaxbContext(clazz);
    try {
        Marshaller m = jaxbContext.createMarshaller();
        // m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
        m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
        return m;
    } catch (JAXBException e) {
        throw new IllegalStateException("Error creating XML marshaller", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) ItemBase(org.platformlayer.core.model.ItemBase) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 33 with JAXBException

use of javax.xml.bind.JAXBException in project platformlayer by platformlayer.

the class ServiceAuthorizationResource method createService.

// We deliberately don't support this at the moment... it's quite restrictive on our data store
// @GET
// @Produces({ APPLICATION_XML, APPLICATION_JSON })
// public <T> ServiceAuthorizationCollection getAll() {
// List<ServiceAuthorization> items = authorizationRepository.getByAccountId(getAccountId());
// ServiceAuthorizationCollection collection = new ServiceAuthorizationCollection();
// collection.items = items;
// return collection;
// }
@POST
@Consumes({ XML, JSON })
@Produces({ XML, JSON })
public <T> ServiceAuthorization createService(final ServiceAuthorization authorization) throws OpsException, RepositoryException {
    ServiceType serviceType = getServiceType();
    authorization.serviceType = serviceType.getKey();
    final ServiceProvider serviceProvider = opsSystem.getServiceProvider(serviceType);
    if (serviceProvider == null) {
        log.warn("Unknown serviceProvider: " + serviceType);
        throw new WebApplicationException(404);
    }
    String data = authorization.data;
    if (Strings.isNullOrEmpty(data)) {
        throw new IllegalArgumentException("Data is required");
    }
    data = data.trim();
    if (data.startsWith("{")) {
        // Convert to XML
        SettingCollection settings = new SettingCollection();
        settings.items = Lists.newArrayList();
        // We presume it's a simple map of keys and values
        try {
            JSONObject json = new JSONObject(data);
            @SuppressWarnings("unchecked") Iterator<String> keys = json.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = json.getString(key);
                Setting setting = new Setting();
                setting.key = key;
                setting.value = value;
                settings.items.add(setting);
            }
        } catch (JSONException e) {
            throw new IllegalArgumentException("Error parsing data", e);
        }
        JaxbHelper jaxbHelper = JaxbHelper.get(SettingCollection.class);
        String xml;
        try {
            xml = jaxbHelper.marshal(settings, false);
        } catch (JAXBException e) {
            throw new IllegalArgumentException("Error converting JSON to XML", e);
        }
        authorization.data = xml;
    }
    // Authentication authentication = getAuthentication();
    //
    // OpsContextBuilder opsContextBuilder = opsSystem.getInjector().getInstance(OpsContextBuilder.class);
    // final OpsContext opsContext = opsContextBuilder.buildOpsContext(serviceType, authentication, false);
    //
    // OpsContext.runInContext(opsContext, new CheckedCallable<Object, Exception>() {
    // @Override
    // public Object call() throws Exception {
    // serviceProvider.validateAuthorization(authorization);
    // return null;
    // }
    // });
    // serviceProvider.validateAuthorization(authorization);
    ServiceAuthorization created = authorizationRepository.createAuthorization(getProject(), authorization);
    // For security, never return the data
    created.data = null;
    return created;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SettingCollection(org.platformlayer.xaas.model.SettingCollection) Setting(org.platformlayer.xaas.model.Setting) JAXBException(javax.xml.bind.JAXBException) JSONException(org.json.JSONException) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) JSONObject(org.json.JSONObject) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) JaxbHelper(org.platformlayer.xml.JaxbHelper) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 34 with JAXBException

use of javax.xml.bind.JAXBException in project platformlayer by platformlayer.

the class CloneHelpers method cloneViaJaxb.

public static <T> T cloneViaJaxb(T o) {
    try {
        Class<T> objectClass = (Class<T>) o.getClass();
        JaxbHelper jaxbHelper = JaxbHelper.get(objectClass);
        String xml = JaxbHelper.toXml(o, false);
        return jaxbHelper.deserialize(new StringReader(xml), objectClass);
    } catch (UnmarshalException e) {
        throw new IllegalStateException("Error while cloning object", e);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error while cloning object", e);
    }
}
Also used : UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JaxbHelper(org.platformlayer.xml.JaxbHelper) StringReader(java.io.StringReader)

Example 35 with JAXBException

use of javax.xml.bind.JAXBException in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method mapToModel.

static <T extends ItemBase> T mapToModel(ProjectId project, ServiceType serviceType, ItemType itemType, ItemEntity entity, JaxbHelper jaxb, SecretProvider secretProvider) throws RepositoryException {
    try {
        int id = entity.id;
        String key = entity.key;
        int stateCode = entity.state;
        byte[] data = entity.data;
        SecretInfo secret = new SecretInfo(entity.secret);
        CryptoKey itemSecret = secretProvider.getItemSecret(secret);
        if (itemSecret == null) {
            throw new RepositoryException("Could not get secret to decrypt item");
        }
        if (itemSecret instanceof AesCryptoKey) {
            log.warn("Legacy AES crypto key on {} {} {} {}", new Object[] { project, serviceType, itemType, entity });
        }
        secret.unlock(itemSecret);
        byte[] plaintext = FathomdbCrypto.decrypt(itemSecret, data);
        String xml = new String(plaintext, Charsets.UTF_8);
        T model = (T) jaxb.unmarshal(xml);
        model.state = ManagedItemState.fromCode(stateCode);
        model.secret = secret;
        PlatformLayerKey plk = new PlatformLayerKey(null, project, serviceType, itemType, new ManagedItemId(key));
        model.setKey(plk);
        return model;
    } catch (JAXBException e) {
        throw new RepositoryException("Error deserializing data", e);
    }
}
Also used : AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) SecretInfo(org.platformlayer.core.model.SecretInfo) JAXBException(javax.xml.bind.JAXBException) CryptoKey(com.fathomdb.crypto.CryptoKey) AesCryptoKey(com.fathomdb.crypto.AesCryptoKey) RepositoryException(org.platformlayer.RepositoryException) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ManagedItemId(org.platformlayer.ids.ManagedItemId)

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