Search in sources :

Example 1 with Setting

use of org.platformlayer.xaas.model.Setting 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)

Aggregations

Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JAXBException (javax.xml.bind.JAXBException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 ServiceType (org.platformlayer.ids.ServiceType)1 ServiceAuthorization (org.platformlayer.xaas.model.ServiceAuthorization)1 Setting (org.platformlayer.xaas.model.Setting)1 SettingCollection (org.platformlayer.xaas.model.SettingCollection)1 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)1 JaxbHelper (org.platformlayer.xml.JaxbHelper)1