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