Example usage for org.apache.commons.httpclient HttpStatus SC_CREATED

List of usage examples for org.apache.commons.httpclient HttpStatus SC_CREATED

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_CREATED.

Prototype

int SC_CREATED

To view the source code for org.apache.commons.httpclient HttpStatus SC_CREATED.

Click Source Link

Document

<tt>201 Created</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.wso2.carbon.cdm.api.Operation.java

@POST
public Message addOperation(DeviceOperationContext operationContext) throws CDMAPIException {
    DeviceManagementService dmService;//from   w w  w  .  j ava2 s .c  o  m
    OperationManager operationManager;
    Message responseMsg = new Message();
    try {
        dmService = CDMAPIUtils.getDeviceManagementService();
        operationManager = dmService
                .getOperationManager(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
        boolean status = operationManager.addOperation(operationContext.getOperation(),
                operationContext.getDevices());
        if (status) {
            Response.status(HttpStatus.SC_CREATED);
            responseMsg.setResponseMessage("Operation has added successfully.");
        } else {
            Response.status(HttpStatus.SC_OK);
            responseMsg.setResponseMessage("Failure in adding the Operation.");
        }
        return responseMsg;
    } catch (DeviceManagementServiceException deviceServiceMgtEx) {
        String errorMsg = "Device management service error";
        log.error(errorMsg, deviceServiceMgtEx);
        throw new CDMAPIException(errorMsg, deviceServiceMgtEx);
    } catch (DeviceManagementException deviceMgtEx) {
        String errorMsg = "Error occurred while adding the operation";
        log.error(errorMsg, deviceMgtEx);
        throw new CDMAPIException(errorMsg, deviceMgtEx);
    } catch (OperationManagementException ex) {
        String errorMsg = "Error occurred while saving the operation";
        log.error(errorMsg, ex);
        throw new CDMAPIException(errorMsg, ex);
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.ConfigurationImpl.java

@POST
public Response saveTenantConfiguration(PlatformConfiguration configuration) {
    ResponsePayload responseMsg = new ResponsePayload();
    try {//from   w  w w  .  j a va 2  s.c  o  m
        DeviceMgtAPIUtils.getTenantConfigurationManagementService().saveConfiguration(configuration,
                MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH);
        //Schedule the task service
        DeviceMgtAPIUtils.scheduleTaskService(DeviceMgtAPIUtils.getNotifierFrequency(configuration));
        responseMsg.setMessageFromServer("Tenant configuration saved successfully.");
        responseMsg.setStatusCode(HttpStatus.SC_CREATED);
        return Response.status(Response.Status.CREATED).entity(responseMsg).build();
    } catch (ConfigurationManagementException e) {
        String msg = "Error occurred while saving the tenant configuration.";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.ConfigurationImpl.java

@PUT
public Response updateConfiguration(PlatformConfiguration configuration) {
    ResponsePayload responseMsg = new ResponsePayload();
    try {/*w  w  w  .  java  2  s  .  c  o  m*/
        DeviceMgtAPIUtils.getTenantConfigurationManagementService().saveConfiguration(configuration,
                MDMAppConstants.RegistryConstants.GENERAL_CONFIG_RESOURCE_PATH);
        //Schedule the task service
        DeviceMgtAPIUtils.scheduleTaskService(DeviceMgtAPIUtils.getNotifierFrequency(configuration));
        responseMsg.setMessageFromServer("Tenant configuration updated successfully.");
        responseMsg.setStatusCode(HttpStatus.SC_CREATED);
        return Response.status(Response.Status.CREATED).entity(responseMsg).build();
    } catch (ConfigurationManagementException e) {
        String msg = "Error occurred while updating the tenant configuration.";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.DeviceNotificationImpl.java

@POST
public Response addNotification(Notification notification) {
    ResponsePayload responseMsg = new ResponsePayload();
    try {/*from w  ww  .  j ava2s.  c o  m*/
        DeviceMgtAPIUtils.getNotificationManagementService().addNotification(notification);
        responseMsg.setMessageFromServer("Notification has been added successfully.");
        responseMsg.setStatusCode(HttpStatus.SC_CREATED);
        return Response.status(Response.Status.CREATED).entity(responseMsg).build();
    } catch (NotificationManagementException e) {
        String msg = "Error occurred while updating notification status.";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.OperationImpl.java

@Override
@POST/*from w w  w.  j  a  va2  s. c om*/
public Response addOperation(DeviceOperationContext operationContext) {
    DeviceManagementProviderService dmService;
    ResponsePayload responseMsg = new ResponsePayload();
    try {
        dmService = DeviceMgtAPIUtils.getDeviceManagementService();

        //TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()"
        String type = null;
        List<DeviceIdentifier> deviceIdentifiers = operationContext.getDevices();
        if (deviceIdentifiers.size() > 0) {
            type = deviceIdentifiers.get(0).getType();
        }
        Activity activity = dmService.addOperation(type, operationContext.getOperation(),
                operationContext.getDevices());
        if (activity != null) {
            responseMsg.setStatusCode(HttpStatus.SC_CREATED);
            responseMsg.setMessageFromServer("Operation has added successfully.");
        }
        return Response.status(Response.Status.CREATED).entity(activity).build();
    } catch (OperationManagementException e) {
        String msg = "Error occurred while saving the operation";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.OperationImpl.java

@Override
@POST/*w ww .  j a v a  2s .  com*/
@Path("installApp/{tenantDomain}")
public Response installApplication(ApplicationWrapper applicationWrapper,
        @PathParam("tenantDomain") String tenantDomain) {
    ResponsePayload responseMsg = new ResponsePayload();
    ApplicationManager appManagerConnector;
    org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation = null;
    Activity activity = null;
    try {
        appManagerConnector = DeviceMgtAPIUtils.getAppManagementService();
        MobileApp mobileApp = applicationWrapper.getApplication();

        if (applicationWrapper.getDeviceIdentifiers() != null) {
            for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) {
                if (deviceIdentifier.getType().equals(Platform.android.toString())) {
                    operation = MDMAndroidOperationUtil.createInstallAppOperation(mobileApp);
                } else if (deviceIdentifier.getType().equals(Platform.ios.toString())) {
                    operation = MDMIOSOperationUtil.createInstallAppOperation(mobileApp);
                }
            }
            activity = appManagerConnector.installApplicationForDevices(operation,
                    applicationWrapper.getDeviceIdentifiers());
        }
        responseMsg.setStatusCode(HttpStatus.SC_CREATED);
        responseMsg.setMessageFromServer("Authentication installation request has been sent to the device.");
        return Response.status(Response.Status.CREATED).entity(activity).build();
    } catch (ApplicationManagementException | MDMAPIException e) {
        String msg = "Error occurred while saving the operation";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.OperationImpl.java

@Override
@POST/*ww w . ja va 2  s.c om*/
@Path("uninstallApp/{tenantDomain}")
public Response uninstallApplication(ApplicationWrapper applicationWrapper,
        @PathParam("tenantDomain") String tenantDomain) {
    ResponsePayload responseMsg = new ResponsePayload();
    ApplicationManager appManagerConnector;
    org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation = null;
    Activity activity = null;
    try {
        appManagerConnector = DeviceMgtAPIUtils.getAppManagementService();
        MobileApp mobileApp = applicationWrapper.getApplication();

        if (applicationWrapper.getDeviceIdentifiers() != null) {
            for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) {
                if (deviceIdentifier.getType().equals(Platform.android.toString())) {
                    operation = MDMAndroidOperationUtil.createAppUninstallOperation(mobileApp);
                } else if (deviceIdentifier.getType().equals(Platform.ios.toString())) {
                    operation = MDMIOSOperationUtil.createAppUninstallOperation(mobileApp);
                }
            }
            activity = appManagerConnector.installApplicationForDevices(operation,
                    applicationWrapper.getDeviceIdentifiers());
        }
        responseMsg.setStatusCode(HttpStatus.SC_CREATED);
        responseMsg.setMessageFromServer("Authentication removal request has been sent to the device.");
        return Response.status(Response.Status.CREATED).entity(activity).build();
    } catch (ApplicationManagementException | MDMAPIException e) {
        String msg = "Error occurred while saving the operation";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.PolicyImpl.java

private Response addPolicy(PolicyManagerService policyManagementService, ResponsePayload responseMsg,
        org.wso2.carbon.policy.mgt.common.Policy policy) {
    List<Device> devices = policy.getDevices();
    if (devices != null && devices.size() == 1) {
        DeviceAccessAuthorizationService deviceAccessAuthorizationService = DeviceManagementDataHolder
                .getInstance().getDeviceAccessAuthorizationService();
        DeviceIdentifier deviceIdentifier = new DeviceIdentifier(devices.get(0).getDeviceIdentifier(),
                devices.get(0).getType());
        PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext
                .getThreadLocalCarbonContext();
        String username = threadLocalCarbonContext.getUsername();
        try {//from  w  ww . j a  v  a  2 s . c  o m
            if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) {
                return Response.status(Response.Status.UNAUTHORIZED).build();
            }
        } catch (DeviceAccessAuthorizationException e) {
            String msg = "Device access authorization exception";
            log.error(msg, e);
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
        }
    }
    try {
        PolicyAdministratorPoint pap = policyManagementService.getPAP();
        pap.addPolicy(policy);
        responseMsg.setStatusCode(HttpStatus.SC_CREATED);
        responseMsg.setMessageFromServer("Policy has been added successfully.");
        return Response.status(Response.Status.CREATED).entity(responseMsg).build();
    } catch (PolicyManagementException e) {
        String msg = "Policy Management related exception";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.PolicyImpl.java

@Override
@PUT//w  w  w. ja  v  a2 s.  c om
@Path("{id}")
public Response updatePolicy(PolicyWrapper policyWrapper, @PathParam("id") int policyId) {

    PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
    ResponsePayload responseMsg = new ResponsePayload();
    org.wso2.carbon.policy.mgt.common.Policy policy = new org.wso2.carbon.policy.mgt.common.Policy();
    policy.setPolicyName(policyWrapper.getPolicyName());
    policy.setId(policyId);
    policy.setProfileId(policyWrapper.getProfileId());
    policy.setDescription(policyWrapper.getDescription());
    policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
    policy.setOwnershipType(policyWrapper.getOwnershipType());
    policy.setRoles(policyWrapper.getRoles());
    policy.setUsers(policyWrapper.getUsers());
    policy.setTenantId(policyWrapper.getTenantId());
    policy.setCompliance(policyWrapper.getCompliance());

    try {
        PolicyAdministratorPoint pap = policyManagementService.getPAP();
        pap.updatePolicy(policy);
        responseMsg.setStatusCode(HttpStatus.SC_CREATED);
        responseMsg.setMessageFromServer("Policy has been updated successfully.");
        return Response.status(Response.Status.CREATED).entity(responseMsg).build();
    } catch (PolicyManagementException e) {
        String msg = "Policy Management related exception in policy update.";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}

From source file:org.wso2.carbon.device.mgt.jaxrs.api.impl.UserImpl.java

/**
 * Method to add user to emm-user-store.
 *
 * @param userWrapper Wrapper object representing input json payload
 * @return {Response} Status of the request wrapped inside Response object
 *///from ww  w. j  a va  2  s  . c om
@Override
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public Response addUser(UserWrapper userWrapper) {
    ResponsePayload responsePayload = new ResponsePayload();
    try {
        UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
        if (userStoreManager.isExistingUser(userWrapper.getUsername())) {
            // if user already exists
            if (log.isDebugEnabled()) {
                log.debug("User by username: " + userWrapper.getUsername()
                        + " already exists. Therefore, request made to add user was refused.");
            }
            // returning response with bad request state
            responsePayload.setStatusCode(HttpStatus.SC_CONFLICT);
            responsePayload.setMessageFromServer("User by username: " + userWrapper.getUsername()
                    + " already exists. Therefore, request made to add user was refused.");
            return Response.status(Response.Status.CONFLICT).entity(responsePayload).build();
        } else {
            String initialUserPassword = generateInitialUserPassword();
            Map<String, String> defaultUserClaims = buildDefaultUserClaims(userWrapper.getFirstname(),
                    userWrapper.getLastname(), userWrapper.getEmailAddress());
            // calling addUser method of carbon user api
            userStoreManager.addUser(userWrapper.getUsername(), initialUserPassword, userWrapper.getRoles(),
                    defaultUserClaims, null);
            // invite newly added user to enroll device
            inviteNewlyAddedUserToEnrollDevice(userWrapper.getUsername(), initialUserPassword);
            // Outputting debug message upon successful addition of user
            if (log.isDebugEnabled()) {
                log.debug("User by username: " + userWrapper.getUsername() + " was successfully added.");
            }
            // returning response with success state
            responsePayload.setStatusCode(HttpStatus.SC_CREATED);
            responsePayload.setMessageFromServer(
                    "User by username: " + userWrapper.getUsername() + " was successfully added.");
            return Response.status(Response.Status.CREATED).entity(responsePayload).build();
        }
    } catch (UserStoreException | MDMAPIException e) {
        String msg = "Exception in trying to add user by username: " + userWrapper.getUsername();
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
    }
}