forked from community/device-mgt-plugins
parent
b57573ecae
commit
d3bf748233
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* you may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.mobile.windows.api.services;
|
||||
|
||||
|
||||
import io.swagger.annotations.*;
|
||||
import org.w3c.dom.Document;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.PluginConstants;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsDeviceEnrolmentException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.operations.WindowsOperationException;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
|
||||
/**
|
||||
* Interface for Syncml message flow.
|
||||
*/
|
||||
@SwaggerDefinition(
|
||||
info = @Info(
|
||||
version = "1.0.0",
|
||||
title = "",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = "name", value = "Syncml Endpoint"),
|
||||
@ExtensionProperty(name = "context",
|
||||
value = "/api/device-mgt/windows/v1.0/syncml"),
|
||||
})
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "devicemgt_windows", description = "")
|
||||
}
|
||||
)
|
||||
@Api(value = "Windows syncml service to initialize management session",
|
||||
description = "This carries all the resources related to Windows syncml message flow.")
|
||||
@Path("/devicemgt")
|
||||
public interface DeviceManagementService {
|
||||
@Path("/request")
|
||||
@POST
|
||||
@Consumes({PluginConstants.SYNCML_MEDIA_TYPE, MediaType.APPLICATION_XML})
|
||||
@Produces(PluginConstants.SYNCML_MEDIA_TYPE)
|
||||
@ApiOperation(
|
||||
httpMethod = "POST",
|
||||
value = "Getting pending operations for Windows device.",
|
||||
notes = "Using this API to fetching more information to enroll the Device and " +
|
||||
"getting pending operations.",
|
||||
tags = "Windows Device Management Administrative Service",
|
||||
authorizations = {
|
||||
@Authorization(
|
||||
value = "permission",
|
||||
scopes = {@AuthorizationScope(
|
||||
scope = "/device-mgt/devices/enroll/windows",
|
||||
description = "Getting pending operations and " +
|
||||
"device information to enroll the device")}
|
||||
)
|
||||
}
|
||||
)
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(
|
||||
code = 201,
|
||||
message = "Ok. \n Successfully getting pending operations.",
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Location",
|
||||
description = "URL of the activity instance that refers to the scheduled operation."),
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "Content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified. \n" +
|
||||
"Used by caches, or in conditional requests.")}),
|
||||
@ApiResponse(
|
||||
code = 303,
|
||||
message = "See Other. \n The source can be retrieved from the URL specified in the location header.",
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Location",
|
||||
description = "The Source URL of the document.")}),
|
||||
@ApiResponse(
|
||||
code = 400,
|
||||
message = "Bad Request. \n Invalid request or validation error."),
|
||||
@ApiResponse(
|
||||
code = 415,
|
||||
message = "Unsupported media type. \n The format of the requested entity was not supported.\n"),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n " +
|
||||
"Server error occurred while getting pending operations.")
|
||||
})
|
||||
Response getResponse(Document request) throws WindowsDeviceEnrolmentException, WindowsOperationException,
|
||||
NotificationManagementException, WindowsConfigurationException;
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,213 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* you may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.mobile.windows.api.services.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
||||
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.PluginConstants;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.CacheEntry;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.SyncmlMessageFormatException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.SyncmlOperationException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsDeviceEnrolmentException;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.util.AuthenticationInfo;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.util.DeviceUtil;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.common.util.WindowsAPIUtils;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.operations.*;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.*;
|
||||
import org.wso2.carbon.device.mgt.mobile.windows.api.services.DeviceManagementService;
|
||||
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
|
||||
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.wso2.carbon.device.mgt.mobile.windows.api.common.util.WindowsAPIUtils.convertToDeviceIdentifierObject;
|
||||
|
||||
|
||||
public class DeviceManagementServiceImpl implements DeviceManagementService {
|
||||
private static Log log = LogFactory.getLog(
|
||||
org.wso2.carbon.device.mgt.mobile.windows.api.services.syncml.impl.SyncmlServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public Response getResponse(Document request) throws WindowsDeviceEnrolmentException, WindowsOperationException,
|
||||
NotificationManagementException, WindowsConfigurationException {
|
||||
|
||||
int msgId;
|
||||
int sessionId;
|
||||
String user;
|
||||
String token;
|
||||
String response;
|
||||
SyncmlDocument syncmlDocument;
|
||||
List<Operation> deviceInfoOperations;
|
||||
List<? extends Operation> pendingOperations;
|
||||
OperationHandler operationHandler = new OperationHandler();
|
||||
DeviceInfo deviceInfo = new DeviceInfo();
|
||||
OperationReply operationReply = new OperationReply();
|
||||
|
||||
try {
|
||||
if (SyncmlParser.parseSyncmlPayload(request) != null) {
|
||||
syncmlDocument = SyncmlParser.parseSyncmlPayload(request);
|
||||
SyncmlHeader syncmlHeader = syncmlDocument.getHeader();
|
||||
sessionId = syncmlHeader.getSessionId();
|
||||
user = syncmlHeader.getSource().getLocName();
|
||||
DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject(syncmlHeader.getSource().
|
||||
getLocURI());
|
||||
msgId = syncmlHeader.getMsgID();
|
||||
if ((PluginConstants.SyncML.SYNCML_FIRST_MESSAGE_ID == msgId) &&
|
||||
(PluginConstants.SyncML.SYNCML_FIRST_SESSION_ID == sessionId)) {
|
||||
token = syncmlHeader.getCredential().getData();
|
||||
CacheEntry cacheToken = (CacheEntry) DeviceUtil.getCacheEntry(token);
|
||||
|
||||
if ((cacheToken.getUsername() != null) && (cacheToken.getUsername().equals(user))) {
|
||||
|
||||
if (modifyEnrollWithMoreDetail(request)) {
|
||||
pendingOperations = operationHandler.getPendingOperations(syncmlDocument);
|
||||
response = operationReply.generateReply(syncmlDocument,pendingOperations);
|
||||
return Response.status(Response.Status.OK).entity(response).build();
|
||||
} else {
|
||||
String msg = "Error occurred in device enrollment.";
|
||||
log.error(msg);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
} else {
|
||||
String msg = "Authentication failure due to incorrect credentials.";
|
||||
log.error(msg);
|
||||
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
|
||||
}
|
||||
} else if (sessionId >= PluginConstants.SyncML.SYNCML_SECOND_SESSION_ID) {
|
||||
if ((syncmlDocument.getBody().getAlert() != null)) {
|
||||
if (!syncmlDocument.getBody().getAlert().getData().equals(Constants.DISENROLL_ALERT_DATA)) {
|
||||
pendingOperations = operationHandler.getPendingOperations(syncmlDocument);
|
||||
return Response.ok().entity(operationReply.generateReply(
|
||||
syncmlDocument, pendingOperations)).build();
|
||||
} else {
|
||||
if (WindowsAPIUtils.getDeviceManagementService().getDevice(deviceIdentifier) != null) {
|
||||
WindowsAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
||||
return Response.ok().entity(operationReply.generateReply(syncmlDocument, null)).build();
|
||||
} else {
|
||||
String msg = "Enrolled device can not be found in the server.";
|
||||
log.error(msg);
|
||||
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pendingOperations = operationHandler.getPendingOperations(syncmlDocument);
|
||||
return Response.ok().entity(operationReply.generateReply(
|
||||
syncmlDocument, pendingOperations)).build();
|
||||
}
|
||||
} else {
|
||||
String msg = "Failure occurred in Device request message.";
|
||||
log.error(msg);
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||
}
|
||||
}
|
||||
} catch (SyncmlMessageFormatException e) {
|
||||
String msg = "Error occurred while parsing syncml request.";
|
||||
log.error(msg, e);
|
||||
throw new WindowsOperationException(msg, e);
|
||||
} catch (OperationManagementException e) {
|
||||
String msg = "Cannot access operation management service.";
|
||||
log.error(msg, e);
|
||||
throw new WindowsOperationException(msg, e);
|
||||
} catch (SyncmlOperationException e) {
|
||||
String msg = "Error occurred while getting effective feature.";
|
||||
log.error(msg, e);
|
||||
throw new WindowsConfigurationException(msg, e);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Failure occurred in dis-enrollment flow.";
|
||||
log.error(msg, e);
|
||||
throw new WindowsOperationException(msg, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enroll phone device
|
||||
*
|
||||
* @param request Device syncml request for the server side.
|
||||
* @return enroll state
|
||||
* @throws WindowsDeviceEnrolmentException
|
||||
* @throws WindowsOperationException
|
||||
*/
|
||||
private boolean modifyEnrollWithMoreDetail(Document request) throws WindowsDeviceEnrolmentException,
|
||||
WindowsOperationException {
|
||||
|
||||
String devMan;
|
||||
String devMod;
|
||||
boolean status = false;
|
||||
String user;
|
||||
SyncmlDocument syncmlDocument;
|
||||
|
||||
try {
|
||||
syncmlDocument = SyncmlParser.parseSyncmlPayload(request);
|
||||
ReplaceTag replace = syncmlDocument.getBody().getReplace();
|
||||
List<ItemTag> itemList = replace.getItems();
|
||||
devMan = itemList.get(PluginConstants.SyncML.DEVICE_MAN_POSITION).getData();
|
||||
devMod = itemList.get(PluginConstants.SyncML.DEVICE_MODEL_POSITION).getData();
|
||||
user = syncmlDocument.getHeader().getSource().getLocName();
|
||||
AuthenticationInfo authenticationInfo = new AuthenticationInfo();
|
||||
authenticationInfo.setUsername(user);
|
||||
WindowsAPIUtils.startTenantFlow(authenticationInfo);
|
||||
DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject(syncmlDocument.
|
||||
getHeader().getSource().getLocURI());
|
||||
Device existingDevice = WindowsAPIUtils.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
if (!existingDevice.getProperties().isEmpty()) {
|
||||
List<Device.Property> existingProperties = new ArrayList<>();
|
||||
|
||||
Device.Property vendorProperty = new Device.Property();
|
||||
vendorProperty.setName(PluginConstants.SyncML.VENDOR);
|
||||
vendorProperty.setValue(devMan);
|
||||
existingProperties.add(vendorProperty);
|
||||
|
||||
Device.Property deviceModelProperty = new Device.Property();
|
||||
deviceModelProperty.setName(PluginConstants.SyncML.MODEL);
|
||||
deviceModelProperty.setValue(devMod);
|
||||
existingProperties.add(deviceModelProperty);
|
||||
|
||||
existingDevice.setProperties(existingProperties);
|
||||
existingDevice.setDeviceIdentifier(syncmlDocument.getHeader().getSource().getLocURI());
|
||||
existingDevice.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS);
|
||||
status = WindowsAPIUtils.getDeviceManagementService().modifyEnrollment(existingDevice);
|
||||
// call effective policy for the enrolling device.
|
||||
PolicyManagerService policyManagerService = WindowsAPIUtils.getPolicyManagerService();
|
||||
policyManagerService.getEffectivePolicy(deviceIdentifier);
|
||||
return status;
|
||||
|
||||
}
|
||||
} catch (DeviceManagementException e) {
|
||||
throw new WindowsDeviceEnrolmentException("Failure occurred while enrolling device.", e);
|
||||
} catch (PolicyManagementException e) {
|
||||
throw new WindowsOperationException("Error occurred while getting effective policy.", e);
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue