parent
83fbfc98be
commit
340cf2a003
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.apimgt.api.APIManagementException;
|
||||
import org.wso2.carbon.apimgt.api.APIProvider;
|
||||
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
||||
import org.wso2.carbon.core.ServerStartupObserver;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.mobile.config.APIConfig;
|
||||
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.mobile.util.DeviceManagementAPIPublisherUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MobileDeviceManagementStartupObserver implements ServerStartupObserver {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MobileDeviceManagementStartupObserver.class);
|
||||
|
||||
public void completingServerStartup() {
|
||||
|
||||
}
|
||||
|
||||
public void completedServerStartup() {
|
||||
try {
|
||||
this.initAPIConfigs();
|
||||
/* Publish all mobile device management related JAX-RS services as APIs */
|
||||
this.publishAPIs();
|
||||
} catch (DeviceManagementException e) {
|
||||
log.error("Error occurred while publishing Mobile Device Management related APIs", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void initAPIConfigs() throws DeviceManagementException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Initializing Mobile Device Management related APIs");
|
||||
}
|
||||
List<APIConfig> apiConfigs =
|
||||
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
|
||||
getApiPublisherConfig().getAPIs();
|
||||
for (APIConfig apiConfig : apiConfigs) {
|
||||
try {
|
||||
APIProvider provider =
|
||||
APIManagerFactory.getInstance().getAPIProvider(apiConfig.getOwner());
|
||||
apiConfig.init(provider);
|
||||
} catch (APIManagementException e) {
|
||||
throw new DeviceManagementException("Error occurred while initializing API Config '" +
|
||||
apiConfig.getName() + "'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void publishAPIs() throws DeviceManagementException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Publishing Mobile Device Management related APIs");
|
||||
}
|
||||
List<APIConfig> apiConfigs =
|
||||
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
|
||||
getApiPublisherConfig().getAPIs();
|
||||
for (APIConfig apiConfig : apiConfigs) {
|
||||
DeviceManagementAPIPublisherUtil.publishAPI(apiConfig);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully published API '" + apiConfig.getName() + "' with the context '" +
|
||||
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 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.config;
|
||||
|
||||
import org.wso2.carbon.apimgt.api.APIProvider;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
|
||||
@XmlRootElement(name = "API")
|
||||
public class APIConfig {
|
||||
|
||||
private String name;
|
||||
private String owner;
|
||||
private String context;
|
||||
private String endpoint;
|
||||
private String version;
|
||||
private String transports;
|
||||
private APIProvider provider;
|
||||
|
||||
public void init(APIProvider provider) {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
@XmlTransient
|
||||
public APIProvider getProvider() {
|
||||
return provider;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Name", nillable = false)
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Owner", nillable = false)
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Context", nillable = false)
|
||||
public String getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public void setContext(String context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Endpoint", nillable = false)
|
||||
public String getEndpoint() {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
public void setEndpoint(String endpoint) {
|
||||
this.endpoint = endpoint;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Version", nillable = false)
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@XmlElement(name = "Transports", nillable = false)
|
||||
public String getTransports() {
|
||||
return transports;
|
||||
}
|
||||
|
||||
public void setTransports(String transports) {
|
||||
this.transports = transports;
|
||||
}
|
||||
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2014, 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.config;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.util.List;
|
||||
|
||||
@XmlRootElement(name = "APIPublisher")
|
||||
public class APIPublisherConfig {
|
||||
|
||||
private List<APIConfig> apis;
|
||||
|
||||
@XmlElementWrapper(name = "APIs", nillable = false, required = true)
|
||||
@XmlElement(name = "API", nillable = false)
|
||||
public List<APIConfig> getAPIs() {
|
||||
return apis;
|
||||
}
|
||||
|
||||
public void setAPIs(List<APIConfig> apis) {
|
||||
this.apis = apis;
|
||||
}
|
||||
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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.util;
|
||||
|
||||
import org.wso2.carbon.apimgt.api.APIManagementException;
|
||||
import org.wso2.carbon.apimgt.api.APIProvider;
|
||||
import org.wso2.carbon.apimgt.api.model.API;
|
||||
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
||||
import org.wso2.carbon.apimgt.api.model.APIStatus;
|
||||
import org.wso2.carbon.apimgt.api.model.URITemplate;
|
||||
import org.wso2.carbon.apimgt.impl.APIConstants;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.mobile.config.APIConfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class DeviceManagementAPIPublisherUtil {
|
||||
|
||||
enum HTTPMethod {
|
||||
GET, POST, DELETE, PUT, OPTIONS
|
||||
}
|
||||
|
||||
private static List<HTTPMethod> httpMethods;
|
||||
|
||||
static {
|
||||
httpMethods = new ArrayList<HTTPMethod>();
|
||||
httpMethods.add(HTTPMethod.GET);
|
||||
httpMethods.add(HTTPMethod.POST);
|
||||
httpMethods.add(HTTPMethod.DELETE);
|
||||
httpMethods.add(HTTPMethod.PUT);
|
||||
httpMethods.add(HTTPMethod.OPTIONS);
|
||||
}
|
||||
|
||||
public static void publishAPI(APIConfig config) throws DeviceManagementException {
|
||||
APIProvider provider = config.getProvider();
|
||||
APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion());
|
||||
API api = new API(id);
|
||||
try {
|
||||
api.setContext(config.getContext());
|
||||
api.setUrl(config.getVersion());
|
||||
api.setUriTemplates(getURITemplates(config.getEndpoint(),
|
||||
APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN));
|
||||
api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);
|
||||
api.addAvailableTiers(provider.getTiers());
|
||||
api.setEndpointSecured(false);
|
||||
api.setStatus(APIStatus.PUBLISHED);
|
||||
api.setTransports(config.getTransports());
|
||||
|
||||
provider.addAPI(api);
|
||||
} catch (APIManagementException e) {
|
||||
throw new DeviceManagementException("Error occurred while registering the API", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeAPI(APIConfig config) throws DeviceManagementException {
|
||||
try {
|
||||
APIProvider provider = config.getProvider();
|
||||
APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion());
|
||||
provider.deleteAPI(id);
|
||||
} catch (APIManagementException e) {
|
||||
throw new DeviceManagementException("Error occurred while removing API", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<URITemplate> getURITemplates(String endpoint, String authType) {
|
||||
Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
|
||||
if (APIConstants.AUTH_NO_AUTHENTICATION.equals(authType)) {
|
||||
for (HTTPMethod method : httpMethods) {
|
||||
URITemplate template = new URITemplate();
|
||||
template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
|
||||
template.setHTTPVerb(method.toString());
|
||||
template.setResourceURI(endpoint);
|
||||
template.setUriTemplate("/*");
|
||||
uriTemplates.add(template);
|
||||
}
|
||||
} else {
|
||||
for (HTTPMethod method : httpMethods) {
|
||||
URITemplate template = new URITemplate();
|
||||
if (HTTPMethod.OPTIONS.equals(method)) {
|
||||
template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
|
||||
} else {
|
||||
template.setAuthType(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN);
|
||||
}
|
||||
template.setHTTPVerb(method.toString());
|
||||
template.setResourceURI(endpoint);
|
||||
template.setUriTemplate("/*");
|
||||
uriTemplates.add(template);
|
||||
}
|
||||
}
|
||||
return uriTemplates;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue