mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
merged code updates
This commit is contained in:
commit
6ec710b250
@ -0,0 +1,94 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>apimgt-extensions</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.apimgt.annotations</artifactId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - API Management Annotations</name>
|
||||||
|
<description>WSO2 Carbon - API Management Custom Annotation Module</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>API Management Extentions - Custom Annotations</Bundle-Description>
|
||||||
|
<Export-Package>
|
||||||
|
org.wso2.carbon.apimgt.annotations.*
|
||||||
|
</Export-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging,
|
||||||
|
javax.servlet,
|
||||||
|
javax.xml.*,
|
||||||
|
org.apache.commons.lang,
|
||||||
|
</Import-Package>
|
||||||
|
<Embed-Dependency>
|
||||||
|
scribe;scope=compile|runtime;inline=false;
|
||||||
|
</Embed-Dependency>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.annotations.api;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface API {
|
||||||
|
|
||||||
|
String name();
|
||||||
|
|
||||||
|
String version();
|
||||||
|
|
||||||
|
String context();
|
||||||
|
|
||||||
|
String[] tags();
|
||||||
|
}
|
||||||
@ -0,0 +1,171 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>apimgt-extensions</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<artifactId>org.wso2.carbon.apimgt.application.extension.api</artifactId>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>WSO2 Carbon - API Application Management API</name>
|
||||||
|
<description>This module provides capability to create api manager application.</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!--CXF -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxws</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.cxf</groupId>
|
||||||
|
<artifactId>cxf-rt-transports-http</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--JAX-RS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-core-asl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.jackson</groupId>
|
||||||
|
<artifactId>jackson-jaxrs</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax</groupId>
|
||||||
|
<artifactId>javaee-web-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-httpclient.wso2</groupId>
|
||||||
|
<artifactId>commons-httpclient</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.bouncycastle.wso2</groupId>
|
||||||
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.user.api</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.queuing</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.base</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.apache.axis2.wso2</groupId>
|
||||||
|
<artifactId>axis2</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.igniterealtime.smack.wso2</groupId>
|
||||||
|
<artifactId>smack</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.igniterealtime.smack.wso2</groupId>
|
||||||
|
<artifactId>smackx</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>jaxen</groupId>
|
||||||
|
<artifactId>jaxen</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>commons-fileupload.wso2</groupId>
|
||||||
|
<artifactId>commons-fileupload</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.apache.ant.wso2</groupId>
|
||||||
|
<artifactId>ant</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.apache.ant.wso2</groupId>
|
||||||
|
<artifactId>ant</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>commons-httpclient.wso2</groupId>
|
||||||
|
<artifactId>commons-httpclient</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.eclipse.equinox</groupId>
|
||||||
|
<artifactId>javax.servlet</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.registry.api</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.apimgt.application.extension</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
<source>${wso2.maven.compiler.source}</source>
|
||||||
|
<target>${wso2.maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>2.2</version>
|
||||||
|
<configuration>
|
||||||
|
<warName>${project.artifactId}</warName>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.api;
|
||||||
|
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.api.util.RegistrationProfile;
|
||||||
|
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.DELETE;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.QueryParam;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the application registration service that exposed for apimApplicationRegistration
|
||||||
|
*/
|
||||||
|
public interface ApiApplicationRegistrationService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is used to register an APIM application for tenant domain.
|
||||||
|
*/
|
||||||
|
@POST
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
Response register(@PathParam("tenantDomain") String tenantDomain,
|
||||||
|
@QueryParam("applicationName") String applicationName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is used to register api application
|
||||||
|
*
|
||||||
|
* @param registrationProfile contains the necessary attributes that are needed in order to register an app.
|
||||||
|
*/
|
||||||
|
@POST
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
Response register(RegistrationProfile registrationProfile);
|
||||||
|
|
||||||
|
@DELETE
|
||||||
|
Response unregister(@QueryParam("applicationName") String applicationName);
|
||||||
|
}
|
||||||
@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.api;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.api.util.RegistrationProfile;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.constants.ApiApplicationConstants;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.api.util.APIUtil;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
|
|
||||||
|
import javax.ws.rs.DELETE;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.QueryParam;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
|
||||||
|
public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegistrationService {
|
||||||
|
private static final Log log = LogFactory.getLog(ApiApplicationRegistrationServiceImpl.class);
|
||||||
|
|
||||||
|
@Path("register/tenants/{tenantDomain}")
|
||||||
|
@POST
|
||||||
|
public Response register(@PathParam("tenantDomain") String tenantDomain,
|
||||||
|
@QueryParam("applicationName") String applicationName) {
|
||||||
|
Response response;
|
||||||
|
try {
|
||||||
|
PrivilegedCarbonContext.startTenantFlow();
|
||||||
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
|
||||||
|
if (PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId() == -1) {
|
||||||
|
String msg = "Invalid tenant domain : " + tenantDomain;
|
||||||
|
response = Response.status(Response.Status.NOT_ACCEPTABLE).entity(msg).build();
|
||||||
|
}
|
||||||
|
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
|
||||||
|
.getRealmConfiguration().getAdminUserName();
|
||||||
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
|
||||||
|
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||||
|
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||||
|
applicationName, ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false);
|
||||||
|
return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
|
||||||
|
} catch (APIManagerException e) {
|
||||||
|
String msg = "Error occurred while registering an application '" + applicationName + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||||
|
} catch (UserStoreException e) {
|
||||||
|
String msg = "Failed to retrieve the tenant" + tenantDomain + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||||
|
} finally {
|
||||||
|
PrivilegedCarbonContext.endTenantFlow();
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("register")
|
||||||
|
@POST
|
||||||
|
public Response register(RegistrationProfile registrationProfile) {
|
||||||
|
Response response;
|
||||||
|
try {
|
||||||
|
String username = APIUtil.getAuthenticatedUser();
|
||||||
|
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||||
|
if (registrationProfile.isMappingAnExistingOAuthApp()) {
|
||||||
|
JSONObject jsonStringObject = new JSONObject();
|
||||||
|
jsonStringObject.put(ApiApplicationConstants.JSONSTRING_USERNAME_TAG, username);
|
||||||
|
jsonStringObject.put(ApiApplicationConstants.JSONSTRING_KEY_TYPE_TAG,
|
||||||
|
ApiApplicationConstants.DEFAULT_TOKEN_TYPE);
|
||||||
|
jsonStringObject.put(ApiApplicationConstants.OAUTH_CLIENT_ID, registrationProfile.getConsumerKey());
|
||||||
|
jsonStringObject.put(ApiApplicationConstants.OAUTH_CLIENT_SECRET,
|
||||||
|
registrationProfile.getConsumerSecret());
|
||||||
|
jsonStringObject.put(ApiApplicationConstants.JSONSTRING_VALIDITY_PERIOD_TAG,
|
||||||
|
ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD);
|
||||||
|
apiManagementProviderService.registerExistingOAuthApplicationToAPIApplication(
|
||||||
|
jsonStringObject.toJSONString(), registrationProfile.getApplicationName(),
|
||||||
|
registrationProfile.getConsumerKey(), username, registrationProfile.isAllowedToAllDomains());
|
||||||
|
return Response.status(Response.Status.ACCEPTED).entity("OAuth App is mapped as APIM App").build();
|
||||||
|
} else {
|
||||||
|
ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||||
|
registrationProfile.getApplicationName(), registrationProfile.getTags(),
|
||||||
|
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false);
|
||||||
|
return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build();
|
||||||
|
}
|
||||||
|
} catch (APIManagerException e) {
|
||||||
|
String msg = "Error occurred while registering an application '"
|
||||||
|
+ registrationProfile.getApplicationName() + "'";
|
||||||
|
log.error(msg, e);
|
||||||
|
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Path("unregister")
|
||||||
|
@DELETE
|
||||||
|
public Response unregister(@QueryParam("applicationName") String applicationName) {
|
||||||
|
Response response;
|
||||||
|
try {
|
||||||
|
String username = APIUtil.getAuthenticatedUser();
|
||||||
|
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||||
|
apiManagementProviderService.removeAPIApplication(applicationName, username);
|
||||||
|
return Response.status(Response.Status.ACCEPTED).build();
|
||||||
|
} catch (APIManagerException e) {
|
||||||
|
String msg = "Error occurred while removing the application '" + applicationName;
|
||||||
|
log.error(msg, e);
|
||||||
|
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.api.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides utility functions used by REST-API.
|
||||||
|
*/
|
||||||
|
public class APIUtil {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(APIUtil.class);
|
||||||
|
|
||||||
|
public static String getAuthenticatedUser() {
|
||||||
|
PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||||
|
String username = threadLocalCarbonContext.getUsername();
|
||||||
|
String tenantDomain = threadLocalCarbonContext.getTenantDomain();
|
||||||
|
if (username.endsWith(tenantDomain)) {
|
||||||
|
return username.substring(0, username.lastIndexOf("@"));
|
||||||
|
}
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getTenantDomainOftheUser() {
|
||||||
|
PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||||
|
String tenantDomain = threadLocalCarbonContext.getTenantDomain();
|
||||||
|
return tenantDomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static APIManagementProviderService getAPIManagementProviderService() {
|
||||||
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||||
|
APIManagementProviderService apiManagementProviderService =
|
||||||
|
(APIManagementProviderService) ctx.getOSGiService(APIManagementProviderService.class, null);
|
||||||
|
if (apiManagementProviderService == null) {
|
||||||
|
String msg = "API management provider service has not initialized.";
|
||||||
|
log.error(msg);
|
||||||
|
throw new IllegalStateException(msg);
|
||||||
|
}
|
||||||
|
return apiManagementProviderService;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.api.util;
|
||||||
|
|
||||||
|
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DTO class to be used when registering an ApiM application.
|
||||||
|
*/
|
||||||
|
@XmlRootElement
|
||||||
|
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
public class RegistrationProfile {
|
||||||
|
|
||||||
|
@XmlElement(required = true)
|
||||||
|
private String applicationName;
|
||||||
|
@XmlElement(required = true)
|
||||||
|
private String tags[];
|
||||||
|
@XmlElement(required = true)
|
||||||
|
private boolean isAllowedToAllDomains;
|
||||||
|
@XmlElement(required = true)
|
||||||
|
private boolean isMappingAnExistingOAuthApp;
|
||||||
|
private String consumerKey;
|
||||||
|
private String consumerSecret;
|
||||||
|
|
||||||
|
public String getApplicationName() {
|
||||||
|
return applicationName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiApplicationName(String apiApplicationName) {
|
||||||
|
this.applicationName = apiApplicationName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getTags() {
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTags(String[] tags) {
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAllowedToAllDomains() {
|
||||||
|
return isAllowedToAllDomains;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsAllowedToAllDomains(boolean isAllowedToAllDomains) {
|
||||||
|
this.isAllowedToAllDomains = isAllowedToAllDomains;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isMappingAnExistingOAuthApp() {
|
||||||
|
return isMappingAnExistingOAuthApp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsMappingAnExistingOAuthApp(boolean isMappingAnExistingOAuthApp) {
|
||||||
|
this.isMappingAnExistingOAuthApp = isMappingAnExistingOAuthApp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConsumerKey() {
|
||||||
|
return consumerKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConsumerKey(String consumerKey) {
|
||||||
|
this.consumerKey = consumerKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConsumerSecret() {
|
||||||
|
return consumerSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConsumerSecret(String consumerSecret) {
|
||||||
|
this.consumerSecret = consumerSecret;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- This file contains the list of permissions that are associated with URL end points
|
||||||
|
of the web app. Each permission should contain the name, permission path ,API path
|
||||||
|
(URL) , HTTP method and OAUTH2 authorization scope (not-required).
|
||||||
|
When defining dynamic paths for APIs, path variables are denoted by '*' notation.
|
||||||
|
NOTE: All the endpoints of the web app should be available in this file. Otherwise
|
||||||
|
it will result 403 error at the runtime.
|
||||||
|
-->
|
||||||
|
<PermissionConfiguration>
|
||||||
|
<APIVersion></APIVersion>
|
||||||
|
<!-- Device related APIs -->
|
||||||
|
<Permission>
|
||||||
|
<name>Register tenant specific application</name>
|
||||||
|
<path>/permission/super admin</path>
|
||||||
|
<url>/register/tenants/*</url>
|
||||||
|
<method>POST</method>
|
||||||
|
<scope>super_admin_user</scope>
|
||||||
|
</Permission>
|
||||||
|
<Permission>
|
||||||
|
<name>Register application</name>
|
||||||
|
<path>/device-mgt/api/application/add</path>
|
||||||
|
<url>/register</url>
|
||||||
|
<method>POST</method>
|
||||||
|
<scope>application_user</scope>
|
||||||
|
</Permission>
|
||||||
|
<Permission>
|
||||||
|
<name>Delete application</name>
|
||||||
|
<path>/device-mgt/api/application/remove</path>
|
||||||
|
<url>/unregister</url>
|
||||||
|
<method>DELETE</method>
|
||||||
|
<scope>application_user</scope>
|
||||||
|
</Permission>
|
||||||
|
</PermissionConfiguration>
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--
|
||||||
|
This file defines class loading policy of the whole container. But this behaviour can be overridden by individual webapps by putting this file into the META-INF/ directory.
|
||||||
|
-->
|
||||||
|
<Classloading xmlns="http://wso2.org/projects/as/classloading">
|
||||||
|
|
||||||
|
<!-- Parent-first or child-first. Default behaviour is child-first.-->
|
||||||
|
<ParentFirst>false</ParentFirst>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Default environments that contains provides to all the webapps. This can be overridden by individual webapps by specifing required environments
|
||||||
|
Tomcat environment is the default and every webapps gets it even if they didn't specify it.
|
||||||
|
e.g. If a webapps requires CXF, they will get both Tomcat and CXF.
|
||||||
|
-->
|
||||||
|
<Environments>CXF,Carbon</Environments>
|
||||||
|
</Classloading>
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||||
|
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||||
|
|
||||||
|
<jaxrs:server id="ApiApplicationRegistration" address="/">
|
||||||
|
<jaxrs:serviceBeans>
|
||||||
|
<bean id="ApiApplicationRegistrationService"
|
||||||
|
class="org.wso2.carbon.apimgt.application.extension.api.ApiApplicationRegistrationServiceImpl">
|
||||||
|
</bean>
|
||||||
|
</jaxrs:serviceBeans>
|
||||||
|
<jaxrs:providers>
|
||||||
|
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
|
||||||
|
</jaxrs:providers>
|
||||||
|
</jaxrs:server>
|
||||||
|
|
||||||
|
</beans>
|
||||||
|
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<web-app version="2.5"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||||
|
metadata-complete="true">
|
||||||
|
<display-name>WSO2 IoT Server</display-name>
|
||||||
|
<description>WSO2 IoT Server</description>
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>CXFServlet</servlet-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
<context-param>
|
||||||
|
<param-name>isAdminService</param-name>
|
||||||
|
<param-value>false</param-value>
|
||||||
|
</context-param>
|
||||||
|
<context-param>
|
||||||
|
<param-name>doAuthentication</param-name>
|
||||||
|
<param-value>true</param-value>
|
||||||
|
</context-param>
|
||||||
|
|
||||||
|
<!--publish to apim-->
|
||||||
|
<context-param>
|
||||||
|
<param-name>managed-api-enabled</param-name>
|
||||||
|
<param-value>false</param-value>
|
||||||
|
</context-param>
|
||||||
|
</web-app>
|
||||||
@ -0,0 +1,119 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>apimgt-extensions</artifactId>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<artifactId>org.wso2.carbon.apimgt.application.extension</artifactId>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - API Application Management</name>
|
||||||
|
<description>This module provides capability to create api manager application.</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.registry.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.apimgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.apimgt.impl</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.apimgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.apimgt.api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.json-simple.wso2</groupId>
|
||||||
|
<artifactId>json-simple</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/Abstract*</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>API Management Application Bundle</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.apimgt.application.extension.internal</Private-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging.*,
|
||||||
|
org.wso2.carbon.user.core.*,
|
||||||
|
org.wso2.carbon.apimgt.api;version="${carbon.api.mgt.version.range}",
|
||||||
|
org.wso2.carbon.apimgt.api.model;version="${carbon.api.mgt.version.range}",
|
||||||
|
org.wso2.carbon.apimgt.impl;version="${carbon.api.mgt.version.range}",
|
||||||
|
org.wso2.carbon.user.api,
|
||||||
|
org.wso2.carbon.utils.multitenancy,
|
||||||
|
org.json.simple,
|
||||||
|
org.wso2.carbon.context,
|
||||||
|
org.wso2.carbon.base
|
||||||
|
</Import-Package>
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.apimgt.application.extension.internal,
|
||||||
|
org.wso2.carbon.apimgt.application.extension.*
|
||||||
|
</Export-Package>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension;
|
||||||
|
|
||||||
|
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This comprise on operation that is been done with api manager from CDMF. This service needs to be implemented in APIM.
|
||||||
|
*/
|
||||||
|
public interface APIManagementProviderService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate and retreive application keys. if the application does exist then
|
||||||
|
* create it and subscribe to apis that are grouped with the tags.
|
||||||
|
*
|
||||||
|
* @param apiApplicationName name of the application.
|
||||||
|
* @param tags tags of the apis that application needs to be subscribed.
|
||||||
|
* @param keyType of the application.
|
||||||
|
* @param username to whom the application is created
|
||||||
|
* @return consumerkey and secrete of the created application.
|
||||||
|
* @throws APIManagerException
|
||||||
|
*/
|
||||||
|
ApiApplicationKey generateAndRetrieveApplicationKeys(String apiApplicationName, String tags[],
|
||||||
|
String keyType, String username, boolean isAllowedAllDomains)
|
||||||
|
throws APIManagerException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate and retreive application keys. if the application does exist then
|
||||||
|
* create it and subscribe to all apis.
|
||||||
|
*
|
||||||
|
* @param apiApplicationName name of the application.
|
||||||
|
* @param keyType of the application.
|
||||||
|
* @param username to whom the application is created
|
||||||
|
* @return consumerkey and secrete of the created application.
|
||||||
|
* @throws APIManagerException
|
||||||
|
*/
|
||||||
|
ApiApplicationKey generateAndRetrieveApplicationKeys(String apiApplicationName, String keyType,
|
||||||
|
String username, boolean isAllowedAllDomains)
|
||||||
|
throws APIManagerException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register existing Oauth application as apim application.
|
||||||
|
*/
|
||||||
|
void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName, String clientId,
|
||||||
|
String username, boolean isAllowedAllDomains)
|
||||||
|
throws APIManagerException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove APIM Application.
|
||||||
|
*/
|
||||||
|
void removeAPIApplication(String applicationName, String username) throws APIManagerException;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,406 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.wso2.carbon.apimgt.api.APIConsumer;
|
||||||
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.API;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.APIKey;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.Application;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.SubscribedAPI;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.Subscriber;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.constants.ApiApplicationConstants;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.util.APIManagerUtil;
|
||||||
|
import org.wso2.carbon.apimgt.impl.APIConstants;
|
||||||
|
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
||||||
|
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||||
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents an implementation of APIManagementProviderService.
|
||||||
|
*/
|
||||||
|
public class APIManagementProviderServiceImpl implements APIManagementProviderService {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(APIManagementProviderServiceImpl.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ApiApplicationKey generateAndRetrieveApplicationKeys(String apiApplicationName, String keyType,
|
||||||
|
String username, boolean isAllowedAllDomains)
|
||||||
|
throws APIManagerException {
|
||||||
|
try {
|
||||||
|
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
|
||||||
|
String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain());
|
||||||
|
int applicationId = createApplicationAndSubscribeToAllAPIs(apiApplicationName, username);
|
||||||
|
Application[] applications = apiConsumer.getApplications(apiConsumer.getSubscriber(username), groupId);
|
||||||
|
Application application = null;
|
||||||
|
for (Application app : applications) {
|
||||||
|
if (app.getId() == applicationId) {
|
||||||
|
application = app;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (application == null) {
|
||||||
|
throw new APIManagerException("Api application creation failed for " + apiApplicationName +
|
||||||
|
" to the user " + username);
|
||||||
|
}
|
||||||
|
APIKey retrievedApiApplicationKey = null;
|
||||||
|
for (APIKey apiKey : application.getKeys()) {
|
||||||
|
String applicationKeyType = apiKey.getType();
|
||||||
|
if (applicationKeyType != null && applicationKeyType.equals(keyType)) {
|
||||||
|
retrievedApiApplicationKey = apiKey;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (retrievedApiApplicationKey != null) {
|
||||||
|
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
|
||||||
|
apiApplicationKey.setConsumerKey(retrievedApiApplicationKey.getConsumerKey());
|
||||||
|
apiApplicationKey.setConsumerSecret(retrievedApiApplicationKey.getConsumerSecret());
|
||||||
|
return apiApplicationKey;
|
||||||
|
}
|
||||||
|
String[] allowedDomains = new String[1];
|
||||||
|
if (isAllowedAllDomains) {
|
||||||
|
allowedDomains[0] = ApiApplicationConstants.ALLOWED_DOMAINS;
|
||||||
|
} else {
|
||||||
|
allowedDomains[0] = APIManagerUtil.getTenantDomain();
|
||||||
|
}
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put(ApiApplicationConstants.JSONSTRING_USERNAME_TAG, username);
|
||||||
|
String ownerJsonString = jsonObject.toJSONString();
|
||||||
|
Map<String, Object> keyDetails = apiConsumer.requestApprovalForApplicationRegistration(username,
|
||||||
|
apiApplicationName,
|
||||||
|
keyType, "",
|
||||||
|
allowedDomains,
|
||||||
|
ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD,
|
||||||
|
"null", groupId,
|
||||||
|
ownerJsonString);
|
||||||
|
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
|
||||||
|
apiApplicationKey.setConsumerKey((String) keyDetails.get(APIConstants.FrontEndParameterNames
|
||||||
|
.CONSUMER_KEY));
|
||||||
|
apiApplicationKey.setConsumerSecret((String) keyDetails.get(
|
||||||
|
APIConstants.FrontEndParameterNames.CONSUMER_SECRET));
|
||||||
|
return apiApplicationKey;
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("Failed to register a api application : " + apiApplicationName, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void registerExistingOAuthApplicationToAPIApplication(String jsonString, String applicationName,
|
||||||
|
String clientId, String username,
|
||||||
|
boolean isAllowedAllDomains)
|
||||||
|
throws APIManagerException {
|
||||||
|
try {
|
||||||
|
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
|
||||||
|
if (apiConsumer != null) {
|
||||||
|
String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain());
|
||||||
|
createApplication(apiConsumer, applicationName, username, groupId);
|
||||||
|
String[] allowedDomains = new String[1];
|
||||||
|
if (isAllowedAllDomains) {
|
||||||
|
allowedDomains[0] = ApiApplicationConstants.ALLOWED_DOMAINS;
|
||||||
|
} else {
|
||||||
|
allowedDomains[0] = APIManagerUtil.getTenantDomain();
|
||||||
|
}
|
||||||
|
apiConsumer.mapExistingOAuthClient(jsonString, username, clientId, applicationName,
|
||||||
|
ApiApplicationConstants.DEFAULT_TOKEN_TYPE, allowedDomains);
|
||||||
|
}
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException(
|
||||||
|
"Failed registering the OAuth app [ clientId " + clientId + " ] with api manager application", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAPIApplication(String applicationName, String username) throws APIManagerException {
|
||||||
|
try {
|
||||||
|
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
|
||||||
|
if (apiConsumer != null) {
|
||||||
|
String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain());
|
||||||
|
Application[] applications = apiConsumer.getApplications(new Subscriber(username), groupId);
|
||||||
|
for (Application application : applications) {
|
||||||
|
if (application.getName().equals(applicationName)) {
|
||||||
|
apiConsumer.removeApplication(application);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException(
|
||||||
|
"Failed to remove the application [ application name " + applicationName + " ]", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ApiApplicationKey generateAndRetrieveApplicationKeys(String apiApplicationName, String tags[],
|
||||||
|
String keyType, String username,
|
||||||
|
boolean isAllowedAllDomains)
|
||||||
|
throws APIManagerException {
|
||||||
|
try {
|
||||||
|
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
|
||||||
|
String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain());
|
||||||
|
int applicationId = createApplicationAndSubscribeToAPIs(apiApplicationName, tags, username);
|
||||||
|
Application[] applications = apiConsumer.getApplications(apiConsumer.getSubscriber(username), groupId);
|
||||||
|
Application application = null;
|
||||||
|
for (Application app : applications) {
|
||||||
|
if (app.getId() == applicationId) {
|
||||||
|
application = app;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (application == null) {
|
||||||
|
throw new APIManagerException(
|
||||||
|
"Api application creation failed for " + apiApplicationName + " to the user " + username);
|
||||||
|
}
|
||||||
|
|
||||||
|
APIKey retrievedApiApplicationKey = null;
|
||||||
|
for (APIKey apiKey : application.getKeys()) {
|
||||||
|
String applicationKeyType = apiKey.getType();
|
||||||
|
if (applicationKeyType != null && applicationKeyType.equals(keyType)) {
|
||||||
|
retrievedApiApplicationKey = apiKey;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (retrievedApiApplicationKey != null) {
|
||||||
|
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
|
||||||
|
apiApplicationKey.setConsumerKey(retrievedApiApplicationKey.getConsumerKey());
|
||||||
|
apiApplicationKey.setConsumerSecret(retrievedApiApplicationKey.getConsumerSecret());
|
||||||
|
return apiApplicationKey;
|
||||||
|
}
|
||||||
|
String[] allowedDomains = new String[1];
|
||||||
|
if (isAllowedAllDomains) {
|
||||||
|
allowedDomains[0] = ApiApplicationConstants.ALLOWED_DOMAINS;
|
||||||
|
} else {
|
||||||
|
allowedDomains[0] = APIManagerUtil.getTenantDomain();
|
||||||
|
}
|
||||||
|
String validityTime = "3600";
|
||||||
|
String ownerJsonString = "{\"username\":\"" + username + "\"}";
|
||||||
|
Map<String, Object> keyDetails = apiConsumer.requestApprovalForApplicationRegistration(username,
|
||||||
|
apiApplicationName,
|
||||||
|
keyType, "",
|
||||||
|
allowedDomains,
|
||||||
|
validityTime,
|
||||||
|
"null",
|
||||||
|
groupId,
|
||||||
|
ownerJsonString);
|
||||||
|
ApiApplicationKey apiApplicationKey = new ApiApplicationKey();
|
||||||
|
apiApplicationKey.setConsumerKey((String) keyDetails.get(APIConstants.FrontEndParameterNames
|
||||||
|
.CONSUMER_KEY));
|
||||||
|
apiApplicationKey.setConsumerSecret((String) keyDetails.get(
|
||||||
|
APIConstants.FrontEndParameterNames.CONSUMER_SECRET));
|
||||||
|
return apiApplicationKey;
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("Failed to register a api application : " + apiApplicationName, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int createApplication(APIConsumer apiConsumer, String applicationName, String username, String groupId)
|
||||||
|
throws APIManagerException {
|
||||||
|
try {
|
||||||
|
if (apiConsumer != null) {
|
||||||
|
if (apiConsumer.getSubscriber(username) == null) {
|
||||||
|
String tenantDomain = MultitenantUtils.getTenantDomain(username);
|
||||||
|
addSubscriber(username, "", groupId, APIManagerUtil.getTenantId(tenantDomain));
|
||||||
|
}
|
||||||
|
Application application = apiConsumer.getApplicationsByName(username, applicationName, groupId);
|
||||||
|
if (application == null) {
|
||||||
|
Subscriber subscriber = apiConsumer.getSubscriber(username);
|
||||||
|
application = new Application(applicationName, subscriber);
|
||||||
|
application.setTier(ApiApplicationConstants.DEFAULT_TIER);
|
||||||
|
application.setGroupId(groupId);
|
||||||
|
return apiConsumer.addApplication(application, username);
|
||||||
|
} else {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Application [" + applicationName + "] already exists for Subscriber [" + username +
|
||||||
|
"]");
|
||||||
|
}
|
||||||
|
return application.getId();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new APIManagerException("Failed to retrieve the api consumer for username" + username);
|
||||||
|
}
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("Failed to create application [name:" + applicationName + " , username:"
|
||||||
|
+ username + ", " + "groupId:" + groupId, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addSubscription(APIConsumer apiConsumer, APIIdentifier apiId, int applicationId, String username)
|
||||||
|
throws APIManagerException {
|
||||||
|
try {
|
||||||
|
if (apiConsumer != null) {
|
||||||
|
APIIdentifier apiIdentifier = new APIIdentifier(apiId.getProviderName(), apiId.getApiName(),
|
||||||
|
apiId.getVersion());
|
||||||
|
apiIdentifier.setTier(ApiApplicationConstants.DEFAULT_TIER);
|
||||||
|
apiConsumer.addSubscription(apiIdentifier, username, applicationId);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Successfully created subscription for API : " + apiId + " from application : " +
|
||||||
|
applicationId);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new APIManagerException("API provider configured for the given API configuration is null. " +
|
||||||
|
"Thus, the API is not published");
|
||||||
|
}
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("Failed to create subscription for api name : " + apiId.getApiName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void addSubscriber(String subscriberName, String subscriberEmail, String groupId, int tenantId)
|
||||||
|
throws APIManagerException {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Creating subscriber with name " + subscriberName);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
APIConsumer consumer = APIManagerFactory.getInstance().getAPIConsumer(subscriberName);
|
||||||
|
if (consumer != null) {
|
||||||
|
Subscriber subscriber = new Subscriber(subscriberName);
|
||||||
|
subscriber.setSubscribedDate(new Date());
|
||||||
|
subscriber.setEmail(subscriberEmail);
|
||||||
|
subscriber.setTenantId(tenantId);
|
||||||
|
consumer.addSubscriber(subscriber, groupId);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Successfully created subscriber with name : " + subscriberName + " with groupID : " +
|
||||||
|
groupId);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new APIManagerException("API provider configured for the given API configuration is null. " +
|
||||||
|
"Thus, the API is not published");
|
||||||
|
}
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("API provider configured for the given API configuration is null. " +
|
||||||
|
"Thus, the API is not published", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method registers an api application and then subscribe the application to the api.
|
||||||
|
*
|
||||||
|
* @param apiApplicationName name of the application.
|
||||||
|
* @param tags are used subscribe the apis with the tag.
|
||||||
|
* @param username subscription is created for the user.
|
||||||
|
* @throws APIManagerException
|
||||||
|
*/
|
||||||
|
private int createApplicationAndSubscribeToAPIs(String apiApplicationName, String tags[], String username)
|
||||||
|
throws APIManagerException {
|
||||||
|
try {
|
||||||
|
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
|
||||||
|
String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain());
|
||||||
|
int applicationId = createApplication(apiConsumer, apiApplicationName, username, groupId);
|
||||||
|
Subscriber subscriber = apiConsumer.getSubscriber(username);
|
||||||
|
Set<API> userVisibleAPIs = null;
|
||||||
|
for (String tag : tags) {
|
||||||
|
Set<API> tagAPIs = apiConsumer.getAPIsWithTag(tag);
|
||||||
|
if (userVisibleAPIs == null) {
|
||||||
|
userVisibleAPIs = tagAPIs;
|
||||||
|
} else {
|
||||||
|
userVisibleAPIs.addAll(tagAPIs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (userVisibleAPIs != null) {
|
||||||
|
Set<SubscribedAPI> subscribedAPIs = apiConsumer.getSubscribedAPIs(subscriber, apiApplicationName,
|
||||||
|
groupId);
|
||||||
|
for (API userVisbleAPI : userVisibleAPIs) {
|
||||||
|
APIIdentifier apiIdentifier = userVisbleAPI.getId();
|
||||||
|
boolean isSubscribed = false;
|
||||||
|
if (subscribedAPIs != null) {
|
||||||
|
for (SubscribedAPI subscribedAPI : subscribedAPIs) {
|
||||||
|
if (subscribedAPI.getApiId().equals(apiIdentifier)) {
|
||||||
|
isSubscribed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isSubscribed) {
|
||||||
|
addSubscription(apiConsumer, apiIdentifier, applicationId, username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return applicationId;
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("Failed to fetch device apis information for the user " + username, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method registers an api application and then subscribe the application to the api.
|
||||||
|
*
|
||||||
|
* @param username subscription is created for the user.
|
||||||
|
* @throws APIManagerException
|
||||||
|
*/
|
||||||
|
private int createApplicationAndSubscribeToAllAPIs(String apiApplicationName, String username)
|
||||||
|
throws APIManagerException {
|
||||||
|
try {
|
||||||
|
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
|
||||||
|
String groupId = getLoggedInUserGroupId(username, APIManagerUtil.getTenantDomain());
|
||||||
|
int applicationId = createApplication(apiConsumer, apiApplicationName, username, groupId);
|
||||||
|
String tenantDomain = MultitenantUtils.getTenantDomain(username);
|
||||||
|
Set<API> userVisibleAPIs = apiConsumer.getAllPublishedAPIs(tenantDomain);
|
||||||
|
Subscriber subscriber = apiConsumer.getSubscriber(username);
|
||||||
|
Set<SubscribedAPI> subscribedAPIs = apiConsumer.getSubscribedAPIs(subscriber);
|
||||||
|
for (API visibleApi : userVisibleAPIs) {
|
||||||
|
APIIdentifier apiIdentifier = visibleApi.getId();
|
||||||
|
boolean isSubscribed = false;
|
||||||
|
for (SubscribedAPI subscribedAPI : subscribedAPIs) {
|
||||||
|
if (subscribedAPI.getApiId().equals(apiIdentifier)) {
|
||||||
|
isSubscribed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isSubscribed) {
|
||||||
|
addSubscription(apiConsumer, apiIdentifier, applicationId, username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return applicationId;
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("Failed to fetch device apis information for the user " + username, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getLoggedInUserGroupId(String username, String tenantDomain) throws APIManagerException {
|
||||||
|
JSONObject loginInfoJsonObj = new JSONObject();
|
||||||
|
try {
|
||||||
|
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
|
||||||
|
loginInfoJsonObj.put("user", username);
|
||||||
|
if (tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
|
||||||
|
loginInfoJsonObj.put("isSuperTenant", true);
|
||||||
|
} else {
|
||||||
|
loginInfoJsonObj.put("isSuperTenant", false);
|
||||||
|
}
|
||||||
|
String loginInfoString = loginInfoJsonObj.toString();
|
||||||
|
return apiConsumer.getGroupIds(loginInfoString);
|
||||||
|
} catch (APIManagementException e) {
|
||||||
|
throw new APIManagerException("Unable to get groupIds of user " + username, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.constants;
|
||||||
|
|
||||||
|
public class ApiApplicationConstants {
|
||||||
|
public static final String DEFAULT_TOKEN_TYPE = "PRODUCTION";
|
||||||
|
public static final String DEFAULT_TIER = "Unlimited";
|
||||||
|
public static final String ALLOWED_DOMAINS = "ALL";
|
||||||
|
public static final String OAUTH_CLIENT_ID = "client_id";
|
||||||
|
public static final String OAUTH_CLIENT_SECRET = "client_secret";
|
||||||
|
public static final String DEFAULT_VALIDITY_PERIOD = "3600";
|
||||||
|
public static final String JSONSTRING_USERNAME_TAG = "username";
|
||||||
|
public static final String JSONSTRING_KEY_TYPE_TAG = "key_type";
|
||||||
|
public static final String JSONSTRING_VALIDITY_PERIOD_TAG = "validityPeriod";
|
||||||
|
}
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.dto;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.constants.ApiApplicationConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This holds api application consumer key and secret.
|
||||||
|
*/
|
||||||
|
public class ApiApplicationKey {
|
||||||
|
private String consumerKey;
|
||||||
|
private String consumerSecret;
|
||||||
|
|
||||||
|
public String getConsumerKey() {
|
||||||
|
return this.consumerKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConsumerKey(String consumerKey) {
|
||||||
|
this.consumerKey = consumerKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConsumerSecret() {
|
||||||
|
return this.consumerSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConsumerSecret(String consumerSecret) {
|
||||||
|
this.consumerSecret = consumerSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
JSONObject obj = new JSONObject();
|
||||||
|
obj.put(ApiApplicationConstants.OAUTH_CLIENT_ID, this.getConsumerKey());
|
||||||
|
obj.put(ApiApplicationConstants.OAUTH_CLIENT_SECRET, this.getConsumerSecret());
|
||||||
|
return obj.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the exceptions related to API management.
|
||||||
|
*/
|
||||||
|
public class APIManagerException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -8933142342423122660L;
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIManagerException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIManagerException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIManagerException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIManagerException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIManagerException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.internal;
|
||||||
|
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||||
|
import org.wso2.carbon.user.core.service.RealmService;
|
||||||
|
import org.wso2.carbon.user.core.tenant.TenantManager;
|
||||||
|
|
||||||
|
public class APIApplicationManagerExtensionDataHolder {
|
||||||
|
private static APIApplicationManagerExtensionDataHolder thisInstance = new APIApplicationManagerExtensionDataHolder();
|
||||||
|
private APIManagementProviderService apiManagementProviderService;
|
||||||
|
private RealmService realmService;
|
||||||
|
private TenantManager tenantManager;
|
||||||
|
|
||||||
|
private APIApplicationManagerExtensionDataHolder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static APIApplicationManagerExtensionDataHolder getInstance() {
|
||||||
|
return thisInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIManagementProviderService getAPIManagementProviderService() {
|
||||||
|
return apiManagementProviderService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAPIManagementProviderService(
|
||||||
|
APIManagementProviderService apiManagementProviderService) {
|
||||||
|
this.apiManagementProviderService = apiManagementProviderService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RealmService getRealmService() {
|
||||||
|
if (realmService == null) {
|
||||||
|
throw new IllegalStateException("Realm service is not initialized properly");
|
||||||
|
}
|
||||||
|
return realmService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRealmService(RealmService realmService) {
|
||||||
|
this.realmService = realmService;
|
||||||
|
this.setTenantManager(realmService);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setTenantManager(RealmService realmService) {
|
||||||
|
if (realmService == null) {
|
||||||
|
throw new IllegalStateException("Realm service is not initialized properly");
|
||||||
|
}
|
||||||
|
this.tenantManager = realmService.getTenantManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TenantManager getTenantManager() {
|
||||||
|
return tenantManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.internal;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.osgi.framework.BundleContext;
|
||||||
|
import org.osgi.service.component.ComponentContext;;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderServiceImpl;
|
||||||
|
import org.wso2.carbon.user.core.service.RealmService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.apimgt.application.extension.internal.APIApplicationManagerExtensionServiceComponent"
|
||||||
|
* @scr.reference name="realm.service"
|
||||||
|
* immediate="true"
|
||||||
|
* interface="org.wso2.carbon.user.core.service.RealmService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setRealmService"
|
||||||
|
* unbind="unsetRealmService"
|
||||||
|
*/
|
||||||
|
public class APIApplicationManagerExtensionServiceComponent {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(APIApplicationManagerExtensionServiceComponent.class);
|
||||||
|
|
||||||
|
protected void activate(ComponentContext componentContext) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Initializing device extension bundle");
|
||||||
|
}
|
||||||
|
APIManagementProviderService apiManagementProviderService = new APIManagementProviderServiceImpl();
|
||||||
|
APIApplicationManagerExtensionDataHolder.getInstance().setAPIManagementProviderService(apiManagementProviderService);
|
||||||
|
BundleContext bundleContext = componentContext.getBundleContext();
|
||||||
|
bundleContext.registerService(APIManagementProviderService.class.getName(), apiManagementProviderService, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext componentContext) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets Realm Service.
|
||||||
|
*
|
||||||
|
* @param realmService An instance of RealmService
|
||||||
|
*/
|
||||||
|
protected void setRealmService(RealmService realmService) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Setting Realm Service");
|
||||||
|
}
|
||||||
|
APIApplicationManagerExtensionDataHolder.getInstance().setRealmService(realmService);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unsets Realm Service.
|
||||||
|
*
|
||||||
|
* @param realmService An instance of RealmService
|
||||||
|
*/
|
||||||
|
protected void unsetRealmService(RealmService realmService) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Unsetting Realm Service");
|
||||||
|
}
|
||||||
|
APIApplicationManagerExtensionDataHolder.getInstance().setRealmService(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.application.extension.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
||||||
|
import org.wso2.carbon.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder;
|
||||||
|
import org.wso2.carbon.base.MultitenantConstants;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.user.api.TenantManager;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
|
|
||||||
|
public final class APIManagerUtil {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(APIManagerUtil.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the tenant Id of the specific tenant Domain
|
||||||
|
*/
|
||||||
|
public static int getTenantId(String tenantDomain) throws APIManagerException {
|
||||||
|
try {
|
||||||
|
if (tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
|
||||||
|
return MultitenantConstants.SUPER_TENANT_ID;
|
||||||
|
}
|
||||||
|
TenantManager tenantManager = APIApplicationManagerExtensionDataHolder.getInstance().getTenantManager();
|
||||||
|
int tenantId = tenantManager.getTenantId(tenantDomain);
|
||||||
|
if (tenantId == -1) {
|
||||||
|
throw new APIManagerException("invalid tenant Domain :" + tenantDomain);
|
||||||
|
}
|
||||||
|
return tenantId;
|
||||||
|
} catch (UserStoreException e) {
|
||||||
|
throw new APIManagerException("invalid tenant Domain :" + tenantDomain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getTenantDomain() {
|
||||||
|
return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,16 +1,36 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>apimgt-extensions</artifactId>
|
<artifactId>apimgt-extensions</artifactId>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>org.wso2.carbon.apimgt.webapp.publisher</artifactId>
|
<artifactId>org.wso2.carbon.apimgt.webapp.publisher</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<packaging>bundle</packaging>
|
<packaging>bundle</packaging>
|
||||||
<name>WSO2 Carbon - API Management Webapp Publisher</name>
|
<name>WSO2 Carbon - API Management Webapp Publisher</name>
|
||||||
<description>WSO2 Carbon - API Management Webapp Publisher</description>
|
<description>WSO2 Carbon - API Management Webapp Publisher</description>
|
||||||
@ -61,6 +81,30 @@
|
|||||||
<groupId>org.apache.axis2.wso2</groupId>
|
<groupId>org.apache.axis2.wso2</groupId>
|
||||||
<artifactId>axis2</artifactId>
|
<artifactId>axis2</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.orbit.org.scannotation</groupId>
|
||||||
|
<artifactId>scannotation</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.ws.rs</groupId>
|
||||||
|
<artifactId>jsr311-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.apimgt.annotations</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.governance</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.governance.api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.governance</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.governance.lcm</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
@ -81,7 +125,8 @@
|
|||||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||||
<Bundle-Description>API Management Webapp Publisher</Bundle-Description>
|
<Bundle-Description>API Management Webapp Publisher</Bundle-Description>
|
||||||
<Private-Package>org.wso2.carbon.apimgt.webapp.publisher.internal</Private-Package>
|
<Private-Package>org.wso2.carbon.apimgt.webapp.publisher.internal
|
||||||
|
</Private-Package>
|
||||||
<Export-Package>
|
<Export-Package>
|
||||||
!org.wso2.carbon.apimgt.webapp.publisher.internal,
|
!org.wso2.carbon.apimgt.webapp.publisher.internal,
|
||||||
org.wso2.carbon.apimgt.webapp.publisher.*
|
org.wso2.carbon.apimgt.webapp.publisher.*
|
||||||
@ -91,7 +136,8 @@
|
|||||||
org.osgi.service.component,
|
org.osgi.service.component,
|
||||||
org.apache.commons.logging,
|
org.apache.commons.logging,
|
||||||
javax.servlet,
|
javax.servlet,
|
||||||
javax.xml.bind.annotation,
|
javax.xml.*,
|
||||||
|
com.google.gson.*,
|
||||||
org.apache.catalina,
|
org.apache.catalina,
|
||||||
org.apache.catalina.core,
|
org.apache.catalina.core,
|
||||||
org.wso2.carbon.apimgt.api,
|
org.wso2.carbon.apimgt.api,
|
||||||
@ -99,8 +145,16 @@
|
|||||||
org.wso2.carbon.apimgt.impl,
|
org.wso2.carbon.apimgt.impl,
|
||||||
org.apache.axis2.*;version="${axis2.osgi.version.range}",
|
org.apache.axis2.*;version="${axis2.osgi.version.range}",
|
||||||
org.wso2.carbon.core,
|
org.wso2.carbon.core,
|
||||||
org.wso2.carbon.utils
|
org.apache.commons.lang,
|
||||||
|
org.wso2.carbon.utils,
|
||||||
|
org.wso2.carbon.apimgt.annotations.*,
|
||||||
|
org.wso2.carbon.governance.lcm.util.*,
|
||||||
|
org.wso2.carbon.registry.core.*
|
||||||
</Import-Package>
|
</Import-Package>
|
||||||
|
<Embed-Dependency>
|
||||||
|
scribe;scope=compile|runtime;inline=false;
|
||||||
|
</Embed-Dependency>
|
||||||
|
<DynamicImport-Package>*</DynamicImport-Package>
|
||||||
</instructions>
|
</instructions>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|||||||
@ -20,11 +20,16 @@ package org.wso2.carbon.apimgt.webapp.publisher;
|
|||||||
|
|
||||||
import org.wso2.carbon.apimgt.api.APIManagementException;
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
||||||
import org.wso2.carbon.apimgt.api.APIProvider;
|
import org.wso2.carbon.apimgt.api.APIProvider;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.URITemplate;
|
||||||
|
import org.wso2.carbon.apimgt.impl.APIConstants;
|
||||||
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
||||||
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import javax.xml.bind.annotation.XmlTransient;
|
import javax.xml.bind.annotation.XmlTransient;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This bean class carries the properties used by some API that needs to be published within the underlying
|
* This bean class carries the properties used by some API that needs to be published within the underlying
|
||||||
@ -48,20 +53,35 @@ public class APIConfig {
|
|||||||
private String name;
|
private String name;
|
||||||
private String owner;
|
private String owner;
|
||||||
private String context;
|
private String context;
|
||||||
|
private String contextTemplate;
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
private String version;
|
private String version;
|
||||||
private String transports;
|
private String transports;
|
||||||
private APIProvider provider;
|
private APIProvider provider;
|
||||||
private boolean isSecured;
|
private boolean isSecured;
|
||||||
|
private Set<URITemplate> uriTemplates;
|
||||||
|
private List<String> tenants;
|
||||||
|
private boolean isSharedWithAllTenants;
|
||||||
|
private String tenantDomain;
|
||||||
|
private String[] tags;
|
||||||
|
|
||||||
public void init() throws APIManagementException {
|
public void init() throws APIManagementException {
|
||||||
try {
|
try {
|
||||||
this.provider = APIManagerFactory.getInstance().getAPIProvider(this.getOwner());
|
this.provider = APIManagerFactory.getInstance().getAPIProvider(owner);
|
||||||
} catch (APIManagementException e) {
|
} catch (APIManagementException e) {
|
||||||
throw new APIManagementException("Error occurred while initializing API provider", e);
|
throw new APIManagementException("Error occurred while initializing API provider", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "ContextTemplate", required = true)
|
||||||
|
public String getContextTemplate() {
|
||||||
|
return contextTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContextTemplate(String contextTemplate) {
|
||||||
|
this.contextTemplate = contextTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
@XmlTransient
|
@XmlTransient
|
||||||
public APIProvider getProvider() {
|
public APIProvider getProvider() {
|
||||||
return provider;
|
return provider;
|
||||||
@ -136,4 +156,43 @@ public class APIConfig {
|
|||||||
isSecured = secured;
|
isSecured = secured;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "UriTemplates", required = false)
|
||||||
|
public Set<URITemplate> getUriTemplates() {
|
||||||
|
return uriTemplates;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public void setUriTemplates(Set<URITemplate> uriTemplates) {
|
||||||
|
this.uriTemplates = uriTemplates;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "isSharedWithAllTenants", required = false)
|
||||||
|
public boolean isSharedWithAllTenants() {
|
||||||
|
return isSharedWithAllTenants;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public void setSharedWithAllTenants(boolean isSharedWithAllTenants) {
|
||||||
|
this.isSharedWithAllTenants = isSharedWithAllTenants;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "tenantDomain", required = false)
|
||||||
|
public String getTenantDomain() {
|
||||||
|
return tenantDomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public void setTenantDomain(String tenantDomain) {
|
||||||
|
this.tenantDomain = tenantDomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "tags", required = false)
|
||||||
|
public String[] getTags() {
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public void setTags(String[] tags) {
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,9 +19,9 @@
|
|||||||
package org.wso2.carbon.apimgt.webapp.publisher;
|
package org.wso2.carbon.apimgt.webapp.publisher;
|
||||||
|
|
||||||
import org.wso2.carbon.apimgt.api.APIManagementException;
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
||||||
|
import org.wso2.carbon.apimgt.api.FaultGatewaysException;
|
||||||
import org.wso2.carbon.apimgt.api.model.API;
|
import org.wso2.carbon.apimgt.api.model.API;
|
||||||
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,7 +39,7 @@ public interface APIPublisherService {
|
|||||||
* @param api An instance of the bean that passes metadata related to the API being published
|
* @param api An instance of the bean that passes metadata related to the API being published
|
||||||
* @throws APIManagementException Is thrown if some unexpected event occurs while publishing the API
|
* @throws APIManagementException Is thrown if some unexpected event occurs while publishing the API
|
||||||
*/
|
*/
|
||||||
void publishAPI(API api) throws APIManagementException;
|
void publishAPI(API api) throws APIManagementException, FaultGatewaysException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method removes an API that's already published within the underlying API-Management infrastructure.
|
* This method removes an API that's already published within the underlying API-Management infrastructure.
|
||||||
@ -55,6 +55,5 @@ public interface APIPublisherService {
|
|||||||
* @param apis A list of the beans that passes metadata related to the APIs being published
|
* @param apis A list of the beans that passes metadata related to the APIs being published
|
||||||
* @throws APIManagementException Is thrown if some unexpected event occurs while publishing the APIs
|
* @throws APIManagementException Is thrown if some unexpected event occurs while publishing the APIs
|
||||||
*/
|
*/
|
||||||
void publishAPIs(List<API> apis) throws APIManagementException;
|
void publishAPIs(List<API> apis) throws APIManagementException, FaultGatewaysException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.wso2.carbon.apimgt.webapp.publisher;
|
package org.wso2.carbon.apimgt.webapp.publisher;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.apimgt.api.APIManagementException;
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
||||||
@ -25,8 +26,17 @@ import org.wso2.carbon.apimgt.api.APIProvider;
|
|||||||
import org.wso2.carbon.apimgt.api.FaultGatewaysException;
|
import org.wso2.carbon.apimgt.api.FaultGatewaysException;
|
||||||
import org.wso2.carbon.apimgt.api.model.API;
|
import org.wso2.carbon.apimgt.api.model.API;
|
||||||
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.URITemplate;
|
||||||
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
||||||
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
||||||
|
import org.wso2.carbon.governance.lcm.util.CommonUtil;
|
||||||
|
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
||||||
|
|
||||||
|
import javax.xml.stream.XMLStreamException;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,31 +48,80 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|||||||
private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class);
|
private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void publishAPI(API api) throws APIManagementException {
|
public void publishAPI(API api) throws APIManagementException, FaultGatewaysException {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Publishing API '" + api.getId() + "'");
|
log.debug("Publishing API '" + api.getId() + "'");
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
String tenantDomain = MultitenantUtils.getTenantDomain(api.getApiOwner());
|
||||||
|
int tenantId =
|
||||||
|
APIPublisherDataHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
|
||||||
|
// Below code snippet is added load API Lifecycle in tenant mode, where in it does not load when tenant is loaded.
|
||||||
|
RegistryService registryService = APIPublisherDataHolder.getInstance().getRegistryService();
|
||||||
|
CommonUtil.addDefaultLifecyclesIfNotAvailable(registryService.getConfigSystemRegistry(tenantId),
|
||||||
|
CommonUtil.getRootSystemRegistry(tenantId));
|
||||||
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
|
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
|
||||||
|
MultitenantUtils.getTenantDomain(api.getApiOwner());
|
||||||
if (provider != null) {
|
if (provider != null) {
|
||||||
if (!provider.isAPIAvailable(api.getId())) {
|
if (!provider.isAPIAvailable(api.getId())) {
|
||||||
provider.addAPI(api);
|
provider.addAPI(api);
|
||||||
log.info("Successfully published API '" + api.getId().getApiName() + "' with context '" +
|
log.info("Successfully published API '" + api.getId().getApiName() + "' with context '" +
|
||||||
api.getContext() + "' and version '" + api.getId().getVersion() + "'");
|
api.getContext() + "' and version '" + api.getId().getVersion() + "'");
|
||||||
} else {
|
} else {
|
||||||
try {
|
|
||||||
provider.updateAPI(api);
|
provider.updateAPI(api);
|
||||||
log.info("An API already exists with the name '" + api.getId().getApiName() + "', context '" +
|
log.info("An API already exists with the name '" + api.getId().getApiName() + "', context '" +
|
||||||
api.getContext() + "' and version '" + api.getId().getVersion() +
|
api.getContext() + "' and version '" + api.getId().getVersion() +
|
||||||
"'. Thus, the API config is updated");
|
"'. Thus, the API config is updated");
|
||||||
} catch (FaultGatewaysException e) {
|
|
||||||
throw new APIManagementException("Error occurred while updating API " + api.getId().getApiName() +
|
|
||||||
"' with context '" + api.getContext() + "' and version '" + api.getId().getVersion() + "'");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
provider.saveSwagger20Definition(api.getId(), createSwaggerDefinition(api));
|
||||||
} else {
|
} else {
|
||||||
throw new APIManagementException("API provider configured for the given API configuration is null. " +
|
throw new APIManagementException("API provider configured for the given API configuration is null. " +
|
||||||
"Thus, the API is not published");
|
"Thus, the API is not published");
|
||||||
}
|
}
|
||||||
|
} catch (UserStoreException e) {
|
||||||
|
throw new APIManagementException("Failed to get the tenant id for the user " + api.getApiOwner(), e);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new APIManagementException("Failed to retrieve life cycle file ", e);
|
||||||
|
} catch (RegistryException e) {
|
||||||
|
throw new APIManagementException("Failed to access the registry ", e);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
throw new APIManagementException("Failed parsing the lifecycle xml.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createSwaggerDefinition(API api) {
|
||||||
|
//{"paths":{"/controller/*":{"get":{"responses":{"200":{}}}},"/manager/*":{"get":{"responses":{"200":{}}}}},
|
||||||
|
// "swagger":"2.0","info":{"title":"RaspberryPi","version":"1.0.0"}}
|
||||||
|
JsonObject swaggerDefinition = new JsonObject();
|
||||||
|
|
||||||
|
JsonObject paths = new JsonObject();
|
||||||
|
for (URITemplate uriTemplate : api.getUriTemplates()) {
|
||||||
|
JsonObject response = new JsonObject();
|
||||||
|
response.addProperty("200", "");
|
||||||
|
|
||||||
|
JsonObject responses = new JsonObject();
|
||||||
|
responses.add("responses", response);
|
||||||
|
|
||||||
|
JsonObject httpVerb = new JsonObject();
|
||||||
|
httpVerb.add(uriTemplate.getHTTPVerb().toLowerCase(), responses);
|
||||||
|
|
||||||
|
JsonObject path = new JsonObject();
|
||||||
|
path.add(uriTemplate.getUriTemplate(), httpVerb);
|
||||||
|
|
||||||
|
paths.add(uriTemplate.getUriTemplate(), httpVerb);
|
||||||
|
}
|
||||||
|
swaggerDefinition.add("paths", paths);
|
||||||
|
swaggerDefinition.addProperty("swagger", "2.0");
|
||||||
|
|
||||||
|
JsonObject info = new JsonObject();
|
||||||
|
info.addProperty("title", api.getId().getApiName());
|
||||||
|
info.addProperty("version", api.getId().getVersion());
|
||||||
|
swaggerDefinition.add("info", info);
|
||||||
|
|
||||||
|
return swaggerDefinition.toString();
|
||||||
|
//return "{\"paths\":{\"/controller/*\":{\"get\":{\"responses\":{\"200\":{}}}},
|
||||||
|
// \"/manager/*\":{\"get\":{\"responses\":{\"200\":{}}}}},\"swagger\":\"2.0\",
|
||||||
|
// \"info\":{\"title\":\"RaspberryPi\",\"version\":\"1.0.0\"}}";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -78,7 +137,7 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void publishAPIs(List<API> apis) throws APIManagementException {
|
public void publishAPIs(List<API> apis) throws APIManagementException, FaultGatewaysException {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("Publishing a batch of APIs");
|
log.debug("Publishing a batch of APIs");
|
||||||
}
|
}
|
||||||
@ -93,5 +152,4 @@ public class APIPublisherServiceImpl implements APIPublisherService {
|
|||||||
log.debug("End of publishing the batch of APIs");
|
log.debug("End of publishing the batch of APIs");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
*
|
*
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
@ -14,29 +14,30 @@
|
|||||||
* KIND, either express or implied. See the License for the
|
* KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.wso2.carbon.apimgt.webapp.publisher;
|
package org.wso2.carbon.apimgt.webapp.publisher;
|
||||||
|
|
||||||
import org.wso2.carbon.apimgt.api.APIManagementException;
|
import org.wso2.carbon.apimgt.api.APIManagementException;
|
||||||
import org.wso2.carbon.apimgt.api.APIProvider;
|
import org.wso2.carbon.apimgt.api.APIProvider;
|
||||||
import org.wso2.carbon.apimgt.api.model.API;
|
import org.wso2.carbon.apimgt.api.model.*;
|
||||||
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.apimgt.impl.APIConstants;
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
||||||
|
import org.wso2.carbon.base.MultitenantConstants;
|
||||||
|
import org.wso2.carbon.user.api.TenantManager;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
import org.wso2.carbon.utils.CarbonUtils;
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
import org.wso2.carbon.utils.ConfigurationContextService;
|
import org.wso2.carbon.utils.ConfigurationContextService;
|
||||||
import org.wso2.carbon.utils.NetworkUtils;
|
import org.wso2.carbon.utils.NetworkUtils;
|
||||||
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class APIPublisherUtil {
|
public class APIPublisherUtil {
|
||||||
|
|
||||||
|
private static final String DEFAULT_API_VERSION = "1.0.0";
|
||||||
|
public static final String API_VERSION_PARAM="{version}";
|
||||||
|
|
||||||
enum HTTPMethod {
|
enum HTTPMethod {
|
||||||
GET, POST, DELETE, PUT, OPTIONS
|
GET, POST, DELETE, PUT, OPTIONS
|
||||||
}
|
}
|
||||||
@ -54,21 +55,53 @@ public class APIPublisherUtil {
|
|||||||
|
|
||||||
public static API getAPI(APIConfig config) throws APIManagementException {
|
public static API getAPI(APIConfig config) throws APIManagementException {
|
||||||
APIProvider provider = config.getProvider();
|
APIProvider provider = config.getProvider();
|
||||||
APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion());
|
String apiVersion = config.getVersion();
|
||||||
|
APIIdentifier id = new APIIdentifier(replaceEmailDomain(config.getOwner()), config.getName(), apiVersion);
|
||||||
API api = new API(id);
|
API api = new API(id);
|
||||||
api.setApiOwner(config.getOwner());
|
api.setApiOwner(config.getOwner());
|
||||||
api.setContext(config.getContext());
|
String context = config.getContext();
|
||||||
|
context = context.startsWith("/") ? context : ("/" + context);
|
||||||
|
String providerDomain = config.getTenantDomain();
|
||||||
|
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(providerDomain)) {
|
||||||
|
//Create tenant aware context for API
|
||||||
|
context = "/t/" + providerDomain + context;
|
||||||
|
}
|
||||||
|
// This is to support the new Pluggable version strategy
|
||||||
|
// if the context does not contain any {version} segment, we use the default version strategy.
|
||||||
|
context = checkAndSetVersionParam(context);
|
||||||
|
api.setContextTemplate(context);
|
||||||
|
context = updateContextWithVersion(config.getVersion(), context);
|
||||||
|
api.setContext(context);
|
||||||
api.setUrl(config.getEndpoint());
|
api.setUrl(config.getEndpoint());
|
||||||
api.setUriTemplates(
|
|
||||||
getURITemplates(config.getEndpoint(), APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN));
|
|
||||||
api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);
|
|
||||||
api.addAvailableTiers(provider.getTiers());
|
api.addAvailableTiers(provider.getTiers());
|
||||||
api.setEndpointSecured(true);
|
api.setEndpointSecured(true);
|
||||||
api.setStatus(APIStatus.PUBLISHED);
|
api.setStatus(APIStatus.PUBLISHED);
|
||||||
api.setTransports(config.getTransports());
|
api.setTransports(config.getTransports());
|
||||||
api.setAsDefaultVersion(true);
|
api.setContextTemplate(config.getContextTemplate());
|
||||||
api.setAsPublishedDefaultVersion(true);
|
api.setUriTemplates(config.getUriTemplates());
|
||||||
|
Set<Tier> tiers = new HashSet<Tier>();
|
||||||
|
tiers.add(new Tier(APIConstants.UNLIMITED_TIER));
|
||||||
|
api.addAvailableTiers(tiers);
|
||||||
|
if (config.isSharedWithAllTenants()) {
|
||||||
|
api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS);
|
||||||
|
api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);
|
||||||
|
} else {
|
||||||
|
api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_CURRENT_TENANT);
|
||||||
|
api.setVisibility(APIConstants.API_PRIVATE_VISIBILITY);
|
||||||
|
}
|
||||||
|
api.setResponseCache(APIConstants.DISABLED);
|
||||||
|
|
||||||
|
String endpointConfig = "{\"production_endpoints\":{\"url\":\" " + config.getEndpoint() + "\",\"config\":null},\"endpoint_type\":\"http\"}";
|
||||||
|
api.setEndpointConfig(endpointConfig);
|
||||||
|
|
||||||
|
if ("".equals(id.getVersion()) || (DEFAULT_API_VERSION.equals(id.getVersion()))) {
|
||||||
|
api.setAsDefaultVersion(Boolean.TRUE);
|
||||||
|
api.setAsPublishedDefaultVersion(Boolean.TRUE);
|
||||||
|
}
|
||||||
|
if (config.getTags() != null && config.getTags().length > 0) {
|
||||||
|
Set<String> tags = new HashSet<>(Arrays.asList(config.getTags()));
|
||||||
|
api.addTags(tags);
|
||||||
|
}
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,4 +158,35 @@ public class APIPublisherUtil {
|
|||||||
return getServerBaseUrl() + context;
|
return getServerBaseUrl() + context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When an input is having '@',replace it with '-AT-' [This is required to persist API data in registry,as registry paths don't allow '@' sign.]
|
||||||
|
* @param input inputString
|
||||||
|
* @return String modifiedString
|
||||||
|
*/
|
||||||
|
private static String replaceEmailDomain(String input){
|
||||||
|
if(input!=null&& input.contains(APIConstants.EMAIL_DOMAIN_SEPARATOR) ){
|
||||||
|
input=input.replace(APIConstants.EMAIL_DOMAIN_SEPARATOR,APIConstants.EMAIL_DOMAIN_SEPARATOR_REPLACEMENT);
|
||||||
|
}
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String updateContextWithVersion(String version, String context) {
|
||||||
|
// This condition should not be true for any occasion but we keep it so that there are no loopholes in
|
||||||
|
// the flow.
|
||||||
|
context = context.replace(API_VERSION_PARAM, version);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String checkAndSetVersionParam(String context) {
|
||||||
|
// This is to support the new Pluggable version strategy
|
||||||
|
// if the context does not contain any {version} segment, we use the default version strategy.
|
||||||
|
if(!context.contains(API_VERSION_PARAM)){
|
||||||
|
if(!context.endsWith("/")){
|
||||||
|
context = context + "/";
|
||||||
|
}
|
||||||
|
context = context +API_VERSION_PARAM;
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.webapp.publisher.config;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "Resource")
|
||||||
|
public class APIResource{
|
||||||
|
|
||||||
|
private String AuthType;
|
||||||
|
private String HttpVerb;
|
||||||
|
private String Uri;
|
||||||
|
private String UriTemplate;
|
||||||
|
private String consumes;
|
||||||
|
private String produces;
|
||||||
|
|
||||||
|
public String getAuthType() {
|
||||||
|
return AuthType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "AuthType", required = true)
|
||||||
|
public void setAuthType(String authType) {
|
||||||
|
AuthType = authType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHttpVerb() {
|
||||||
|
return HttpVerb;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "HttpVerb", required = true)
|
||||||
|
public void setHttpVerb(String httpVerb) {
|
||||||
|
HttpVerb = httpVerb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUri() {
|
||||||
|
return Uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Uri", required = true)
|
||||||
|
public void setUri(String uri) {
|
||||||
|
Uri = uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUriTemplate() {
|
||||||
|
return UriTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "UriTemplate", required = true)
|
||||||
|
public void setUriTemplate(String uriTemplate) {
|
||||||
|
UriTemplate = uriTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConsumes() {
|
||||||
|
return consumes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Consumes", required = true)
|
||||||
|
public void setConsumes(String consumes) {
|
||||||
|
this.consumes = consumes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProduces() {
|
||||||
|
return produces;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Produces", required = true)
|
||||||
|
public void setProduces(String produces) {
|
||||||
|
this.produces = produces;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.webapp.publisher.config;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "ResourceConfiguration")
|
||||||
|
public class APIResourceConfiguration {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String context;
|
||||||
|
private String version;
|
||||||
|
private List<APIResource> resources;
|
||||||
|
private String[] tags;
|
||||||
|
|
||||||
|
public List<APIResource> getResources() {
|
||||||
|
return resources;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Resources", required = true)
|
||||||
|
public void setResources(List<APIResource> resources) {
|
||||||
|
this.resources = resources;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContext() {
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Context", required = true)
|
||||||
|
public void setContext(String context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Name")
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Version")
|
||||||
|
public void setVersion(String version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getTags() {
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Tags")
|
||||||
|
public void setTags(String[] tags) {
|
||||||
|
this.tags = tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
package org.wso2.carbon.apimgt.webapp.publisher.config;
|
||||||
|
|
||||||
|
public class APIResourceManagementException extends Exception{
|
||||||
|
private static final long serialVersionUID = -3151279311929070297L;
|
||||||
|
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIResourceManagementException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIResourceManagementException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIResourceManagementException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIResourceManagementException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIResourceManagementException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.webapp.publisher.config;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will add, update custom permissions defined in resources.xml in webapps.
|
||||||
|
*/
|
||||||
|
public class APIResourceManager {
|
||||||
|
|
||||||
|
private static APIResourceManager resourceManager;
|
||||||
|
private List<APIResource> resourceList;
|
||||||
|
|
||||||
|
private APIResourceManager(){};
|
||||||
|
|
||||||
|
public static APIResourceManager getInstance() {
|
||||||
|
if (resourceManager == null) {
|
||||||
|
synchronized (APIResourceManager.class) {
|
||||||
|
if (resourceManager == null) {
|
||||||
|
resourceManager = new APIResourceManager();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resourceManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initializeResources(InputStream resourceStream) throws APIResourceManagementException {
|
||||||
|
try {
|
||||||
|
if(resourceStream != null){
|
||||||
|
/* Un-marshaling Device Management configuration */
|
||||||
|
JAXBContext cdmContext = JAXBContext.newInstance(APIResourceConfiguration.class);
|
||||||
|
Unmarshaller unmarshaller = cdmContext.createUnmarshaller();
|
||||||
|
APIResourceConfiguration resourcesConfiguration = (APIResourceConfiguration)
|
||||||
|
unmarshaller.unmarshal(resourceStream);
|
||||||
|
if((resourcesConfiguration != null) && (resourcesConfiguration.getResources() != null)){
|
||||||
|
this.resourceList = resourcesConfiguration.getResources();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (JAXBException e) {
|
||||||
|
throw new APIResourceManagementException("Error occurred while initializing Data Source config", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<APIResource> getAPIResources(){
|
||||||
|
return resourceList;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,13 +18,20 @@
|
|||||||
*/
|
*/
|
||||||
package org.wso2.carbon.apimgt.webapp.publisher.internal;
|
package org.wso2.carbon.apimgt.webapp.publisher.internal;
|
||||||
|
|
||||||
|
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService;
|
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.user.core.service.RealmService;
|
||||||
|
import org.wso2.carbon.user.core.tenant.TenantManager;
|
||||||
import org.wso2.carbon.utils.ConfigurationContextService;
|
import org.wso2.carbon.utils.ConfigurationContextService;
|
||||||
|
|
||||||
public class APIPublisherDataHolder {
|
public class APIPublisherDataHolder {
|
||||||
|
|
||||||
private APIPublisherService apiPublisherService;
|
private APIPublisherService apiPublisherService;
|
||||||
private ConfigurationContextService configurationContextService;
|
private ConfigurationContextService configurationContextService;
|
||||||
|
private RealmService realmService;
|
||||||
|
private TenantManager tenantManager;
|
||||||
|
private RegistryService registryService;
|
||||||
|
|
||||||
private static APIPublisherDataHolder thisInstance = new APIPublisherDataHolder();
|
private static APIPublisherDataHolder thisInstance = new APIPublisherDataHolder();
|
||||||
|
|
||||||
@ -57,4 +64,34 @@ public class APIPublisherDataHolder {
|
|||||||
return configurationContextService;
|
return configurationContextService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RealmService getRealmService() {
|
||||||
|
if (realmService == null) {
|
||||||
|
throw new IllegalStateException("Realm service is not initialized properly");
|
||||||
|
}
|
||||||
|
return realmService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRealmService(RealmService realmService) {
|
||||||
|
this.realmService = realmService;
|
||||||
|
this.setTenantManager(realmService);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setTenantManager(RealmService realmService) {
|
||||||
|
if (realmService == null) {
|
||||||
|
throw new IllegalStateException("Realm service is not initialized properly");
|
||||||
|
}
|
||||||
|
this.tenantManager = realmService.getTenantManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TenantManager getTenantManager() {
|
||||||
|
return tenantManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RegistryService getRegistryService() {
|
||||||
|
return registryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistryService(RegistryService registryService) {
|
||||||
|
this.registryService = registryService;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,8 @@ import org.osgi.service.component.ComponentContext;
|
|||||||
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
|
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService;
|
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService;
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherServiceImpl;
|
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherServiceImpl;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.user.core.service.RealmService;
|
||||||
import org.wso2.carbon.utils.ConfigurationContextService;
|
import org.wso2.carbon.utils.ConfigurationContextService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,6 +37,18 @@ import org.wso2.carbon.utils.ConfigurationContextService;
|
|||||||
* policy="dynamic"
|
* policy="dynamic"
|
||||||
* bind="setConfigurationContextService"
|
* bind="setConfigurationContextService"
|
||||||
* unbind="unsetConfigurationContextService"
|
* unbind="unsetConfigurationContextService"
|
||||||
|
* @scr.reference name="user.realmservice.default"
|
||||||
|
* interface="org.wso2.carbon.user.core.service.RealmService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setRealmService"
|
||||||
|
* unbind="unsetRealmService"
|
||||||
|
* @scr.reference name="registry.service"
|
||||||
|
* interface="org.wso2.carbon.registry.core.service.RegistryService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setRegistryService"
|
||||||
|
* unbind="unsetRegistryService"
|
||||||
*/
|
*/
|
||||||
public class APIPublisherServiceComponent {
|
public class APIPublisherServiceComponent {
|
||||||
|
|
||||||
@ -95,4 +109,28 @@ public class APIPublisherServiceComponent {
|
|||||||
APIPublisherDataHolder.getInstance().setConfigurationContextService(null);
|
APIPublisherDataHolder.getInstance().setConfigurationContextService(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setRealmService(RealmService realmService) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Setting Realm Service");
|
||||||
|
}
|
||||||
|
APIPublisherDataHolder.getInstance().setRealmService(realmService);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetRealmService(RealmService realmService) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Unsetting Realm Service");
|
||||||
|
}
|
||||||
|
APIPublisherDataHolder.getInstance().setRealmService(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setRegistryService(RegistryService registryService) {
|
||||||
|
if (registryService != null && log.isDebugEnabled()) {
|
||||||
|
log.debug("Registry service initialized");
|
||||||
|
}
|
||||||
|
APIPublisherDataHolder.getInstance().setRegistryService(registryService);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetRegistryService(RegistryService registryService) {
|
||||||
|
APIPublisherDataHolder.getInstance().setRegistryService(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,13 +24,18 @@ import org.apache.catalina.LifecycleListener;
|
|||||||
import org.apache.catalina.core.StandardContext;
|
import org.apache.catalina.core.StandardContext;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.apimgt.api.model.API;
|
import org.wso2.carbon.apimgt.api.model.*;
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.APIConfig;
|
import org.wso2.carbon.apimgt.webapp.publisher.*;
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService;
|
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResource;
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherUtil;
|
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration;
|
||||||
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
|
||||||
|
import org.wso2.carbon.apimgt.webapp.publisher.lifecycle.util.AnnotationUtil;
|
||||||
|
import org.wso2.carbon.base.MultitenantConstants;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class APIPublisherLifecycleListener implements LifecycleListener {
|
public class APIPublisherLifecycleListener implements LifecycleListener {
|
||||||
@ -45,7 +50,10 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
|
|||||||
private static final String PARAM_MANAGED_API_OWNER = "managed-api-owner";
|
private static final String PARAM_MANAGED_API_OWNER = "managed-api-owner";
|
||||||
private static final String PARAM_MANAGED_API_TRANSPORTS = "managed-api-transports";
|
private static final String PARAM_MANAGED_API_TRANSPORTS = "managed-api-transports";
|
||||||
private static final String PARAM_MANAGED_API_IS_SECURED = "managed-api-isSecured";
|
private static final String PARAM_MANAGED_API_IS_SECURED = "managed-api-isSecured";
|
||||||
|
private static final String PARAM_MANAGED_API_APPLICATION = "managed-api-application";
|
||||||
|
private static final String PARAM_MANAGED_API_CONTEXT_TEMPLATE = "managed-api-context-template";
|
||||||
|
private static final String PARAM_SHARED_WITH_ALL_TENANTS = "isSharedWithAllTenants";
|
||||||
|
private static final String PARAM_PROVIDER_TENANT_DOMAIN = "providerTenantDomain";
|
||||||
private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class);
|
private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -53,65 +61,112 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
|
|||||||
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
|
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
|
||||||
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
|
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
|
||||||
ServletContext servletContext = context.getServletContext();
|
ServletContext servletContext = context.getServletContext();
|
||||||
|
|
||||||
|
|
||||||
String param = servletContext.getInitParameter(PARAM_MANAGED_API_ENABLED);
|
String param = servletContext.getInitParameter(PARAM_MANAGED_API_ENABLED);
|
||||||
boolean isManagedApi = (param != null && !param.isEmpty()) && Boolean.parseBoolean(param);
|
boolean isManagedApi = (param != null && !param.isEmpty()) && Boolean.parseBoolean(param);
|
||||||
|
|
||||||
if (isManagedApi) {
|
if (isManagedApi) {
|
||||||
APIConfig apiConfig = this.buildApiConfig(servletContext);
|
|
||||||
try {
|
try {
|
||||||
|
AnnotationUtil annotationUtil = new AnnotationUtil(context);
|
||||||
|
Set<String> annotatedAPIClasses = annotationUtil.
|
||||||
|
scanStandardContext(org.wso2.carbon.apimgt.annotations.api.API.class.getName());
|
||||||
|
List<APIResourceConfiguration> apiDefinitions = annotationUtil.extractAPIInfo(annotatedAPIClasses);
|
||||||
|
|
||||||
|
for (APIResourceConfiguration apiDefinition : apiDefinitions) {
|
||||||
|
APIConfig apiConfig = this.buildApiConfig(servletContext, apiDefinition);
|
||||||
|
try {
|
||||||
|
int tenantId = APIPublisherDataHolder.getInstance().getTenantManager().getTenantId(apiConfig.getTenantDomain());
|
||||||
|
boolean isTenantActive = APIPublisherDataHolder.getInstance().getTenantManager().isTenantActive(tenantId);
|
||||||
|
if (isTenantActive) {
|
||||||
apiConfig.init();
|
apiConfig.init();
|
||||||
API api = APIPublisherUtil.getAPI(apiConfig);
|
API api = APIPublisherUtil.getAPI(apiConfig);
|
||||||
APIPublisherService apiPublisherService =
|
APIPublisherService apiPublisherService =
|
||||||
APIPublisherDataHolder.getInstance().getApiPublisherService();
|
APIPublisherDataHolder.getInstance().getApiPublisherService();
|
||||||
if (apiPublisherService == null) {
|
if (apiPublisherService == null) {
|
||||||
throw new IllegalStateException("API Publisher service is not initialized properly");
|
throw new IllegalStateException(
|
||||||
|
"API Publisher service is not initialized properly");
|
||||||
}
|
}
|
||||||
apiPublisherService.publishAPI(api);
|
apiPublisherService.publishAPI(api);
|
||||||
|
} else {
|
||||||
|
log.error("No tenant [" + apiConfig.getTenantDomain() + "] found when publishing the webapp");
|
||||||
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
/* Throwable is caught as none of the RuntimeExceptions that can potentially occur at this point
|
log.error("Error occurred while publishing API '" + apiConfig.getName() +
|
||||||
does not seem to be logged anywhere else within the framework */
|
"' with the context '" + apiConfig.getContext() +
|
||||||
log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" +
|
"' and version '" + apiConfig.getVersion() + "'", e);
|
||||||
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'", e);
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error enconterd while discovering annotated classes", e);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
log.error("Error while scanning class for annotations", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private APIConfig buildApiConfig(ServletContext servletContext) {
|
private List<APIResourceConfiguration> mergeAPIDefinitions(List<APIResourceConfiguration> inputList) {
|
||||||
|
//TODO : Need to implemented, to merge API Definitions in cases where implementation of an API Lies in two
|
||||||
|
// classes
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the API Configuration to be passed to APIM, from a given list of URL templates
|
||||||
|
*
|
||||||
|
* @param servletContext
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private APIConfig buildApiConfig(ServletContext servletContext, APIResourceConfiguration apidef) {
|
||||||
APIConfig apiConfig = new APIConfig();
|
APIConfig apiConfig = new APIConfig();
|
||||||
|
|
||||||
String name = servletContext.getInitParameter(PARAM_MANAGED_API_NAME);
|
String name = apidef.getName();
|
||||||
if (name == null || name.isEmpty()) {
|
if (name == null || name.isEmpty()) {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("'managed-api-name' attribute is not configured. Therefore, using the default, " +
|
log.debug("API Name not set in @API Annotation");
|
||||||
"which is the name of the web application");
|
|
||||||
}
|
}
|
||||||
name = servletContext.getServletContextName();
|
name = servletContext.getServletContextName();
|
||||||
}
|
}
|
||||||
apiConfig.setName(name);
|
apiConfig.setName(name);
|
||||||
|
|
||||||
String version = servletContext.getInitParameter(PARAM_MANAGED_API_VERSION);
|
String version = apidef.getVersion();
|
||||||
if (version == null || version.isEmpty()) {
|
if (version == null || version.isEmpty()) {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("'managed-api-version' attribute is not configured. Therefore, using the " +
|
log.debug("'API Version not set in @API Annotation'");
|
||||||
"default, which is '1.0.0'");
|
|
||||||
}
|
}
|
||||||
version = API_CONFIG_DEFAULT_VERSION;
|
version = API_CONFIG_DEFAULT_VERSION;
|
||||||
}
|
}
|
||||||
apiConfig.setVersion(version);
|
apiConfig.setVersion(version);
|
||||||
|
|
||||||
String context = servletContext.getInitParameter(PARAM_MANAGED_API_CONTEXT);
|
|
||||||
|
String context = apidef.getContext();
|
||||||
if (context == null || context.isEmpty()) {
|
if (context == null || context.isEmpty()) {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("'managed-api-context' attribute is not configured. Therefore, using the default, " +
|
log.debug("'API Context not set in @API Annotation'");
|
||||||
"which is the original context assigned to the web application");
|
|
||||||
}
|
}
|
||||||
context = servletContext.getContextPath();
|
context = servletContext.getContextPath();
|
||||||
}
|
}
|
||||||
apiConfig.setContext(context);
|
apiConfig.setContext(context);
|
||||||
|
|
||||||
|
String[] tags = apidef.getTags();
|
||||||
|
if (tags == null || tags.length == 0) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("'API tag not set in @API Annotation'");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
apiConfig.setTags(tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
String contextTemplate = servletContext.getInitParameter(PARAM_MANAGED_API_CONTEXT_TEMPLATE);
|
||||||
|
if (contextTemplate == null || contextTemplate.isEmpty()) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("'managed-api-context-template' attribute is not configured. Therefore, using the default," +
|
||||||
|
" " +
|
||||||
|
"which is the original context template assigned to the web application");
|
||||||
|
}
|
||||||
|
contextTemplate = servletContext.getContextPath();
|
||||||
|
}
|
||||||
|
apiConfig.setContextTemplate(contextTemplate);
|
||||||
|
|
||||||
String endpoint = servletContext.getInitParameter(PARAM_MANAGED_API_ENDPOINT);
|
String endpoint = servletContext.getInitParameter(PARAM_MANAGED_API_ENDPOINT);
|
||||||
if (endpoint == null || endpoint.isEmpty()) {
|
if (endpoint == null || endpoint.isEmpty()) {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
@ -152,6 +207,25 @@ public class APIPublisherLifecycleListener implements LifecycleListener {
|
|||||||
}
|
}
|
||||||
apiConfig.setTransports(transports);
|
apiConfig.setTransports(transports);
|
||||||
|
|
||||||
|
String sharingValueParam = servletContext.getInitParameter(PARAM_SHARED_WITH_ALL_TENANTS);
|
||||||
|
boolean isSharedWithAllTenants = (sharingValueParam == null || (!sharingValueParam.isEmpty()) && Boolean.parseBoolean(sharingValueParam) );
|
||||||
|
apiConfig.setSharedWithAllTenants(isSharedWithAllTenants);
|
||||||
|
|
||||||
|
String tenantDomain = servletContext.getInitParameter(PARAM_PROVIDER_TENANT_DOMAIN);
|
||||||
|
tenantDomain = (tenantDomain!= null && !tenantDomain.isEmpty()) ? tenantDomain : MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
|
||||||
|
apiConfig.setTenantDomain(tenantDomain);
|
||||||
|
|
||||||
|
Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
|
||||||
|
for (APIResource apiResource : apidef.getResources()) {
|
||||||
|
URITemplate template = new URITemplate();
|
||||||
|
template.setAuthType(apiResource.getAuthType());
|
||||||
|
template.setHTTPVerb(apiResource.getHttpVerb());
|
||||||
|
template.setResourceURI(apiResource.getUri());
|
||||||
|
template.setUriTemplate(apiResource.getUriTemplate());
|
||||||
|
uriTemplates.add(template);
|
||||||
|
}
|
||||||
|
apiConfig.setUriTemplates(uriTemplates);
|
||||||
|
|
||||||
return apiConfig;
|
return apiConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,265 @@
|
|||||||
|
/*
|
||||||
|
* 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.apimgt.webapp.publisher.lifecycle.util;
|
||||||
|
|
||||||
|
import org.apache.catalina.core.StandardContext;
|
||||||
|
import org.apache.commons.lang.ArrayUtils;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.scannotation.AnnotationDB;
|
||||||
|
import org.scannotation.WarUrlFinder;
|
||||||
|
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResource;
|
||||||
|
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.security.AccessController;
|
||||||
|
import java.security.PrivilegedAction;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class AnnotationUtil {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(AnnotationUtil.class);
|
||||||
|
|
||||||
|
private static final String PACKAGE_ORG_APACHE = "org.apache";
|
||||||
|
private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus";
|
||||||
|
private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework";
|
||||||
|
private static final String AUTH_TYPE = "Any";
|
||||||
|
private static final String PROTOCOL_HTTP = "http";
|
||||||
|
private static final String SERVER_HOST = "carbon.local.ip";
|
||||||
|
private static final String HTTP_PORT = "httpPort";
|
||||||
|
public static final String DIR_WEB_INF_LIB = "/WEB-INF/lib";
|
||||||
|
public static final String STRING_ARR = "string_arr";
|
||||||
|
public static final String STRING = "string";
|
||||||
|
|
||||||
|
private StandardContext context;
|
||||||
|
private Method[] pathClazzMethods;
|
||||||
|
private Class<Path> pathClazz;
|
||||||
|
private ClassLoader classLoader;
|
||||||
|
private ServletContext servletContext;
|
||||||
|
|
||||||
|
|
||||||
|
public AnnotationUtil(final StandardContext context) {
|
||||||
|
this.context = context;
|
||||||
|
servletContext = context.getServletContext();
|
||||||
|
classLoader = servletContext.getClassLoader();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scan the context for classes with annotations
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public Set<String> scanStandardContext(String className) throws IOException {
|
||||||
|
AnnotationDB db = new AnnotationDB();
|
||||||
|
db.addIgnoredPackages(PACKAGE_ORG_APACHE);
|
||||||
|
db.addIgnoredPackages(PACKAGE_ORG_CODEHAUS);
|
||||||
|
db.addIgnoredPackages(PACKAGE_ORG_SPRINGFRAMEWORK);
|
||||||
|
|
||||||
|
URL[] libPath = WarUrlFinder.findWebInfLibClasspaths(servletContext);
|
||||||
|
URL classPath = WarUrlFinder.findWebInfClassesPath(servletContext);
|
||||||
|
URL[] urls = (URL[]) ArrayUtils.add(libPath, libPath.length, classPath);
|
||||||
|
|
||||||
|
db.scanArchives(urls);
|
||||||
|
|
||||||
|
//Returns a list of classes with given Annotation
|
||||||
|
return db.getAnnotationIndex().get(className);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method identifies the URL templates and context by reading the annotations of a class
|
||||||
|
* @param entityClasses
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public List<APIResourceConfiguration> extractAPIInfo(Set<String> entityClasses)
|
||||||
|
throws ClassNotFoundException {
|
||||||
|
|
||||||
|
List<APIResourceConfiguration> apiResourceConfigs = new ArrayList<APIResourceConfiguration>();
|
||||||
|
|
||||||
|
if (entityClasses != null && !entityClasses.isEmpty()) {
|
||||||
|
for (final String className : entityClasses) {
|
||||||
|
|
||||||
|
APIResourceConfiguration resource =
|
||||||
|
AccessController.doPrivileged(new PrivilegedAction<APIResourceConfiguration>() {
|
||||||
|
public APIResourceConfiguration run() {
|
||||||
|
Class<?> clazz = null;
|
||||||
|
APIResourceConfiguration apiResourceConfig = null;
|
||||||
|
try {
|
||||||
|
clazz = classLoader.loadClass(className);
|
||||||
|
Class<Path> apiClazz = (Class<Path>)
|
||||||
|
classLoader.loadClass(org.wso2.carbon.apimgt.annotations.api.API.class.getName());
|
||||||
|
Annotation apiAnno = clazz.getAnnotation(apiClazz);
|
||||||
|
|
||||||
|
List<APIResource> resourceList = null;
|
||||||
|
apiResourceConfig = new APIResourceConfiguration();
|
||||||
|
|
||||||
|
if (apiAnno != null) {
|
||||||
|
|
||||||
|
Method[] apiClazzMethods = apiClazz.getMethods();
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Application Context root = " + servletContext.getContextPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
for(int k=0;k<apiClazzMethods.length;k++){
|
||||||
|
switch (apiClazzMethods[k].getName()){
|
||||||
|
case "name" :
|
||||||
|
apiResourceConfig.setName(invokeMethod(apiClazzMethods[k], apiAnno, STRING));
|
||||||
|
break;
|
||||||
|
case "version" :
|
||||||
|
apiResourceConfig.setVersion(invokeMethod(apiClazzMethods[k], apiAnno, STRING));
|
||||||
|
break;
|
||||||
|
case "context" :
|
||||||
|
apiResourceConfig.setContext(invokeMethod(apiClazzMethods[k], apiAnno, STRING));
|
||||||
|
break;
|
||||||
|
case "tags" :
|
||||||
|
apiResourceConfig.setTags(invokeMethod(apiClazzMethods[k], apiAnno));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String rootContext = "";
|
||||||
|
pathClazz = (Class<Path>) classLoader.loadClass(Path.class.getName());
|
||||||
|
pathClazzMethods = pathClazz.getMethods();
|
||||||
|
|
||||||
|
Annotation rootContectAnno = clazz.getAnnotation(pathClazz);
|
||||||
|
if (rootContectAnno != null) {
|
||||||
|
rootContext = invokeMethod(pathClazzMethods[0], rootContectAnno, STRING);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("API Root Context = " + rootContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Method[] annotatedMethods = clazz.getDeclaredMethods();
|
||||||
|
resourceList = getApiResources(rootContext, annotatedMethods);
|
||||||
|
apiResourceConfig.setResources(resourceList);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
log.error("Error encountered while scanning for annotations", throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
log.error("Error when passing the api annotation for device type apis.");
|
||||||
|
}
|
||||||
|
return apiResourceConfig;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
apiResourceConfigs.add(resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return apiResourceConfigs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<APIResource> getApiResources(String rootContext, Method[] annotatedMethods) throws Throwable {
|
||||||
|
List<APIResource> resourceList;
|
||||||
|
resourceList = new ArrayList<APIResource>();
|
||||||
|
for (Method method : annotatedMethods) {
|
||||||
|
Annotation methodContextAnno = method.getAnnotation(pathClazz);
|
||||||
|
if (methodContextAnno != null) {
|
||||||
|
String subCtx = invokeMethod(pathClazzMethods[0], methodContextAnno, STRING);
|
||||||
|
APIResource resource = new APIResource();
|
||||||
|
resource.setUriTemplate(makeContextURLReady(subCtx));
|
||||||
|
|
||||||
|
String serverIP = System.getProperty(SERVER_HOST);
|
||||||
|
String httpServerPort = System.getProperty(HTTP_PORT);
|
||||||
|
|
||||||
|
resource.setUri(PROTOCOL_HTTP + "://" + serverIP + ":" + httpServerPort + makeContextURLReady(rootContext) + makeContextURLReady(subCtx));
|
||||||
|
resource.setAuthType(AUTH_TYPE);
|
||||||
|
|
||||||
|
Annotation[] annotations = method.getDeclaredAnnotations();
|
||||||
|
for(int i=0; i<annotations.length; i++){
|
||||||
|
|
||||||
|
if(annotations[i].annotationType().getName().equals(GET.class.getName())){
|
||||||
|
resource.setHttpVerb(HttpMethod.GET);
|
||||||
|
}
|
||||||
|
if(annotations[i].annotationType().getName().equals(POST.class.getName())){
|
||||||
|
resource.setHttpVerb(HttpMethod.POST);
|
||||||
|
}
|
||||||
|
if(annotations[i].annotationType().getName().equals(OPTIONS.class.getName())){
|
||||||
|
resource.setHttpVerb(HttpMethod.OPTIONS);
|
||||||
|
}
|
||||||
|
if(annotations[i].annotationType().getName().equals(DELETE.class.getName())){
|
||||||
|
resource.setHttpVerb(HttpMethod.DELETE);
|
||||||
|
}
|
||||||
|
if(annotations[i].annotationType().getName().equals(PUT.class.getName())){
|
||||||
|
resource.setHttpVerb(HttpMethod.PUT);
|
||||||
|
}
|
||||||
|
if(annotations[i].annotationType().getName().equals(Consumes.class.getName())){
|
||||||
|
Class<Consumes> consumesClass = (Class<Consumes>) classLoader.loadClass(Consumes.class.getName());
|
||||||
|
Method[] consumesClassMethods = consumesClass.getMethods();
|
||||||
|
Annotation consumesAnno = method.getAnnotation(consumesClass);
|
||||||
|
resource.setConsumes(invokeMethod(consumesClassMethods[0], consumesAnno, STRING_ARR));
|
||||||
|
}
|
||||||
|
if(annotations[i].annotationType().getName().equals(Produces.class.getName())){
|
||||||
|
Class<Produces> producesClass = (Class<Produces>) classLoader.loadClass(Produces.class.getName());
|
||||||
|
Method[] producesClassMethods = producesClass.getMethods();
|
||||||
|
Annotation producesAnno = method.getAnnotation(producesClass);
|
||||||
|
resource.setProduces(invokeMethod(producesClassMethods[0], producesAnno, STRING_ARR));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resourceList.add(resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resourceList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String makeContextURLReady(String context){
|
||||||
|
if(context != null && !context.equalsIgnoreCase("")){
|
||||||
|
if(context.startsWith("/")){
|
||||||
|
return context;
|
||||||
|
}else{
|
||||||
|
return "/"+context;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When an annotation and method is passed, this method invokes that executes said method against the annotation
|
||||||
|
* @param method
|
||||||
|
* @param annotation
|
||||||
|
* @param returnType
|
||||||
|
* @return
|
||||||
|
* @throws Throwable
|
||||||
|
*/
|
||||||
|
private String invokeMethod(Method method, Annotation annotation, String returnType) throws Throwable {
|
||||||
|
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
|
||||||
|
switch (returnType){
|
||||||
|
case STRING:
|
||||||
|
return (String) methodHandler.invoke(annotation, method, null);
|
||||||
|
case STRING_ARR:
|
||||||
|
return ((String[])methodHandler.invoke(annotation, method, null))[0];
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When an annotation and method is passed, this method invokes that executes said method against the annotation
|
||||||
|
*/
|
||||||
|
private String[] invokeMethod(Method method, Annotation annotation) throws Throwable {
|
||||||
|
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
|
||||||
|
return ((String[])methodHandler.invoke(annotation, method, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!--
|
<!--
|
||||||
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
~
|
~
|
||||||
~ WSO2 Inc. licenses this file to you under the Apache License,
|
~ WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
~ Version 2.0 (the "License"); you may not use this file except
|
~ Version 2.0 (the "License"); you may not use this file except
|
||||||
@ -22,20 +22,22 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<artifactId>carbon-devicemgt</artifactId>
|
<artifactId>carbon-devicemgt</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
|
||||||
<artifactId>apimgt-extensions</artifactId>
|
<artifactId>apimgt-extensions</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<name>WSO2 Carbon - API Management Extensions Component</name>
|
<name>WSO2 Carbon - API Management Extensions Component</name>
|
||||||
<url>http://wso2.org</url>
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>org.wso2.carbon.apimgt.webapp.publisher</module>
|
<module>org.wso2.carbon.apimgt.webapp.publisher</module>
|
||||||
|
<module>org.wso2.carbon.apimgt.application.extension</module>
|
||||||
|
<module>org.wso2.carbon.apimgt.application.extension.api</module>
|
||||||
|
<module>org.wso2.carbon.apimgt.annotations</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -21,13 +21,13 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<artifactId>certificate-mgt</artifactId>
|
<artifactId>certificate-mgt</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>org.wso2.carbon.certificate.mgt.core</artifactId>
|
<artifactId>org.wso2.carbon.certificate.mgt.core</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<packaging>bundle</packaging>
|
<packaging>bundle</packaging>
|
||||||
<name>WSO2 Carbon - Certificate Management Core</name>
|
<name>WSO2 Carbon - Certificate Management Core</name>
|
||||||
<description>WSO2 Carbon - Certificate Management Core</description>
|
<description>WSO2 Carbon - Certificate Management Core</description>
|
||||||
@ -69,8 +69,10 @@
|
|||||||
org.bouncycastle.operator,
|
org.bouncycastle.operator,
|
||||||
org.bouncycastle.operator.jcajce,
|
org.bouncycastle.operator.jcajce,
|
||||||
org.bouncycastle.pkcs,
|
org.bouncycastle.pkcs,
|
||||||
|
org.bouncycastle.openssl,
|
||||||
org.bouncycastle.util,
|
org.bouncycastle.util,
|
||||||
org.jscep.message,
|
org.jscep.message,
|
||||||
|
org.wso2.carbon.context,
|
||||||
org.jscep.transaction,
|
org.jscep.transaction,
|
||||||
org.w3c.dom,
|
org.w3c.dom,
|
||||||
org.xml.sax,
|
org.xml.sax,
|
||||||
|
|||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.certificate.mgt.core.bean;
|
||||||
|
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
public class Certificate {
|
||||||
|
String serial;
|
||||||
|
X509Certificate certificate;
|
||||||
|
int tenantId;
|
||||||
|
String tenantDomain;
|
||||||
|
|
||||||
|
public int getTenantId() {
|
||||||
|
return tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantId(int tenantId) {
|
||||||
|
this.tenantId = tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSerial() {
|
||||||
|
return serial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSerial(String serial) {
|
||||||
|
this.serial = serial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public X509Certificate getCertificate() {
|
||||||
|
return certificate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCertificate(X509Certificate certificate) {
|
||||||
|
this.certificate = certificate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTenantDomain() {
|
||||||
|
return tenantDomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantDomain(String tenantDomain) {
|
||||||
|
this.tenantDomain = tenantDomain;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,7 +18,12 @@
|
|||||||
|
|
||||||
package org.wso2.carbon.certificate.mgt.core.dao;
|
package org.wso2.carbon.certificate.mgt.core.dao;
|
||||||
|
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents the key operations associated with persisting certificate related
|
* This class represents the key operations associated with persisting certificate related
|
||||||
@ -30,21 +35,43 @@ public interface CertificateDAO {
|
|||||||
* This can be used to store a certificate in the database, where it will be stored against the serial number
|
* This can be used to store a certificate in the database, where it will be stored against the serial number
|
||||||
* of the certificate.
|
* of the certificate.
|
||||||
*
|
*
|
||||||
* @param byteArrayInputStream Holds the certificate.
|
* @param certificate Holds the certificate and relevant details.
|
||||||
* @param serialNumber Serial number of the certificate.
|
|
||||||
* @throws CertificateManagementDAOException
|
* @throws CertificateManagementDAOException
|
||||||
*/
|
*/
|
||||||
void addCertificate(ByteArrayInputStream byteArrayInputStream, String serialNumber
|
void addCertificate(List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificate)
|
||||||
) throws CertificateManagementDAOException;
|
throws CertificateManagementDAOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Usage is to obtain a certificate stored in the database by providing the serial number.
|
* Usage is to obtain a certificate stored in the database by providing the common name.
|
||||||
*
|
*
|
||||||
* @param serialNumber Serial number of the certificate.
|
* @param serialNumber Serial number of the certificate.
|
||||||
* @return representation of the certificate.
|
* @return representation of the certificate.
|
||||||
* @throws CertificateManagementDAOException
|
* @throws CertificateManagementDAOException
|
||||||
*/
|
*/
|
||||||
byte[] retrieveCertificate(String serialNumber
|
org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse retrieveCertificate(String serialNumber
|
||||||
) throws CertificateManagementDAOException;
|
) throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the certificates in a paginated manner.
|
||||||
|
* @param request Request mentioning pagination details such as length and stating index.
|
||||||
|
* @return Pagination result with data and the count of results.
|
||||||
|
* @throws CertificateManagementDAOException
|
||||||
|
*/
|
||||||
|
PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the certificates.
|
||||||
|
* @return List of certificates
|
||||||
|
* @throws CertificateManagementDAOException
|
||||||
|
*/
|
||||||
|
public List<CertificateResponse> getAllCertificates() throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a certificate identified by a serial number()
|
||||||
|
* @param serialNumber serial number
|
||||||
|
* @return whether the certificate was removed or not.
|
||||||
|
*/
|
||||||
|
boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
public List<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,58 +18,243 @@
|
|||||||
|
|
||||||
package org.wso2.carbon.certificate.mgt.core.dao.impl;
|
package org.wso2.carbon.certificate.mgt.core.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.bean.Certificate;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateDAO;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateDAO;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOUtil;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.impl.CertificateGenerator;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.util.CertificateManagerUtil;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.util.Serializer;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.cert.CertificateEncodingException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class GenericCertificateDAOImpl implements CertificateDAO {
|
public class GenericCertificateDAOImpl implements CertificateDAO {
|
||||||
|
private static final Log log = LogFactory.getLog(GenericCertificateDAOImpl.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addCertificate(ByteArrayInputStream byteArrayInputStream, String serialNumber)
|
public void addCertificate(List<Certificate> certificates)
|
||||||
throws CertificateManagementDAOException {
|
throws CertificateManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
PreparedStatement stmt = null;
|
PreparedStatement stmt = null;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_CERTIFICATE (SERIAL_NUMBER, CERTIFICATE) VALUES (?,?)");
|
stmt = conn.prepareStatement(
|
||||||
|
"INSERT INTO DM_DEVICE_CERTIFICATE (SERIAL_NUMBER, CERTIFICATE, TENANT_ID) VALUES (?,?,?)");
|
||||||
|
for (Certificate certificate : certificates) {
|
||||||
|
String serialNumber = certificate.getSerial();
|
||||||
|
if (serialNumber == null || serialNumber.isEmpty()) {
|
||||||
|
serialNumber = String.valueOf(certificate.getCertificate().getSerialNumber());
|
||||||
|
}
|
||||||
|
byte[] bytes = Serializer.serialize(certificate.getCertificate());
|
||||||
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
||||||
|
|
||||||
stmt.setString(1, serialNumber);
|
stmt.setString(1, serialNumber);
|
||||||
stmt.setObject(2, byteArrayInputStream);
|
stmt.setObject(2, byteArrayInputStream);
|
||||||
stmt.execute();
|
stmt.setInt(3, certificate.getTenantId());
|
||||||
} catch (SQLException e) {
|
stmt.addBatch();
|
||||||
throw new CertificateManagementDAOException("Error occurred while saving certificate with serial " +
|
}
|
||||||
serialNumber, e);
|
stmt.executeBatch();
|
||||||
|
} catch (SQLException | IOException e) {
|
||||||
|
throw new CertificateManagementDAOException("Error occurred while saving certificates. "
|
||||||
|
, e);
|
||||||
} finally {
|
} finally {
|
||||||
CertificateManagementDAOUtil.cleanupResources(stmt, null);
|
CertificateManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] retrieveCertificate(String serialNumber)
|
public CertificateResponse retrieveCertificate(String serialNumber)
|
||||||
throws CertificateManagementDAOException {
|
throws CertificateManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
PreparedStatement stmt = null;
|
PreparedStatement stmt = null;
|
||||||
ResultSet resultSet = null;
|
ResultSet resultSet = null;
|
||||||
byte[] binaryStream = null;
|
CertificateResponse certificateResponse = null;
|
||||||
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
String query = "SELECT CERTIFICATE FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER = ?";
|
String query =
|
||||||
|
"SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER = ?" +
|
||||||
|
" AND TENANT_ID = ? ";
|
||||||
stmt = conn.prepareStatement(query);
|
stmt = conn.prepareStatement(query);
|
||||||
stmt.setString(1, serialNumber);
|
stmt.setString(1, serialNumber);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
resultSet = stmt.executeQuery();
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
binaryStream = resultSet.getBytes("CERTIFICATE");
|
certificateResponse = new CertificateResponse();
|
||||||
|
byte [] certificateBytes = resultSet.getBytes("CERTIFICATE");
|
||||||
|
certificateResponse.setCertificate(certificateBytes);
|
||||||
|
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
|
||||||
|
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
|
||||||
|
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new CertificateManagementDAOException(
|
String errorMsg =
|
||||||
"Unable to get the read the certificate with serial" + serialNumber, e);
|
"Unable to get the read the certificate with serial" + serialNumber;
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
return certificateResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<CertificateResponse> searchCertificate(String serialNumber)
|
||||||
|
throws CertificateManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
CertificateResponse certificateResponse = null;
|
||||||
|
List<CertificateResponse> certificates = new ArrayList<>();
|
||||||
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String query =
|
||||||
|
"SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER LIKE ?" +
|
||||||
|
" AND TENANT_ID = ? ";
|
||||||
|
stmt = conn.prepareStatement(query);
|
||||||
|
stmt.setString(1, "%" + serialNumber + "%");
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
certificateResponse = new CertificateResponse();
|
||||||
|
byte [] certificateBytes = resultSet.getBytes("CERTIFICATE");
|
||||||
|
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
|
||||||
|
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
|
||||||
|
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
|
||||||
|
certificates.add(certificateResponse);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg =
|
||||||
|
"Unable to get the read the certificate with serial" + serialNumber;
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
return certificates;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
CertificateResponse certificateResponse;
|
||||||
|
List<CertificateResponse> certificates = new ArrayList<>();
|
||||||
|
PaginationResult paginationResult;
|
||||||
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
try {
|
||||||
|
Connection conn = this.getConnection();
|
||||||
|
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? " +
|
||||||
|
"ORDER BY ID DESC LIMIT ?,?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setInt(2, request.getStartIndex());
|
||||||
|
stmt.setInt(3, request.getRowCount());
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
int resultCount = 0;
|
||||||
|
while (resultSet.next()) {
|
||||||
|
certificateResponse = new CertificateResponse();
|
||||||
|
byte [] certificateBytes = resultSet.getBytes("CERTIFICATE");
|
||||||
|
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
|
||||||
|
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
|
||||||
|
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
|
||||||
|
certificates.add(certificateResponse);
|
||||||
|
resultCount++;
|
||||||
|
}
|
||||||
|
paginationResult = new PaginationResult();
|
||||||
|
paginationResult.setData(certificates);
|
||||||
|
paginationResult.setRecordsTotal(resultCount);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg = "SQL error occurred while retrieving the certificates.";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
OperationManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
return paginationResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<CertificateResponse> getAllCertificates() throws CertificateManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
CertificateResponse certificateResponse;
|
||||||
|
List<CertificateResponse> certificates = new ArrayList<>();
|
||||||
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
try {
|
||||||
|
Connection conn = this.getConnection();
|
||||||
|
String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? " +
|
||||||
|
"ORDER BY ID DESC";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
certificateResponse = new CertificateResponse();
|
||||||
|
byte [] certificateBytes = resultSet.getBytes("CERTIFICATE");
|
||||||
|
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
|
||||||
|
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
|
||||||
|
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
|
||||||
|
certificates.add(certificateResponse);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg = "SQL error occurred while retrieving the certificates.";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
OperationManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
return certificates;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String query =
|
||||||
|
"DELETE FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER = ?" +
|
||||||
|
" AND TENANT_ID = ? ";
|
||||||
|
stmt = conn.prepareStatement(query);
|
||||||
|
stmt.setString(1, serialNumber);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
|
||||||
|
if(stmt.executeUpdate() > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg =
|
||||||
|
"Unable to get the read the certificate with serial" + serialNumber;
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
} finally {
|
} finally {
|
||||||
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
|
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
}
|
}
|
||||||
return binaryStream;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection getConnection() throws SQLException {
|
private Connection getConnection() throws SQLException {
|
||||||
|
|||||||
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* 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.certificate.mgt.core.dto;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
public class CertificateResponse {
|
||||||
|
|
||||||
|
byte[] certificate;
|
||||||
|
String serialNumber;
|
||||||
|
int tenantId;
|
||||||
|
String commonName;
|
||||||
|
long notAfter;
|
||||||
|
long notBefore;
|
||||||
|
BigInteger certificateserial;
|
||||||
|
String issuer;
|
||||||
|
String subject;
|
||||||
|
int certificateVersion;
|
||||||
|
|
||||||
|
public long getNotAfter() {
|
||||||
|
return notAfter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNotAfter(long notAfter) {
|
||||||
|
this.notAfter = notAfter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getNotBefore() {
|
||||||
|
return notBefore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNotBefore(long notBefore) {
|
||||||
|
this.notBefore = notBefore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigInteger getCertificateserial() {
|
||||||
|
return certificateserial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCertificateserial(BigInteger certificateserial) {
|
||||||
|
this.certificateserial = certificateserial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIssuer() {
|
||||||
|
return issuer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIssuer(String issuer) {
|
||||||
|
this.issuer = issuer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSubject() {
|
||||||
|
return subject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubject(String subject) {
|
||||||
|
this.subject = subject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCertificateVersion() {
|
||||||
|
return certificateVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCertificateVersion(int certificateVersion) {
|
||||||
|
this.certificateVersion = certificateVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommonName() {
|
||||||
|
return commonName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommonName(String commonName) {
|
||||||
|
this.commonName = commonName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getCertificate() {
|
||||||
|
return certificate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCertificate(byte[] certificate) {
|
||||||
|
this.certificate = certificate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSerialNumber() {
|
||||||
|
return serialNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSerialNumber(String serialNumber) {
|
||||||
|
this.serialNumber = serialNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTenantId() {
|
||||||
|
return tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantId(int tenantId) {
|
||||||
|
this.tenantId = tenantId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -39,6 +39,7 @@ import org.bouncycastle.cms.CMSException;
|
|||||||
import org.bouncycastle.cms.CMSSignedData;
|
import org.bouncycastle.cms.CMSSignedData;
|
||||||
import org.bouncycastle.cms.CMSSignedDataGenerator;
|
import org.bouncycastle.cms.CMSSignedDataGenerator;
|
||||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
import org.bouncycastle.openssl.PEMWriter;
|
||||||
import org.bouncycastle.operator.ContentSigner;
|
import org.bouncycastle.operator.ContentSigner;
|
||||||
import org.bouncycastle.operator.OperatorCreationException;
|
import org.bouncycastle.operator.OperatorCreationException;
|
||||||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
|
||||||
@ -59,6 +60,7 @@ import org.wso2.carbon.certificate.mgt.core.dao.CertificateDAO;
|
|||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dto.CAStatus;
|
import org.wso2.carbon.certificate.mgt.core.dto.CAStatus;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse;
|
import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse;
|
||||||
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
||||||
import org.wso2.carbon.certificate.mgt.core.util.CommonUtil;
|
import org.wso2.carbon.certificate.mgt.core.util.CommonUtil;
|
||||||
@ -181,7 +183,13 @@ public class CertificateGenerator {
|
|||||||
|
|
||||||
certificate.verify(certificate.getPublicKey());
|
certificate.verify(certificate.getPublicKey());
|
||||||
|
|
||||||
saveCertInKeyStore(certificate);
|
List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificates = new ArrayList<>();
|
||||||
|
org.wso2.carbon.certificate.mgt.core.bean.Certificate certificateToStore =
|
||||||
|
new org.wso2.carbon.certificate.mgt.core.bean.Certificate();
|
||||||
|
certificateToStore.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
|
||||||
|
certificateToStore.setCertificate(certificate);
|
||||||
|
certificates.add(certificateToStore);
|
||||||
|
saveCertInKeyStore(certificates);
|
||||||
|
|
||||||
return certificate;
|
return certificate;
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
@ -296,6 +304,57 @@ public class CertificateGenerator {
|
|||||||
return (certificate != null);
|
return (certificate != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CertificateResponse verifyPEMSignature(X509Certificate requestCertificate) throws KeystoreException {
|
||||||
|
KeyStoreReader keyStoreReader = new KeyStoreReader();
|
||||||
|
CertificateResponse lookUpCertificate;
|
||||||
|
|
||||||
|
String commonNameExtracted = getCommonName(requestCertificate);
|
||||||
|
lookUpCertificate = keyStoreReader.getCertificateBySerial(commonNameExtracted);
|
||||||
|
return lookUpCertificate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCommonName(X509Certificate requestCertificate) {
|
||||||
|
String distinguishedName = requestCertificate.getSubjectDN().getName();
|
||||||
|
if(distinguishedName != null && !distinguishedName.isEmpty()) {
|
||||||
|
String[] dnSplits = distinguishedName.split(",");
|
||||||
|
for (String dnSplit : dnSplits) {
|
||||||
|
if (dnSplit.contains("CN=")) {
|
||||||
|
String[] cnSplits = dnSplit.split("=");
|
||||||
|
if (cnSplits[1] != null) {
|
||||||
|
return cnSplits[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public X509Certificate pemToX509Certificate(String pem)
|
||||||
|
throws KeystoreException {
|
||||||
|
InputStream inputStream = null;
|
||||||
|
X509Certificate x509Certificate = null;
|
||||||
|
try {
|
||||||
|
|
||||||
|
inputStream = new ByteArrayInputStream(Base64.decodeBase64(pem.getBytes()));
|
||||||
|
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
||||||
|
x509Certificate = (X509Certificate) cf.generateCertificate(inputStream);
|
||||||
|
|
||||||
|
} catch (CertificateException e) {
|
||||||
|
String errorMsg = "Certificate issue occurred when generating converting PEM to x509Certificate";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new KeystoreException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (inputStream != null) {
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error closing Certificate input stream", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return x509Certificate;
|
||||||
|
}
|
||||||
|
|
||||||
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException {
|
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException {
|
||||||
|
|
||||||
if (headerSignature == null || headerSignature.isEmpty()) {
|
if (headerSignature == null || headerSignature.isEmpty()) {
|
||||||
@ -391,8 +450,13 @@ public class CertificateGenerator {
|
|||||||
issuedCert = new JcaX509CertificateConverter().setProvider(
|
issuedCert = new JcaX509CertificateConverter().setProvider(
|
||||||
ConfigurationUtil.PROVIDER).getCertificate(
|
ConfigurationUtil.PROVIDER).getCertificate(
|
||||||
certificateBuilder.build(sigGen));
|
certificateBuilder.build(sigGen));
|
||||||
|
org.wso2.carbon.certificate.mgt.core.bean.Certificate certificate =
|
||||||
saveCertInKeyStore(issuedCert);
|
new org.wso2.carbon.certificate.mgt.core.bean.Certificate();
|
||||||
|
List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificates = new ArrayList<>();
|
||||||
|
certificate.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
|
||||||
|
certificate.setCertificate(issuedCert);
|
||||||
|
certificates.add(certificate);
|
||||||
|
saveCertInKeyStore(certificates);
|
||||||
} catch (CertIOException e) {
|
} catch (CertIOException e) {
|
||||||
String errorMsg = "Certificate Input output issue occurred when generating generateCertificateFromCSR";
|
String errorMsg = "Certificate Input output issue occurred when generating generateCertificateFromCSR";
|
||||||
log.error(errorMsg, e);
|
log.error(errorMsg, e);
|
||||||
@ -549,7 +613,7 @@ public class CertificateGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveCertInKeyStore(X509Certificate certificate)
|
public void saveCertInKeyStore(List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificate)
|
||||||
throws KeystoreException {
|
throws KeystoreException {
|
||||||
|
|
||||||
if (certificate == null) {
|
if (certificate == null) {
|
||||||
@ -557,18 +621,10 @@ public class CertificateGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String serialNumber = certificate.getSerialNumber().toString();
|
|
||||||
byte[] bytes = Serializer.serialize(certificate);
|
|
||||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
|
||||||
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
|
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
|
||||||
CertificateManagementDAOFactory.beginTransaction();
|
CertificateManagementDAOFactory.beginTransaction();
|
||||||
certificateDAO.addCertificate(byteArrayInputStream, serialNumber);
|
certificateDAO.addCertificate(certificate);
|
||||||
CertificateManagementDAOFactory.commitTransaction();
|
CertificateManagementDAOFactory.commitTransaction();
|
||||||
} catch (IOException e) {
|
|
||||||
String errorMsg = "IOException occurred when saving the generated certificate";
|
|
||||||
log.error(errorMsg, e);
|
|
||||||
CertificateManagementDAOFactory.rollbackTransaction();
|
|
||||||
throw new KeystoreException(errorMsg, e);
|
|
||||||
} catch (CertificateManagementDAOException e) {
|
} catch (CertificateManagementDAOException e) {
|
||||||
String errorMsg = "Error occurred when saving the generated certificate";
|
String errorMsg = "Error occurred when saving the generated certificate";
|
||||||
log.error(errorMsg, e);
|
log.error(errorMsg, e);
|
||||||
@ -584,6 +640,8 @@ public class CertificateGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String extractChallengeToken(X509Certificate certificate) {
|
public String extractChallengeToken(X509Certificate certificate) {
|
||||||
|
|
||||||
byte[] challengePassword = certificate.getExtensionValue(
|
byte[] challengePassword = certificate.getExtensionValue(
|
||||||
@ -644,4 +702,28 @@ public class CertificateGenerator {
|
|||||||
certCA.getIssuerX500Principal().getName());
|
certCA.getIssuerX500Principal().getName());
|
||||||
return signedCertificate;
|
return signedCertificate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void extractCertificateDetails(byte[] certificateBytes, CertificateResponse certificateResponse)
|
||||||
|
throws CertificateManagementDAOException {
|
||||||
|
try {
|
||||||
|
if (certificateBytes != null) {
|
||||||
|
java.security.cert.Certificate x509Certificate =
|
||||||
|
(java.security.cert.Certificate) Serializer.deserialize(certificateBytes);
|
||||||
|
if (x509Certificate instanceof X509Certificate) {
|
||||||
|
X509Certificate certificate = (X509Certificate) x509Certificate;
|
||||||
|
certificateResponse.setNotAfter(certificate.getNotAfter().getTime());
|
||||||
|
certificateResponse.setNotBefore(certificate.getNotBefore().getTime());
|
||||||
|
certificateResponse.setCertificateserial(certificate.getSerialNumber());
|
||||||
|
certificateResponse.setIssuer(certificate.getIssuerDN().getName());
|
||||||
|
certificateResponse.setSubject(certificate.getSubjectDN().getName());
|
||||||
|
certificateResponse.setCertificateVersion(certificate.getVersion());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ClassNotFoundException | IOException e) {
|
||||||
|
String errorMsg = "Error while deserializing the certificate.";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -21,6 +21,7 @@ import org.apache.commons.logging.Log;
|
|||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse;
|
||||||
import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil;
|
import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil;
|
||||||
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
||||||
import org.wso2.carbon.certificate.mgt.core.util.Serializer;
|
import org.wso2.carbon.certificate.mgt.core.util.Serializer;
|
||||||
@ -30,13 +31,10 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.security.KeyStore;
|
import java.security.*;
|
||||||
import java.security.KeyStoreException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.security.PrivateKey;
|
|
||||||
import java.security.UnrecoverableKeyException;
|
|
||||||
import java.security.cert.Certificate;
|
import java.security.cert.Certificate;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class KeyStoreReader {
|
public class KeyStoreReader {
|
||||||
@ -213,8 +211,11 @@ public class KeyStoreReader {
|
|||||||
Certificate raCertificate = null;
|
Certificate raCertificate = null;
|
||||||
try {
|
try {
|
||||||
CertificateManagementDAOFactory.openConnection();
|
CertificateManagementDAOFactory.openConnection();
|
||||||
byte[] certificateBytes = CertificateManagementDAOFactory.getCertificateDAO().retrieveCertificate(alias);
|
CertificateResponse certificateResponse = CertificateManagementDAOFactory.getCertificateDAO().
|
||||||
raCertificate = (Certificate) Serializer.deserialize(certificateBytes);
|
retrieveCertificate(alias);
|
||||||
|
if(certificateResponse != null) {
|
||||||
|
raCertificate = (Certificate) Serializer.deserialize(certificateResponse.getCertificate());
|
||||||
|
}
|
||||||
} catch (CertificateManagementDAOException e) {
|
} catch (CertificateManagementDAOException e) {
|
||||||
String errorMsg = "Error when retrieving certificate the the database for the alias " + alias;
|
String errorMsg = "Error when retrieving certificate the the database for the alias " + alias;
|
||||||
log.error(errorMsg, e);
|
log.error(errorMsg, e);
|
||||||
@ -261,4 +262,39 @@ public class KeyStoreReader {
|
|||||||
|
|
||||||
return raPrivateKey;
|
return raPrivateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CertificateResponse getCertificateBySerial(String serialNumber) throws KeystoreException {
|
||||||
|
|
||||||
|
CertificateResponse certificateResponse = null;
|
||||||
|
try {
|
||||||
|
CertificateManagementDAOFactory.openConnection();
|
||||||
|
certificateResponse = CertificateManagementDAOFactory.getCertificateDAO().
|
||||||
|
retrieveCertificate(serialNumber);
|
||||||
|
if(certificateResponse != null && certificateResponse.getCertificate() != null) {
|
||||||
|
Certificate certificate = (Certificate) Serializer.deserialize(certificateResponse.getCertificate());
|
||||||
|
if (certificate instanceof X509Certificate) {
|
||||||
|
X509Certificate x509cert = (X509Certificate) certificate;
|
||||||
|
String commonName = CertificateGenerator.getCommonName(x509cert);
|
||||||
|
certificateResponse.setCommonName(commonName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (CertificateManagementDAOException e) {
|
||||||
|
String errorMsg = "Error when retrieving certificate from the the database for the serial number: " +
|
||||||
|
serialNumber;
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new KeystoreException(errorMsg, e);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg = "Error when making a connection to the database.";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new KeystoreException(errorMsg, e);
|
||||||
|
} catch (ClassNotFoundException | IOException e) {
|
||||||
|
String errorMsg = "Error when deserializing saved certificate.";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new KeystoreException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
return certificateResponse;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,8 +18,12 @@
|
|||||||
package org.wso2.carbon.certificate.mgt.core.service;
|
package org.wso2.carbon.certificate.mgt.core.service;
|
||||||
|
|
||||||
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
|
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse;
|
import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse;
|
||||||
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
@ -50,9 +54,28 @@ public interface CertificateManagementService {
|
|||||||
|
|
||||||
boolean verifySignature(String headerSignature) throws KeystoreException;
|
boolean verifySignature(String headerSignature) throws KeystoreException;
|
||||||
|
|
||||||
|
public CertificateResponse verifyPEMSignature(X509Certificate requestCertificate) throws KeystoreException;
|
||||||
|
|
||||||
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException;
|
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException;
|
||||||
|
|
||||||
String extractChallengeToken(X509Certificate certificate);
|
String extractChallengeToken(X509Certificate certificate);
|
||||||
|
|
||||||
X509Certificate getSignedCertificateFromCSR(String binarySecurityToken) throws KeystoreException;
|
X509Certificate getSignedCertificateFromCSR(String binarySecurityToken) throws KeystoreException;
|
||||||
|
|
||||||
|
public CertificateResponse getCertificateBySerial(String serial) throws KeystoreException;
|
||||||
|
|
||||||
|
public void saveCertificate(List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificate)
|
||||||
|
throws KeystoreException;
|
||||||
|
|
||||||
|
public X509Certificate pemToX509Certificate(String pem) throws KeystoreException;
|
||||||
|
|
||||||
|
public CertificateResponse retrieveCertificate(String serialNumber) throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
public PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
public List<CertificateResponse> getCertificates() throws CertificateManagementDAOException;
|
||||||
|
|
||||||
|
public List<CertificateResponse> searchCertificates(String serialNumber) throws CertificateManagementDAOException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,19 +17,29 @@
|
|||||||
*/
|
*/
|
||||||
package org.wso2.carbon.certificate.mgt.core.service;
|
package org.wso2.carbon.certificate.mgt.core.service;
|
||||||
|
|
||||||
|
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
|
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateDAO;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOException;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse;
|
||||||
import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse;
|
import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse;
|
||||||
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
||||||
import org.wso2.carbon.certificate.mgt.core.impl.CertificateGenerator;
|
import org.wso2.carbon.certificate.mgt.core.impl.CertificateGenerator;
|
||||||
import org.wso2.carbon.certificate.mgt.core.impl.KeyStoreReader;
|
import org.wso2.carbon.certificate.mgt.core.impl.KeyStoreReader;
|
||||||
import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil;
|
import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||||
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||||
|
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
import java.security.cert.Certificate;
|
import java.security.cert.Certificate;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CertificateManagementServiceImpl implements CertificateManagementService {
|
public class CertificateManagementServiceImpl implements CertificateManagementService {
|
||||||
@ -94,6 +104,10 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
|
|||||||
return certificateGenerator.verifySignature(headerSignature);
|
return certificateGenerator.verifySignature(headerSignature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CertificateResponse verifyPEMSignature(X509Certificate requestCertificate) throws KeystoreException {
|
||||||
|
return certificateGenerator.verifyPEMSignature(requestCertificate);
|
||||||
|
}
|
||||||
|
|
||||||
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException {
|
public X509Certificate extractCertificateFromSignature(String headerSignature) throws KeystoreException {
|
||||||
return certificateGenerator.extractCertificateFromSignature(headerSignature);
|
return certificateGenerator.extractCertificateFromSignature(headerSignature);
|
||||||
}
|
}
|
||||||
@ -106,4 +120,94 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
|
|||||||
return certificateGenerator.getSignedCertificateFromCSR(binarySecurityToken);
|
return certificateGenerator.getSignedCertificateFromCSR(binarySecurityToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CertificateResponse getCertificateBySerial(String serial) throws KeystoreException {
|
||||||
|
return keyStoreReader.getCertificateBySerial(serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveCertificate(List<org.wso2.carbon.certificate.mgt.core.bean.Certificate> certificate)
|
||||||
|
throws KeystoreException {
|
||||||
|
certificateGenerator.saveCertInKeyStore(certificate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public X509Certificate pemToX509Certificate(String pem) throws KeystoreException {
|
||||||
|
return certificateGenerator.pemToX509Certificate(pem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CertificateResponse retrieveCertificate(String serialNumber)
|
||||||
|
throws CertificateManagementDAOException {
|
||||||
|
CertificateDAO certificateDAO;
|
||||||
|
try {
|
||||||
|
CertificateManagementDAOFactory.openConnection();
|
||||||
|
certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
|
||||||
|
return certificateDAO.retrieveCertificate(serialNumber);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg = "Error when opening connection";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PaginationResult getAllCertificates(PaginationRequest request)
|
||||||
|
throws CertificateManagementDAOException {
|
||||||
|
try {
|
||||||
|
CertificateManagementDAOFactory.openConnection();
|
||||||
|
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
|
||||||
|
return certificateDAO.getAllCertificates(request);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg = "Error when opening connection";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException {
|
||||||
|
try {
|
||||||
|
CertificateManagementDAOFactory.beginTransaction();
|
||||||
|
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
|
||||||
|
Boolean status = certificateDAO.removeCertificate(serialNumber);
|
||||||
|
CertificateManagementDAOFactory.commitTransaction();
|
||||||
|
return status;
|
||||||
|
} catch (TransactionManagementException e) {
|
||||||
|
String errorMsg = "Error when deleting";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<CertificateResponse> getCertificates() throws CertificateManagementDAOException {
|
||||||
|
try {
|
||||||
|
CertificateManagementDAOFactory.openConnection();
|
||||||
|
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
|
||||||
|
return certificateDAO.getAllCertificates();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg = "Error when opening connection";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public List<CertificateResponse> searchCertificates(String serialNumber) throws CertificateManagementDAOException {
|
||||||
|
try {
|
||||||
|
CertificateManagementDAOFactory.openConnection();
|
||||||
|
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
|
||||||
|
return certificateDAO.searchCertificate(serialNumber);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
String errorMsg = "Error when opening connection";
|
||||||
|
log.error(errorMsg, e);
|
||||||
|
throw new CertificateManagementDAOException(errorMsg, e);
|
||||||
|
} finally {
|
||||||
|
CertificateManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,15 @@
|
|||||||
|
package org.wso2.carbon.certificate.mgt.core.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.certificate.mgt.core.exception.CertificateManagementException;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
|
||||||
|
public class TenantUtil {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(TenantUtil.class);
|
||||||
|
|
||||||
|
public static int getTenanntId(String tenantDomain) throws CertificateManagementException {
|
||||||
|
return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -22,14 +22,14 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<artifactId>carbon-devicemgt</artifactId>
|
<artifactId>carbon-devicemgt</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<artifactId>certificate-mgt</artifactId>
|
<artifactId>certificate-mgt</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<name>WSO2 Carbon - Certificate Management Component</name>
|
<name>WSO2 Carbon - Certificate Management Component</name>
|
||||||
<url>http://wso2.org</url>
|
<url>http://wso2.org</url>
|
||||||
|
|||||||
@ -0,0 +1,143 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>device-mgt</artifactId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - Device Analytics</name>
|
||||||
|
<description>WSO2 Carbon - Device Analytics</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testng</groupId>
|
||||||
|
<artifactId>testng</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.tomcat</groupId>
|
||||||
|
<artifactId>tomcat</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.tomcat</groupId>
|
||||||
|
<artifactId>tomcat-servlet-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.databridge.agent</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.databridge.commons</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.analytics</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.analytics.api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.registry</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.registry.indexing</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json.wso2</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Data Publisher</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.analytics.data.publisher.internal</Private-Package>
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.analytics.data.publisher.internal,
|
||||||
|
org.wso2.carbon.device.mgt.analytics.data.publisher.*;version="${carbon.device.mgt.version}"
|
||||||
|
</Export-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging.*,
|
||||||
|
org.wso2.carbon.context;version="${carbon.kernel.version.range}",
|
||||||
|
org.wso2.carbon.utils;version="${carbon.kernel.version.range}",
|
||||||
|
org.wso2.carbon.databridge.*;version="${carbon.analytics.common.version.range}",
|
||||||
|
org.wso2.carbon.analytics.*;version="${carbon.analytics.version.range}",
|
||||||
|
org.wso2.carbon.registry.core.*;resolution:=optional,
|
||||||
|
org.wso2.carbon.registry.common.*;version="${carbon.registry.imp.pkg.version.range}",
|
||||||
|
org.wso2.carbon.registry.indexing.*; version="${carbon.registry.imp.pkg.version.range}",
|
||||||
|
org.json;version="${commons-json.version}",
|
||||||
|
javax.xml.bind,
|
||||||
|
javax.xml.bind.annotation,
|
||||||
|
javax.xml.parsers,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.base
|
||||||
|
</Import-Package>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class AnalyticsDataRecord {
|
||||||
|
private Map<String, Object> values;
|
||||||
|
private long timestamp;
|
||||||
|
|
||||||
|
public AnalyticsDataRecord() {
|
||||||
|
values = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnalyticsDataRecord(Map<String, Object> values) {
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getValues() {
|
||||||
|
return this.values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String name, Object value) {
|
||||||
|
values.put(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getValue(String name) {
|
||||||
|
return this.values.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValues(Map<String, Object> values) {
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTimestamp() {
|
||||||
|
return this.timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimestamp(long timestamp) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class DeviceAnalyticsUtil {
|
||||||
|
|
||||||
|
public static Document convertToDocument(File file) throws DataPublisherConfigurationException {
|
||||||
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
|
factory.setNamespaceAware(true);
|
||||||
|
try {
|
||||||
|
DocumentBuilder docBuilder = factory.newDocumentBuilder();
|
||||||
|
return docBuilder.parse(file);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new DataPublisherConfigurationException("Error occurred while parsing file, while converting " +
|
||||||
|
"to a org.w3c.dom.Document", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,221 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.wso2.carbon.base.MultitenantConstants;
|
||||||
|
import org.wso2.carbon.context.CarbonContext;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.databridge.agent.DataPublisher;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointException;
|
||||||
|
import org.wso2.carbon.databridge.commons.exception.TransportException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.config.AnalyticsConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherAlreadyExistsException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.internal.DeviceAnalyticsDataHolder;
|
||||||
|
import org.wso2.carbon.registry.core.Registry;
|
||||||
|
import org.wso2.carbon.registry.core.Resource;
|
||||||
|
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.registry.core.service.TenantRegistryLoader;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is used to manage data publisher per tenant.
|
||||||
|
*/
|
||||||
|
public class DeviceDataPublisher {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(DeviceDataPublisher.class);
|
||||||
|
private static final String TENANT_DAS_CONFIG_LOCATION = "/das/config.json";
|
||||||
|
private static final String USERNAME_CONFIG_TAG = "username";
|
||||||
|
private static final String PASSWORD_CONFIG_TAG = "password";
|
||||||
|
/**
|
||||||
|
* map to store data publishers for each tenant.
|
||||||
|
*/
|
||||||
|
private static Map<String, DataPublisher> dataPublisherMap;
|
||||||
|
private static DeviceDataPublisher deviceDataPublisher;
|
||||||
|
|
||||||
|
public static DeviceDataPublisher getInstance() {
|
||||||
|
if (deviceDataPublisher == null) {
|
||||||
|
synchronized (DeviceDataPublisher.class) {
|
||||||
|
if (deviceDataPublisher == null) {
|
||||||
|
deviceDataPublisher = new DeviceDataPublisher();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deviceDataPublisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DeviceDataPublisher() {
|
||||||
|
dataPublisherMap = new ConcurrentHashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this return the data publisher for the tenant.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws DataPublisherConfigurationException
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public DataPublisher getDataPublisher() throws DataPublisherConfigurationException {
|
||||||
|
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||||
|
//Get LoadBalancingDataPublisher which has been registered for the tenant.
|
||||||
|
DataPublisher dataPublisher = getDataPublisher(tenantDomain);
|
||||||
|
//If a LoadBalancingDataPublisher had not been registered for the tenant.
|
||||||
|
if (dataPublisher == null) {
|
||||||
|
AnalyticsConfiguration analyticsConfig = AnalyticsConfiguration.getInstance();
|
||||||
|
if (!analyticsConfig.isEnable()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String analyticsServerUrlGroups = analyticsConfig.getReceiverServerUrl();
|
||||||
|
String analyticsServerUsername = analyticsConfig.getAdminUsername();
|
||||||
|
String analyticsServerPassword = analyticsConfig.getAdminPassword();
|
||||||
|
if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
|
||||||
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
String userInfo[] = getAnalyticsServerUserInfo(tenantId);
|
||||||
|
if (userInfo != null) {
|
||||||
|
analyticsServerUsername = userInfo[0];
|
||||||
|
analyticsServerPassword = userInfo[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Create new DataPublisher for the tenant.
|
||||||
|
try {
|
||||||
|
dataPublisher = new DataPublisher(analyticsServerUrlGroups, analyticsServerUsername,
|
||||||
|
analyticsServerPassword);
|
||||||
|
//Add created DataPublisher.
|
||||||
|
addDataPublisher(tenantDomain, dataPublisher);
|
||||||
|
} catch (DataEndpointAgentConfigurationException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Configuration Exception on data publisher for " +
|
||||||
|
"ReceiverGroup = " + analyticsServerUrlGroups + " for username " + analyticsServerUsername, e);
|
||||||
|
} catch (DataEndpointException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Invalid ReceiverGroup = " + analyticsServerUrlGroups, e);
|
||||||
|
} catch (DataEndpointConfigurationException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Invalid Data endpoint configuration.", e);
|
||||||
|
} catch (DataEndpointAuthenticationException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Authentication Failed for user " +
|
||||||
|
analyticsServerUsername, e);
|
||||||
|
} catch (TransportException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Error occurred while retrieving data publisher", e);
|
||||||
|
} catch (DataPublisherAlreadyExistsException e) {
|
||||||
|
log.warn("Attempting to register a data publisher for the tenant " + tenantDomain +
|
||||||
|
" when one already exists. Returning existing data publisher");
|
||||||
|
return getDataPublisher(tenantDomain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dataPublisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the data publisher which has been registered under the tenant domain.
|
||||||
|
*
|
||||||
|
* @param tenantDomain - The tenant domain under which the data publisher is registered
|
||||||
|
* @return - Instance of the DataPublisher which was registered. Null if not registered.
|
||||||
|
*/
|
||||||
|
private DataPublisher getDataPublisher(String tenantDomain) {
|
||||||
|
if (dataPublisherMap.containsKey(tenantDomain)) {
|
||||||
|
return dataPublisherMap.get(tenantDomain);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a LoadBalancingDataPublisher to the data publisher map.
|
||||||
|
*
|
||||||
|
* @param tenantDomain - The tenant domain under which the data publisher will be registered.
|
||||||
|
* @param dataPublisher - Instance of the LoadBalancingDataPublisher
|
||||||
|
* @throws DataPublisherAlreadyExistsException
|
||||||
|
* -
|
||||||
|
* If a data publisher has already been registered under the tenant
|
||||||
|
* domain
|
||||||
|
*/
|
||||||
|
private void addDataPublisher(String tenantDomain, DataPublisher dataPublisher)
|
||||||
|
throws DataPublisherAlreadyExistsException {
|
||||||
|
if (dataPublisherMap.containsKey(tenantDomain)) {
|
||||||
|
throw new DataPublisherAlreadyExistsException(
|
||||||
|
"A DataPublisher has already been created for the tenant " + tenantDomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
dataPublisherMap.put(tenantDomain, dataPublisher);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retrieve the credential from registry
|
||||||
|
*/
|
||||||
|
private String[] getAnalyticsServerUserInfo(int tenantId) throws DataPublisherConfigurationException {
|
||||||
|
try {
|
||||||
|
String config = getConfigRegistryResourceContent(tenantId, TENANT_DAS_CONFIG_LOCATION);
|
||||||
|
JSONObject jsonConfigforDas = new JSONObject(config);
|
||||||
|
String credential[] = new String[2];
|
||||||
|
credential[0] = jsonConfigforDas.getString(USERNAME_CONFIG_TAG);
|
||||||
|
credential[1] = jsonConfigforDas.getString(PASSWORD_CONFIG_TAG);
|
||||||
|
return credential;
|
||||||
|
} catch (RegistryException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Failed to load the registry for tenant " + tenantId, e);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
throw new DataPublisherConfigurationException(
|
||||||
|
"Failed to parse the credential from the registry for tenant " + tenantId, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the credential detail from the registry for tenants.
|
||||||
|
*
|
||||||
|
* @param tenantId for identify tenant space.
|
||||||
|
* @param registryLocation retrieve the config file from tenant space.
|
||||||
|
* @return the config for tenant
|
||||||
|
* @throws RegistryException
|
||||||
|
*/
|
||||||
|
private String getConfigRegistryResourceContent(int tenantId, final String registryLocation)
|
||||||
|
throws RegistryException {
|
||||||
|
String content = null;
|
||||||
|
try {
|
||||||
|
PrivilegedCarbonContext.startTenantFlow();
|
||||||
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
||||||
|
RegistryService registryService = DeviceAnalyticsDataHolder.getInstance().getRegistryService();
|
||||||
|
if (registryService != null) {
|
||||||
|
Registry registry = registryService.getConfigSystemRegistry(tenantId);
|
||||||
|
this.loadTenantRegistry(tenantId);
|
||||||
|
if (registry.resourceExists(registryLocation)) {
|
||||||
|
Resource resource = registry.get(registryLocation);
|
||||||
|
content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
PrivilegedCarbonContext.endTenantFlow();
|
||||||
|
}
|
||||||
|
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadTenantRegistry(int tenantId) throws RegistryException {
|
||||||
|
TenantRegistryLoader tenantRegistryLoader = DeviceAnalyticsDataHolder.getInstance().getTenantRegistryLoader();
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().getIndexLoaderService().loadTenantIndex(tenantId);
|
||||||
|
tenantRegistryLoader.loadTenantRegistry(tenantId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.config;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.DeviceAnalyticsUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configurations related to DAS data publisher and DAL.
|
||||||
|
*/
|
||||||
|
@XmlRootElement(name = "AnalyticsConfiguration")
|
||||||
|
public class AnalyticsConfiguration {
|
||||||
|
|
||||||
|
private String receiverServerUrl;
|
||||||
|
private String adminUsername;
|
||||||
|
private String adminPassword;
|
||||||
|
private boolean enable;
|
||||||
|
|
||||||
|
private static AnalyticsConfiguration config;
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(AnalyticsConfiguration.class);
|
||||||
|
private static final String DEVICE_ANALYTICS_CONFIG_PATH =
|
||||||
|
CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "device-analytics-config.xml";
|
||||||
|
|
||||||
|
private AnalyticsConfiguration() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AnalyticsConfiguration getInstance() {
|
||||||
|
if (config == null) {
|
||||||
|
throw new InvalidConfigurationStateException("Device analytics configuration is not " +
|
||||||
|
"initialized properly");
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@XmlElement(name = "AdminUsername", required = true)
|
||||||
|
public String getAdminUsername() {
|
||||||
|
return adminUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdminUsername(String adminUsername) {
|
||||||
|
this.adminUsername = adminUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "AdminPassword", required = true)
|
||||||
|
public String getAdminPassword() {
|
||||||
|
return adminPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdminPassword(String adminPassword) {
|
||||||
|
this.adminPassword = adminPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "ReceiverServerUrl", required = true)
|
||||||
|
public String getReceiverServerUrl() {
|
||||||
|
return receiverServerUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReceiverServerUrl(String receiverServerUrl) {
|
||||||
|
this.receiverServerUrl = receiverServerUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Enabled", required = true)
|
||||||
|
public boolean isEnable() {
|
||||||
|
return enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnable(boolean status) {
|
||||||
|
this.enable = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void init() throws DataPublisherConfigurationException {
|
||||||
|
try {
|
||||||
|
File authConfig = new File(AnalyticsConfiguration.DEVICE_ANALYTICS_CONFIG_PATH);
|
||||||
|
Document doc = DeviceAnalyticsUtil.convertToDocument(authConfig);
|
||||||
|
|
||||||
|
/* Un-marshaling device analytics configuration */
|
||||||
|
JAXBContext ctx = JAXBContext.newInstance(AnalyticsConfiguration.class);
|
||||||
|
Unmarshaller unmarshaller = ctx.createUnmarshaller();
|
||||||
|
config = (AnalyticsConfiguration) unmarshaller.unmarshal(doc);
|
||||||
|
} catch (JAXBException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Error occurred while un-marshalling device analytics " +
|
||||||
|
"Config", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.config;
|
||||||
|
|
||||||
|
public class InvalidConfigurationStateException extends RuntimeException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -3151274311329070297L;
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.exception;
|
||||||
|
|
||||||
|
public class DataPublisherAlreadyExistsException extends Exception {
|
||||||
|
public DataPublisherAlreadyExistsException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherAlreadyExistsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherAlreadyExistsException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherAlreadyExistsException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DataPublisherAlreadyExistsException(String message, Throwable cause,
|
||||||
|
boolean enableSuppression,
|
||||||
|
boolean writableStackTrace) {
|
||||||
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.exception;
|
||||||
|
|
||||||
|
public class DataPublisherConfigurationException extends Exception {
|
||||||
|
public DataPublisherConfigurationException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherConfigurationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherConfigurationException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherConfigurationException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DataPublisherConfigurationException(String message, Throwable cause,
|
||||||
|
boolean enableSuppression,
|
||||||
|
boolean writableStackTrace) {
|
||||||
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.exception;
|
||||||
|
|
||||||
|
public class DeviceManagementAnalyticsException extends Exception {
|
||||||
|
public DeviceManagementAnalyticsException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceManagementAnalyticsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceManagementAnalyticsException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceManagementAnalyticsException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DeviceManagementAnalyticsException(String message, Throwable cause,
|
||||||
|
boolean enableSuppression,
|
||||||
|
boolean writableStackTrace) {
|
||||||
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.internal;
|
||||||
|
|
||||||
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.registry.core.service.TenantRegistryLoader;
|
||||||
|
import org.wso2.carbon.registry.indexing.service.TenantIndexingLoader;
|
||||||
|
|
||||||
|
public class DeviceAnalyticsDataHolder {
|
||||||
|
private static DeviceAnalyticsDataHolder thisInstance = new DeviceAnalyticsDataHolder();
|
||||||
|
/**
|
||||||
|
* AnalyticsDataAPI is service used to retrieve data from DAS.
|
||||||
|
*/
|
||||||
|
private AnalyticsDataAPI analyticsDataAPI;
|
||||||
|
private TenantRegistryLoader tenantRegistryLoader;
|
||||||
|
private TenantIndexingLoader indexLoader;
|
||||||
|
private RegistryService registryService;
|
||||||
|
private DeviceAnalyticsDataHolder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static DeviceAnalyticsDataHolder getInstance() {
|
||||||
|
return thisInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnalyticsDataAPI getAnalyticsDataAPI() {
|
||||||
|
return analyticsDataAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAnalyticsDataAPI(AnalyticsDataAPI analyticsDataAPI) {
|
||||||
|
this.analyticsDataAPI = analyticsDataAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader){
|
||||||
|
this.tenantRegistryLoader = tenantRegistryLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TenantRegistryLoader getTenantRegistryLoader(){
|
||||||
|
return tenantRegistryLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIndexLoaderService(TenantIndexingLoader indexLoader) {
|
||||||
|
this.indexLoader = indexLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TenantIndexingLoader getIndexLoaderService(){
|
||||||
|
return indexLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RegistryService getRegistryService() {
|
||||||
|
return registryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistryService(RegistryService registryService) {
|
||||||
|
this.registryService = registryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,151 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.internal;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.osgi.framework.BundleContext;
|
||||||
|
import org.osgi.framework.ServiceRegistration;
|
||||||
|
import org.osgi.service.component.ComponentContext;
|
||||||
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.config.AnalyticsConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.DeviceAnalyticsService;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.DeviceAnalyticsServiceImpl;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.registry.core.service.TenantRegistryLoader;
|
||||||
|
import org.wso2.carbon.registry.indexing.service.TenantIndexingLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.analytics.internal.DeviceAnalyticsServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="device.analytics.api"
|
||||||
|
* interface="org.wso2.carbon.analytics.api.AnalyticsDataAPI"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setAnalyticsDataAPI"
|
||||||
|
* unbind="unsetAnalyticsDataAPI"
|
||||||
|
* @scr.reference name="registry.service"
|
||||||
|
* interface="org.wso2.carbon.registry.core.service.RegistryService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setRegistryService"
|
||||||
|
* unbind="unsetRegistryService"
|
||||||
|
* @scr.reference name="tenant.registryloader"
|
||||||
|
* interface="org.wso2.carbon.registry.core.service.TenantRegistryLoader"
|
||||||
|
* cardinality="1..1" policy="dynamic"
|
||||||
|
* bind="setTenantRegistryLoader"
|
||||||
|
* unbind="unsetTenantRegistryLoader"
|
||||||
|
* @scr.reference name="tenant.indexloader"
|
||||||
|
* interface="org.wso2.carbon.registry.indexing.service.TenantIndexingLoader"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setIndexLoader"
|
||||||
|
* unbind="unsetIndexLoader"
|
||||||
|
*/
|
||||||
|
public class DeviceAnalyticsServiceComponent {
|
||||||
|
|
||||||
|
private ServiceRegistration analyticsServiceRef;
|
||||||
|
private static Log log = LogFactory.getLog(DeviceAnalyticsServiceComponent.class);
|
||||||
|
|
||||||
|
protected void activate(ComponentContext componentCtx) {
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Initializing device analytics bundle");
|
||||||
|
}
|
||||||
|
AnalyticsConfiguration.init();
|
||||||
|
|
||||||
|
BundleContext bundleCtx = componentCtx.getBundleContext();
|
||||||
|
this.analyticsServiceRef =
|
||||||
|
bundleCtx.registerService(DeviceAnalyticsService.class, new DeviceAnalyticsServiceImpl(), null);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Device management analytics bundle has been successfully initialized");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while initializing device analytics bundle", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext componentCtx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Deactivating device analytics bundle");
|
||||||
|
}
|
||||||
|
if (analyticsServiceRef != null) {
|
||||||
|
analyticsServiceRef.unregister();
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Device analytics bundle has been successfully deactivated");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets AnalyticsDataAPI Service.
|
||||||
|
*
|
||||||
|
* @param analyticsDataAPI An instance of AnalyticsDataAPI
|
||||||
|
*/
|
||||||
|
protected void setAnalyticsDataAPI(AnalyticsDataAPI analyticsDataAPI) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Setting AnalyticsDataAPI Service");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setAnalyticsDataAPI(analyticsDataAPI);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un sets AnalyticsDataAPI Service.
|
||||||
|
*
|
||||||
|
* @param analyticsDataAPI An instance of AnalyticsDataAPI
|
||||||
|
*/
|
||||||
|
protected void unsetAnalyticsDataAPI(AnalyticsDataAPI analyticsDataAPI) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Un-Setting AnalyticsDataAPI Service");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setAnalyticsDataAPI(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setRegistryService(RegistryService registryService) {
|
||||||
|
if (registryService != null && log.isDebugEnabled()) {
|
||||||
|
log.debug("Registry service initialized");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setRegistryService(registryService);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetRegistryService(RegistryService registryService) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setRegistryService(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setTenantRegistryLoader(tenantRegistryLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setTenantRegistryLoader(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setIndexLoader(TenantIndexingLoader indexLoader) {
|
||||||
|
if (indexLoader != null && log.isDebugEnabled()) {
|
||||||
|
log.debug("IndexLoader service initialized");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setIndexLoaderService(indexLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetIndexLoader(TenantIndexingLoader indexLoader) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setIndexLoaderService(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.service;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.AnalyticsDataRecord;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DeviceManagementAnalyticsException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This service can be used to publish and retreive data from the Analytics Server.
|
||||||
|
*/
|
||||||
|
public interface DeviceAnalyticsService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is used to publish an event to DAS.
|
||||||
|
* @param streamName is the name of the stream that the data needs to pushed
|
||||||
|
* @param version is the version of the stream
|
||||||
|
* @param metaDataArray - meta data that needs to pushed
|
||||||
|
* @param correlationDataArray - correlation data that needs to be pushed
|
||||||
|
* @param payloadDataArray - payload data that needs to be pushed
|
||||||
|
* @return
|
||||||
|
* @throws DataPublisherConfigurationException
|
||||||
|
*/
|
||||||
|
boolean publishEvent(String streamName, String version, Object[] metaDataArray, Object[] correlationDataArray,
|
||||||
|
Object[] payloadDataArray) throws DataPublisherConfigurationException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This service can be used to retrieve all the event for the query.
|
||||||
|
* @param tableName is the name of the table that events need to be retrieved
|
||||||
|
* @param query is query to be executed.
|
||||||
|
* @return the record list
|
||||||
|
* @throws DeviceManagementAnalyticsException
|
||||||
|
*/
|
||||||
|
List<AnalyticsDataRecord> getAllEventsForDevice(String tableName,
|
||||||
|
String query) throws DeviceManagementAnalyticsException;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* 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.analytics.data.publisher.service;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDrillDownRequest;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.core.AnalyticsDataServiceUtils;
|
||||||
|
import org.wso2.carbon.analytics.datasource.commons.Record;
|
||||||
|
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
|
||||||
|
import org.wso2.carbon.context.CarbonContext;
|
||||||
|
import org.wso2.carbon.databridge.agent.DataPublisher;
|
||||||
|
import org.wso2.carbon.databridge.commons.utils.DataBridgeCommonsUtils;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.AnalyticsDataRecord;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.DeviceDataPublisher;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DeviceManagementAnalyticsException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.internal.DeviceAnalyticsDataHolder;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the implementation of Osgi Service which can be used to publish and retireved
|
||||||
|
* event/records.
|
||||||
|
*/
|
||||||
|
public class DeviceAnalyticsServiceImpl implements DeviceAnalyticsService {
|
||||||
|
private static Log log = LogFactory.getLog(DeviceAnalyticsServiceImpl.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param streamName is the name of the stream that the data needs to pushed
|
||||||
|
* @param version is the version of the stream
|
||||||
|
* @param metaDataArray - meta data that needs to pushed
|
||||||
|
* @param correlationDataArray - correlation data that needs to be pushed
|
||||||
|
* @param payloadDataArray - payload data that needs to be pushed
|
||||||
|
* @return
|
||||||
|
* @throws DataPublisherConfigurationException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean publishEvent(String streamName, String version, Object[] metaDataArray,
|
||||||
|
Object[] correlationDataArray,
|
||||||
|
Object[] payloadDataArray) throws DataPublisherConfigurationException {
|
||||||
|
DataPublisher dataPublisher = DeviceDataPublisher.getInstance().getDataPublisher();
|
||||||
|
if (dataPublisher != null) {
|
||||||
|
String streamId = DataBridgeCommonsUtils.generateStreamId(streamName, version);
|
||||||
|
return dataPublisher.tryPublish(streamId, System.currentTimeMillis(), metaDataArray, correlationDataArray,
|
||||||
|
payloadDataArray);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param tableName is the name of the table that events need to be retrieved
|
||||||
|
* @param query is query to be executed.
|
||||||
|
* @return
|
||||||
|
* @throws AnalyticsException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<AnalyticsDataRecord> getAllEventsForDevice(String tableName, String query) throws
|
||||||
|
DeviceManagementAnalyticsException {
|
||||||
|
try {
|
||||||
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
AnalyticsDataAPI analyticsDataAPI = DeviceAnalyticsDataHolder.getInstance().getAnalyticsDataAPI();
|
||||||
|
int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query);
|
||||||
|
if (eventCount == 0) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
AnalyticsDrillDownRequest drillDownRequest = new AnalyticsDrillDownRequest();
|
||||||
|
drillDownRequest.setQuery(query);
|
||||||
|
drillDownRequest.setTableName(tableName);
|
||||||
|
drillDownRequest.setRecordCount(eventCount);
|
||||||
|
List<SearchResultEntry> resultEntries = analyticsDataAPI.drillDownSearch(tenantId, drillDownRequest);
|
||||||
|
List<String> recordIds = getRecordIds(resultEntries);
|
||||||
|
AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds);
|
||||||
|
List<Record> records = AnalyticsDataServiceUtils.listRecords(analyticsDataAPI, response);
|
||||||
|
return getAnalyticsDataRecords(records);
|
||||||
|
} catch (AnalyticsException e) {
|
||||||
|
throw new DeviceManagementAnalyticsException(
|
||||||
|
"Failed fetch data for table " + tableName + "with the query " + query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getRecordIds(List<SearchResultEntry> searchResults) {
|
||||||
|
List<String> ids = new ArrayList<>();
|
||||||
|
for (SearchResultEntry searchResult : searchResults) {
|
||||||
|
ids.add(searchResult.getId());
|
||||||
|
}
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<AnalyticsDataRecord> getAnalyticsDataRecords(List<Record> records) {
|
||||||
|
List<AnalyticsDataRecord> analyticsDataRecords = new ArrayList<>();
|
||||||
|
for (Record record : records) {
|
||||||
|
AnalyticsDataRecord analyticsDataRecord = new AnalyticsDataRecord(record.getValues());
|
||||||
|
analyticsDataRecords.add(analyticsDataRecord);
|
||||||
|
}
|
||||||
|
return analyticsDataRecords;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -21,13 +21,12 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>device-mgt</artifactId>
|
<artifactId>device-mgt</artifactId>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
|
||||||
<packaging>bundle</packaging>
|
<packaging>bundle</packaging>
|
||||||
<name>WSO2 Carbon - Device Management Commons</name>
|
<name>WSO2 Carbon - Device Management Commons</name>
|
||||||
<description>WSO2 Carbon - Device Management Commons</description>
|
<description>WSO2 Carbon - Device Management Commons</description>
|
||||||
@ -38,7 +37,6 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.felix</groupId>
|
<groupId>org.apache.felix</groupId>
|
||||||
<artifactId>maven-bundle-plugin</artifactId>
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
<version>1.4.0</version>
|
|
||||||
<extensions>true</extensions>
|
<extensions>true</extensions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<instructions>
|
<instructions>
|
||||||
|
|||||||
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.common;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds the information of the device type and its provider tenant.
|
||||||
|
*/
|
||||||
|
public class DeviceTypeIdentifier implements Serializable {
|
||||||
|
|
||||||
|
private String deviceType;
|
||||||
|
private int tenantId;
|
||||||
|
private static final int DEFAULT_SHARE_WITH_ALL_TENANTS_ID = -1;
|
||||||
|
|
||||||
|
public DeviceTypeIdentifier(String deviceType, int tenantId) {
|
||||||
|
this.deviceType = deviceType;
|
||||||
|
this.tenantId = tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceTypeIdentifier(String deviceType) {
|
||||||
|
this.deviceType = deviceType;
|
||||||
|
this.tenantId = DEFAULT_SHARE_WITH_ALL_TENANTS_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantId(int tenantId) {
|
||||||
|
this.tenantId = tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceType() {
|
||||||
|
return this.deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTenantId() {
|
||||||
|
return this.tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = this.deviceType.hashCode();
|
||||||
|
result = 31 * result + ("@" + this.tenantId).hashCode();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return (obj instanceof DeviceTypeIdentifier) && deviceType.equals(
|
||||||
|
((DeviceTypeIdentifier) obj).deviceType) && tenantId == ((DeviceTypeIdentifier) obj).tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSharedWithAllTenant() {
|
||||||
|
return tenantId == DEFAULT_SHARE_WITH_ALL_TENANTS_ID;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,137 +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.common;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class EmailMessageProperties {
|
|
||||||
|
|
||||||
private String messageBody;
|
|
||||||
private String[] mailTo;
|
|
||||||
private String[] ccList;
|
|
||||||
private String[] bccList;
|
|
||||||
private String subject;
|
|
||||||
private String firstName;
|
|
||||||
private String enrolmentUrl;
|
|
||||||
private String title;
|
|
||||||
private String password;
|
|
||||||
private String userName;
|
|
||||||
private String domainName;
|
|
||||||
|
|
||||||
public String getUserName() {
|
|
||||||
return userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
|
||||||
this.userName = userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDomainName() {
|
|
||||||
return domainName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDomainName(String domainName) {
|
|
||||||
this.domainName = domainName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMessageBody() {
|
|
||||||
return messageBody;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMessageBody(String messageBody) {
|
|
||||||
this.messageBody = messageBody;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] getMailTo() {
|
|
||||||
return mailTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMailTo(String[] mailTo) {
|
|
||||||
this.mailTo = mailTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] getCcList() {
|
|
||||||
return ccList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCcList(String[] ccList) {
|
|
||||||
this.ccList = ccList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] getBccList() {
|
|
||||||
return bccList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBccList(String[] bccList) {
|
|
||||||
this.bccList = bccList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSubject() {
|
|
||||||
return subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubject(String subject) {
|
|
||||||
this.subject = subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFirstName() {
|
|
||||||
return firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEnrolmentUrl() {
|
|
||||||
return enrolmentUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEnrolmentUrl(String enrolmentUrl) {
|
|
||||||
this.enrolmentUrl = enrolmentUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPassword() {
|
|
||||||
return password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPassword(String password) {
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "EmailMessageProperties{" +
|
|
||||||
"messageBody='" + messageBody + '\'' +
|
|
||||||
", mailTo=" + Arrays.toString(mailTo) +
|
|
||||||
", ccList=" + Arrays.toString(ccList) +
|
|
||||||
", bccList=" + Arrays.toString(bccList) +
|
|
||||||
", subject='" + subject + '\'' +
|
|
||||||
", firstName='" + firstName + '\'' +
|
|
||||||
", enrolmentUrl='" + enrolmentUrl + '\'' +
|
|
||||||
", title='" + title + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,10 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
*
|
*
|
||||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
* Version 2.0 (the "License"); you may not use this file except
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
* in compliance with the License.
|
* in compliance with the License.
|
||||||
* you may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.wso2.carbon.device.mgt.common;
|
package org.wso2.carbon.device.mgt.common;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -27,8 +28,11 @@ public class Feature implements Serializable {
|
|||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private String deviceType;
|
private String deviceType;
|
||||||
|
private String method;
|
||||||
|
private String type;
|
||||||
private List<MetadataEntry> metadataEntries;
|
private List<MetadataEntry> metadataEntries;
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@ -37,6 +41,7 @@ public class Feature implements Serializable {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
@ -45,6 +50,7 @@ public class Feature implements Serializable {
|
|||||||
this.code = code;
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -61,6 +67,7 @@ public class Feature implements Serializable {
|
|||||||
this.metadataEntries = metadataEntries;
|
this.metadataEntries = metadataEntries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
public String getDeviceType() {
|
public String getDeviceType() {
|
||||||
return deviceType;
|
return deviceType;
|
||||||
}
|
}
|
||||||
@ -69,6 +76,7 @@ public class Feature implements Serializable {
|
|||||||
this.deviceType = deviceType;
|
this.deviceType = deviceType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
@ -77,6 +85,24 @@ public class Feature implements Serializable {
|
|||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public String getMethod() {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMethod(String method) {
|
||||||
|
this.method = method;
|
||||||
|
}
|
||||||
|
|
||||||
public static class MetadataEntry implements Serializable {
|
public static class MetadataEntry implements Serializable {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
@ -98,5 +124,4 @@ public class Feature implements Serializable {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,8 @@ import java.util.Properties;
|
|||||||
|
|
||||||
public class Application implements Serializable {
|
public class Application implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1998101711L;
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
private String platform;
|
private String platform;
|
||||||
private String category;
|
private String category;
|
||||||
@ -34,6 +36,8 @@ public class Application implements Serializable {
|
|||||||
private Properties appProperties;
|
private Properties appProperties;
|
||||||
private String applicationIdentifier;
|
private String applicationIdentifier;
|
||||||
|
|
||||||
|
private int memoryUsage;
|
||||||
|
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
@ -109,6 +113,14 @@ public class Application implements Serializable {
|
|||||||
this.applicationIdentifier = applicationIdentifier;
|
this.applicationIdentifier = applicationIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getMemoryUsage() {
|
||||||
|
return memoryUsage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMemoryUsage(int memoryUsage) {
|
||||||
|
this.memoryUsage = memoryUsage;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.device.details;
|
||||||
|
|
||||||
|
public class DeviceDetailsMgtException extends Exception{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -315127931134444499L;
|
||||||
|
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,301 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.device.details;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class DeviceInfo implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1998101733L;
|
||||||
|
|
||||||
|
private int deviceId;
|
||||||
|
private String deviceType;
|
||||||
|
private DeviceIdentifier deviceIdentifier;
|
||||||
|
|
||||||
|
private String IMEI;
|
||||||
|
private String IMSI;
|
||||||
|
private String deviceModel;
|
||||||
|
private String vendor;
|
||||||
|
private String osVersion;
|
||||||
|
private Double batteryLevel;
|
||||||
|
private Double internalTotalMemory;
|
||||||
|
private Double internalAvailableMemory;
|
||||||
|
private Double externalTotalMemory;
|
||||||
|
private Double externalAvailableMemory;
|
||||||
|
private String operator;
|
||||||
|
private String connectionType;
|
||||||
|
private Double mobileSignalStrength;
|
||||||
|
private String ssid;
|
||||||
|
private Double cpuUsage;
|
||||||
|
private Double totalRAMMemory;
|
||||||
|
private Double availableRAMMemory;
|
||||||
|
private boolean pluggedIn;
|
||||||
|
|
||||||
|
private Map<String, String> deviceDetailsMap = new HashMap<>();
|
||||||
|
|
||||||
|
public int getDeviceId() {
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceId(int deviceId) {
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceType() {
|
||||||
|
return deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceType(String deviceType) {
|
||||||
|
this.deviceType = deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceIdentifier getDeviceIdentifier() {
|
||||||
|
return deviceIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) {
|
||||||
|
this.deviceIdentifier = deviceIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIMEI() {
|
||||||
|
if (IMEI != null) {
|
||||||
|
return IMEI;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIMEI(String IMEI) {
|
||||||
|
this.IMEI = IMEI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIMSI() {
|
||||||
|
if (IMSI != null) {
|
||||||
|
return IMSI;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIMSI(String IMSI) {
|
||||||
|
this.IMSI = IMSI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceModel() {
|
||||||
|
if (deviceModel != null) {
|
||||||
|
return deviceModel;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceModel(String deviceModel) {
|
||||||
|
this.deviceModel = deviceModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVendor() {
|
||||||
|
if (vendor != null) {
|
||||||
|
return vendor;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVendor(String vendor) {
|
||||||
|
this.vendor = vendor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOsVersion() {
|
||||||
|
if (osVersion != null) {
|
||||||
|
return osVersion;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOsVersion(String osVersion) {
|
||||||
|
this.osVersion = osVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getBatteryLevel() {
|
||||||
|
if (batteryLevel != null) {
|
||||||
|
return batteryLevel;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBatteryLevel(Double batteryLevel) {
|
||||||
|
this.batteryLevel = batteryLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getInternalTotalMemory() {
|
||||||
|
if (internalTotalMemory != null) {
|
||||||
|
return internalTotalMemory;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInternalTotalMemory(Double internalTotalMemory) {
|
||||||
|
this.internalTotalMemory = internalTotalMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getInternalAvailableMemory() {
|
||||||
|
if (internalAvailableMemory != null) {
|
||||||
|
return internalAvailableMemory;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInternalAvailableMemory(Double internalAvailableMemory) {
|
||||||
|
this.internalAvailableMemory = internalAvailableMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getExternalTotalMemory() {
|
||||||
|
if (externalTotalMemory != null) {
|
||||||
|
return externalTotalMemory;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExternalTotalMemory(Double externalTotalMemory) {
|
||||||
|
this.externalTotalMemory = externalTotalMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getExternalAvailableMemory() {
|
||||||
|
if (externalAvailableMemory != null) {
|
||||||
|
return externalAvailableMemory;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExternalAvailableMemory(Double externalAvailableMemory) {
|
||||||
|
this.externalAvailableMemory = externalAvailableMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOperator() {
|
||||||
|
if (operator != null) {
|
||||||
|
return operator;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperator(String operator) {
|
||||||
|
this.operator = operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConnectionType() {
|
||||||
|
if (connectionType != null) {
|
||||||
|
return connectionType;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnectionType(String connectionType) {
|
||||||
|
this.connectionType = connectionType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getMobileSignalStrength() {
|
||||||
|
if (mobileSignalStrength != null) {
|
||||||
|
return mobileSignalStrength;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMobileSignalStrength(Double mobileSignalStrength) {
|
||||||
|
this.mobileSignalStrength = mobileSignalStrength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSsid() {
|
||||||
|
if (ssid != null) {
|
||||||
|
return ssid;
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSsid(String ssid) {
|
||||||
|
this.ssid = ssid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getCpuUsage() {
|
||||||
|
if (cpuUsage != null) {
|
||||||
|
return cpuUsage;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCpuUsage(Double cpuUsage) {
|
||||||
|
this.cpuUsage = cpuUsage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getTotalRAMMemory() {
|
||||||
|
if (totalRAMMemory != null) {
|
||||||
|
return totalRAMMemory;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalRAMMemory(Double totalRAMMemory) {
|
||||||
|
this.totalRAMMemory = totalRAMMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getAvailableRAMMemory() {
|
||||||
|
if (availableRAMMemory != null) {
|
||||||
|
return availableRAMMemory;
|
||||||
|
} else {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAvailableRAMMemory(Double availableRAMMemory) {
|
||||||
|
this.availableRAMMemory = availableRAMMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPluggedIn() {
|
||||||
|
return pluggedIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPluggedIn(boolean pluggedIn) {
|
||||||
|
this.pluggedIn = pluggedIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceDetailsMap(Map<String, String> deviceDetailsMap) {
|
||||||
|
this.deviceDetailsMap = deviceDetailsMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getDeviceDetailsMap() {
|
||||||
|
return deviceDetailsMap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.device.details;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class DeviceLocation implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1998101722L;
|
||||||
|
|
||||||
|
private int deviceId;
|
||||||
|
private DeviceIdentifier deviceIdentifier;
|
||||||
|
private Double latitude;
|
||||||
|
private Double longitude;
|
||||||
|
|
||||||
|
private String street1;
|
||||||
|
private String street2;
|
||||||
|
|
||||||
|
private String city;
|
||||||
|
private String state;
|
||||||
|
private String zip;
|
||||||
|
private String country;
|
||||||
|
|
||||||
|
public int getDeviceId() {
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceId(int deviceId) {
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceIdentifier getDeviceIdentifier() {
|
||||||
|
return deviceIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) {
|
||||||
|
this.deviceIdentifier = deviceIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getLatitude() {
|
||||||
|
return latitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLatitude(Double latitude) {
|
||||||
|
this.latitude = latitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getLongitude() {
|
||||||
|
return longitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLongitude(Double longitude) {
|
||||||
|
this.longitude = longitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStreet1() {
|
||||||
|
return street1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStreet1(String street1) {
|
||||||
|
this.street1 = street1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStreet2() {
|
||||||
|
return street2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStreet2(String street2) {
|
||||||
|
this.street2 = street2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(String state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getZip() {
|
||||||
|
return zip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZip(String zip) {
|
||||||
|
this.zip = zip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCountry() {
|
||||||
|
return country;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCountry(String country) {
|
||||||
|
this.country = country;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.device.details;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
|
||||||
|
public class DeviceWrapper {
|
||||||
|
|
||||||
|
private Device device;
|
||||||
|
private DeviceIdentifier deviceIdentifier;
|
||||||
|
private DeviceInfo deviceInfo;
|
||||||
|
private DeviceLocation deviceLocation;
|
||||||
|
|
||||||
|
public Device getDevice() {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDevice(Device device) {
|
||||||
|
this.device = device;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceIdentifier getDeviceIdentifier() {
|
||||||
|
return deviceIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) {
|
||||||
|
this.deviceIdentifier = deviceIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceInfo getDeviceInfo() {
|
||||||
|
return deviceInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceInfo(DeviceInfo deviceInfo) {
|
||||||
|
this.deviceInfo = deviceInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceLocation getDeviceLocation() {
|
||||||
|
return deviceLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceLocation(DeviceLocation deviceLocation) {
|
||||||
|
this.deviceLocation = deviceLocation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.group.mgt;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds Device Group details and expose to external access
|
||||||
|
*/
|
||||||
|
@XmlRootElement
|
||||||
|
public class DeviceGroup implements Serializable {
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
private String name;
|
||||||
|
private Long dateOfCreation;
|
||||||
|
private Long dateOfLastUpdate;
|
||||||
|
private String owner;
|
||||||
|
private List<GroupUser> users;
|
||||||
|
private List<String> roles;
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public Long getDateOfCreation() {
|
||||||
|
return dateOfCreation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateOfCreation(Long dateOfCreation) {
|
||||||
|
this.dateOfCreation = dateOfCreation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public Long getDateOfLastUpdate() {
|
||||||
|
return dateOfLastUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateOfLastUpdate(Long dateOfLastUpdate) {
|
||||||
|
this.dateOfLastUpdate = dateOfLastUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public String getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOwner(String owner) {
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public List<GroupUser> getUsers() {
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setUsers(List<GroupUser> users) {
|
||||||
|
this.users = users;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public List<String> getRoles() {
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setRoles(List<String> roles) {
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DeviceGroup getGroup() {
|
||||||
|
DeviceGroup deviceGroup = new DeviceGroup();
|
||||||
|
deviceGroup.setDescription(getDescription());
|
||||||
|
deviceGroup.setName(getName());
|
||||||
|
deviceGroup.setDateOfCreation(getDateOfCreation());
|
||||||
|
deviceGroup.setDateOfLastUpdate(getDateOfLastUpdate());
|
||||||
|
deviceGroup.setOwner(getOwner());
|
||||||
|
deviceGroup.setUsers(getUsers());
|
||||||
|
deviceGroup.setRoles(getRoles());
|
||||||
|
return deviceGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.group.mgt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a custom exception specified for group management
|
||||||
|
*/
|
||||||
|
public class GroupAlreadyEixistException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -312678379574816874L;
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public GroupAlreadyEixistException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupAlreadyEixistException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupAlreadyEixistException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupAlreadyEixistException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupAlreadyEixistException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.group.mgt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a custom exception specified for group management
|
||||||
|
*/
|
||||||
|
public class GroupManagementException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -312678248574816874L;
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public GroupManagementException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupManagementException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupManagementException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupManagementException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public GroupManagementException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.group.mgt;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds Device Group user name and assigned group roles of user. Exposed to external access
|
||||||
|
*/
|
||||||
|
@XmlRootElement
|
||||||
|
public class GroupUser implements Serializable {
|
||||||
|
private String username;
|
||||||
|
private List<String> groupRoles;
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement
|
||||||
|
public List<String> getGroupRoles() {
|
||||||
|
return groupRoles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGroupRoles(List<String> groupRoles) {
|
||||||
|
this.groupRoles = groupRoles;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.search;
|
||||||
|
|
||||||
|
public class Condition {
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
private String value;
|
||||||
|
public String operator;
|
||||||
|
|
||||||
|
private State state;
|
||||||
|
|
||||||
|
public enum State {
|
||||||
|
AND, OR;
|
||||||
|
};
|
||||||
|
|
||||||
|
public State getState(){
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(State state){
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOperator() {
|
||||||
|
return operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperator(String operator) {
|
||||||
|
this.operator = operator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.common.search;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SearchContext {
|
||||||
|
|
||||||
|
private List<Condition> conditions;
|
||||||
|
// private int start;
|
||||||
|
// private int end;
|
||||||
|
|
||||||
|
public List<Condition> getConditions() {
|
||||||
|
return conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConditions(List<Condition> conditions) {
|
||||||
|
this.conditions = conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public int getStart() {
|
||||||
|
// return start;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setStart(int start) {
|
||||||
|
// this.start = start;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int getEnd() {
|
||||||
|
// return end;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void setEnd(int end) {
|
||||||
|
// this.end = end;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
@ -39,6 +39,19 @@ public interface DeviceManagementService extends ApplicationManager {
|
|||||||
*/
|
*/
|
||||||
String getType();
|
String getType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This returns the tenant domain of the provider.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getProviderTenantDomain();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns true if the device type is shared between all tenants and false if its not.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean isSharedWithAllTenants();
|
||||||
|
|
||||||
void init() throws DeviceManagementException;
|
void init() throws DeviceManagementException;
|
||||||
|
|
||||||
DeviceManager getDeviceManager();
|
DeviceManager getDeviceManager();
|
||||||
|
|||||||
@ -18,17 +18,16 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<artifactId>device-mgt</artifactId>
|
<artifactId>device-mgt</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
|
||||||
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
||||||
<version>1.0.4-SNAPSHOT</version>
|
|
||||||
<packaging>bundle</packaging>
|
<packaging>bundle</packaging>
|
||||||
<name>WSO2 Carbon - Device Management Core</name>
|
<name>WSO2 Carbon - Device Management Core</name>
|
||||||
<description>WSO2 Carbon - Device Management Core</description>
|
<description>WSO2 Carbon - Device Management Core</description>
|
||||||
@ -43,7 +42,6 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.felix</groupId>
|
<groupId>org.apache.felix</groupId>
|
||||||
<artifactId>maven-bundle-plugin</artifactId>
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
<version>1.4.0</version>
|
|
||||||
<extensions>true</extensions>
|
<extensions>true</extensions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<instructions>
|
<instructions>
|
||||||
@ -66,7 +64,6 @@
|
|||||||
org.wso2.carbon.context,
|
org.wso2.carbon.context,
|
||||||
org.wso2.carbon.core,
|
org.wso2.carbon.core,
|
||||||
org.wso2.carbon.utils.*,
|
org.wso2.carbon.utils.*,
|
||||||
org.wso2.carbon.registry.api,
|
|
||||||
org.wso2.carbon.device.mgt.common.*,
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
org.wso2.carbon.user.api,
|
org.wso2.carbon.user.api,
|
||||||
org.wso2.carbon.user.core.*,
|
org.wso2.carbon.user.core.*,
|
||||||
@ -80,10 +77,14 @@
|
|||||||
org.wso2.carbon.identity.oauth.stub.dto,
|
org.wso2.carbon.identity.oauth.stub.dto,
|
||||||
org.wso2.carbon.ndatasource.core,
|
org.wso2.carbon.ndatasource.core,
|
||||||
org.wso2.carbon.apimgt.impl,
|
org.wso2.carbon.apimgt.impl,
|
||||||
org.wso2.carbon.ndatasource.core,
|
org.wso2.carbon.ntask.core.*,
|
||||||
org.apache.axis2.transport.mail,
|
org.wso2.carbon.ntask.common,
|
||||||
org.apache.catalina,
|
org.apache.catalina,
|
||||||
org.apache.catalina.core
|
org.apache.catalina.core,
|
||||||
|
org.apache.commons.collections,
|
||||||
|
org.wso2.carbon.email.sender.*,
|
||||||
|
org.wso2.carbon,
|
||||||
|
org.wso2.carbon.base
|
||||||
</Import-Package>
|
</Import-Package>
|
||||||
<Export-Package>
|
<Export-Package>
|
||||||
!org.wso2.carbon.device.mgt.core.internal,
|
!org.wso2.carbon.device.mgt.core.internal,
|
||||||
@ -92,11 +93,9 @@
|
|||||||
</instructions>
|
</instructions>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>2.18</version>
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<systemPropertyVariables>
|
<systemPropertyVariables>
|
||||||
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
|
<log4j.configuration>file:src/test/resources/log4j.properties</log4j.configuration>
|
||||||
@ -106,7 +105,6 @@
|
|||||||
</suiteXmlFiles>
|
</suiteXmlFiles>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
@ -216,6 +214,37 @@
|
|||||||
<groupId>org.wso2.tomcat</groupId>
|
<groupId>org.wso2.tomcat</groupId>
|
||||||
<artifactId>tomcat-servlet-api</artifactId>
|
<artifactId>tomcat-servlet-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--Ntask dependencies-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.commons</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ntask.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-collections.wso2</groupId>
|
||||||
|
<artifactId>commons-collections</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.email.sender.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.34</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>1.7.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@ -58,4 +58,21 @@ public final class DeviceManagementConstants {
|
|||||||
public static final String MONITOR_OPERATION_CODE = "MONITOR";
|
public static final String MONITOR_OPERATION_CODE = "MONITOR";
|
||||||
public static final String POLICY_OPERATION_CODE = PolicyOperation.POLICY_OPERATION_CODE;
|
public static final String POLICY_OPERATION_CODE = PolicyOperation.POLICY_OPERATION_CODE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class EmailAttributes {
|
||||||
|
private EmailAttributes() {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String ENCODED_SCHEME = "UTF-8";
|
||||||
|
public static final String FIRST_NAME = "first-name";
|
||||||
|
public static final String USERNAME = "username";
|
||||||
|
public static final String PASSWORD = "password";
|
||||||
|
public static final String DOMAIN = "domain-name";
|
||||||
|
|
||||||
|
public static final String SERVER_BASE_URL_HTTPS = "base-url-https";
|
||||||
|
public static final String SERVER_BASE_URL_HTTP = "base-url-http";
|
||||||
|
public static final String DOWNLOAD_URL = "download-url";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core;
|
|||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier;
|
||||||
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
||||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
|
||||||
@ -32,23 +33,29 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class DeviceManagementPluginRepository implements DeviceManagerStartupListener {
|
public class DeviceManagementPluginRepository implements DeviceManagerStartupListener {
|
||||||
|
|
||||||
private Map<String, DeviceManagementService> providers;
|
private Map<DeviceTypeIdentifier, DeviceManagementService> providers;
|
||||||
private boolean isInited;
|
private boolean isInited;
|
||||||
private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class);
|
private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class);
|
||||||
|
|
||||||
public DeviceManagementPluginRepository() {
|
public DeviceManagementPluginRepository() {
|
||||||
providers = Collections.synchronizedMap(new HashMap<String, DeviceManagementService>());
|
providers = Collections.synchronizedMap(new HashMap<DeviceTypeIdentifier, DeviceManagementService>());
|
||||||
DeviceManagementServiceComponent.registerStartupListener(this);
|
DeviceManagementServiceComponent.registerStartupListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
|
public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
|
||||||
String deviceType = provider.getType();
|
String deviceType = provider.getType();
|
||||||
|
String tenantDomain = provider.getProviderTenantDomain();
|
||||||
|
boolean isSharedWithAllTenants = provider.isSharedWithAllTenants();
|
||||||
|
int tenantId = DeviceManagerUtil.getTenantId(tenantDomain);
|
||||||
|
if (tenantId == -1) {
|
||||||
|
throw new DeviceManagementException("No tenant available for tenant domain " + tenantDomain);
|
||||||
|
}
|
||||||
synchronized (providers) {
|
synchronized (providers) {
|
||||||
try {
|
try {
|
||||||
if (isInited) {
|
if (isInited) {
|
||||||
/* Initializing Device Management Service Provider */
|
/* Initializing Device Management Service Provider */
|
||||||
provider.init();
|
provider.init();
|
||||||
DeviceManagerUtil.registerDeviceType(deviceType);
|
DeviceManagerUtil.registerDeviceType(deviceType, tenantId, isSharedWithAllTenants);
|
||||||
DeviceManagementDataHolder.getInstance().setRequireDeviceAuthorization(deviceType,
|
DeviceManagementDataHolder.getInstance().setRequireDeviceAuthorization(deviceType,
|
||||||
provider.getDeviceManager().requireDeviceAuthorization());
|
provider.getDeviceManager().requireDeviceAuthorization());
|
||||||
|
|
||||||
@ -57,20 +64,47 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
|
|||||||
throw new DeviceManagementException("Error occurred while adding device management provider '" +
|
throw new DeviceManagementException("Error occurred while adding device management provider '" +
|
||||||
deviceType + "'", e);
|
deviceType + "'", e);
|
||||||
}
|
}
|
||||||
providers.put(deviceType, provider);
|
if (isSharedWithAllTenants) {
|
||||||
|
DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType);
|
||||||
|
providers.put(deviceTypeIdentifier, provider);
|
||||||
|
} else {
|
||||||
|
DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType, tenantId);
|
||||||
|
providers.put(deviceTypeIdentifier, provider);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
|
public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
|
||||||
providers.remove(provider.getType());
|
String deviceTypeName=provider.getType();
|
||||||
|
if(provider.isSharedWithAllTenants()){
|
||||||
|
DeviceTypeIdentifier deviceTypeIdentifier =new DeviceTypeIdentifier(deviceTypeName);
|
||||||
|
providers.remove(deviceTypeIdentifier);
|
||||||
|
}else{
|
||||||
|
int providerTenantId=DeviceManagerUtil.getTenantId(provider.getProviderTenantDomain());
|
||||||
|
DeviceTypeIdentifier deviceTypeIdentifier =new DeviceTypeIdentifier(deviceTypeName, providerTenantId);
|
||||||
|
providers.remove(deviceTypeIdentifier);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DeviceManagementService getDeviceManagementService(String type) {
|
public DeviceManagementService getDeviceManagementService(String type, int tenantId) {
|
||||||
return providers.get(type);
|
//Priority need to be given to the tenant before public.
|
||||||
|
DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(type, tenantId);
|
||||||
|
DeviceManagementService provider = providers.get(deviceTypeIdentifier);
|
||||||
|
if (provider == null) {
|
||||||
|
deviceTypeIdentifier = new DeviceTypeIdentifier(type);
|
||||||
|
provider = providers.get(deviceTypeIdentifier);
|
||||||
|
}
|
||||||
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, DeviceManagementService> getAllDeviceManagementServices() {
|
public Map<DeviceTypeIdentifier, DeviceManagementService> getAllDeviceManagementServices(int tenantId) {
|
||||||
return providers;
|
Map<DeviceTypeIdentifier, DeviceManagementService> tenantProviders = new HashMap<>();
|
||||||
|
for (DeviceTypeIdentifier identifier : providers.keySet()) {
|
||||||
|
if (identifier.getTenantId() == tenantId || identifier.isSharedWithAllTenant()) {
|
||||||
|
tenantProviders.put(identifier, providers.get(identifier));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tenantProviders;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -79,7 +113,8 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
|
|||||||
for (DeviceManagementService provider : providers.values()) {
|
for (DeviceManagementService provider : providers.values()) {
|
||||||
try {
|
try {
|
||||||
provider.init();
|
provider.init();
|
||||||
DeviceManagerUtil.registerDeviceType(provider.getType());
|
int tenantId=DeviceManagerUtil.getTenantId(provider.getProviderTenantDomain());
|
||||||
|
DeviceManagerUtil.registerDeviceType(provider.getType(), tenantId, provider.isSharedWithAllTenants());
|
||||||
//TODO:
|
//TODO:
|
||||||
//This is a temporory fix.
|
//This is a temporory fix.
|
||||||
//windows and IOS cannot resolve user info by extracting certs
|
//windows and IOS cannot resolve user info by extracting certs
|
||||||
|
|||||||
@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.core.config;
|
|||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
import org.wso2.carbon.device.mgt.core.config.email.NotificationMessagesConfig;
|
|
||||||
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
||||||
import org.wso2.carbon.utils.CarbonUtils;
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
@ -35,21 +34,17 @@ import javax.xml.validation.SchemaFactory;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class responsible for the cdm manager configuration initialization.
|
* Class responsible for the cdm configuration initialization.
|
||||||
*/
|
*/
|
||||||
public class DeviceConfigurationManager {
|
public class DeviceConfigurationManager {
|
||||||
|
|
||||||
private DeviceManagementConfig currentDeviceConfig;
|
private DeviceManagementConfig currentDeviceConfig;
|
||||||
private NotificationMessagesConfig notificationMessagesConfig;
|
|
||||||
private static DeviceConfigurationManager deviceConfigManager;
|
private static DeviceConfigurationManager deviceConfigManager;
|
||||||
|
|
||||||
private static final String DEVICE_MGT_CONFIG_PATH =
|
private static final String DEVICE_MGT_CONFIG_PATH =
|
||||||
CarbonUtils.getCarbonConfigDirPath() + File.separator +
|
CarbonUtils.getCarbonConfigDirPath() + File.separator +
|
||||||
DeviceManagementConstants.DataSourceProperties.DEVICE_CONFIG_XML_NAME;
|
DeviceManagementConstants.DataSourceProperties.DEVICE_CONFIG_XML_NAME;
|
||||||
private static final String DEVICE_MGT_CONFIG_SCHEMA_PATH = "resources/config/schema/device-mgt-config-schema.xsd";
|
private static final String DEVICE_MGT_CONFIG_SCHEMA_PATH = "resources/config/schema/device-mgt-config-schema.xsd";
|
||||||
private static final String NOTIFICATION_MSG_CONFIG_PATH =
|
|
||||||
CarbonUtils.getCarbonConfigDirPath() + File.separator +
|
|
||||||
DeviceManagementConstants.NotificationProperties.NOTIFICATION_CONFIG_FILE;
|
|
||||||
|
|
||||||
public static DeviceConfigurationManager getInstance() {
|
public static DeviceConfigurationManager getInstance() {
|
||||||
if (deviceConfigManager == null) {
|
if (deviceConfigManager == null) {
|
||||||
@ -63,7 +58,6 @@ public class DeviceConfigurationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void initConfig() throws DeviceManagementException {
|
public synchronized void initConfig() throws DeviceManagementException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File deviceMgtConfig = new File(DeviceConfigurationManager.DEVICE_MGT_CONFIG_PATH);
|
File deviceMgtConfig = new File(DeviceConfigurationManager.DEVICE_MGT_CONFIG_PATH);
|
||||||
Document doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig);
|
Document doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig);
|
||||||
@ -76,18 +70,6 @@ public class DeviceConfigurationManager {
|
|||||||
} catch (JAXBException e) {
|
} catch (JAXBException e) {
|
||||||
throw new DeviceManagementException("Error occurred while initializing Data Source config", e);
|
throw new DeviceManagementException("Error occurred while initializing Data Source config", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
File notificationConfig = new File(DeviceConfigurationManager.NOTIFICATION_MSG_CONFIG_PATH);
|
|
||||||
Document doc = DeviceManagerUtil.convertToDocument(notificationConfig);
|
|
||||||
|
|
||||||
/* Un-marshaling Notifications Management configuration */
|
|
||||||
JAXBContext notificationContext = JAXBContext.newInstance(NotificationMessagesConfig.class);
|
|
||||||
Unmarshaller unmarshaller = notificationContext.createUnmarshaller();
|
|
||||||
this.notificationMessagesConfig = (NotificationMessagesConfig) unmarshaller.unmarshal(doc);
|
|
||||||
} catch(JAXBException e){
|
|
||||||
throw new DeviceManagementException("Error occurred while initializing Notification settings config", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Schema getSchema() throws DeviceManagementException {
|
private static Schema getSchema() throws DeviceManagementException {
|
||||||
@ -105,8 +87,4 @@ public class DeviceConfigurationManager {
|
|||||||
return currentDeviceConfig;
|
return currentDeviceConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NotificationMessagesConfig getNotificationMessagesConfig() {
|
|
||||||
return notificationMessagesConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,9 +18,9 @@
|
|||||||
package org.wso2.carbon.device.mgt.core.config;
|
package org.wso2.carbon.device.mgt.core.config;
|
||||||
|
|
||||||
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
||||||
import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations;
|
|
||||||
import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations;
|
import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations;
|
||||||
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
|
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
@ -31,8 +31,10 @@ import javax.xml.bind.annotation.XmlRootElement;
|
|||||||
@XmlRootElement(name = "ManagementRepository")
|
@XmlRootElement(name = "ManagementRepository")
|
||||||
public class DeviceManagementConfigRepository {
|
public class DeviceManagementConfigRepository {
|
||||||
|
|
||||||
|
|
||||||
|
// private EmailConfigurations emailConfigurations;
|
||||||
|
private TaskConfiguration taskConfiguration;
|
||||||
private DataSourceConfig dataSourceConfig;
|
private DataSourceConfig dataSourceConfig;
|
||||||
private EmailConfigurations emailConfigurations;
|
|
||||||
private IdentityConfigurations identityConfigurations;
|
private IdentityConfigurations identityConfigurations;
|
||||||
private PolicyConfiguration policyConfiguration;
|
private PolicyConfiguration policyConfiguration;
|
||||||
|
|
||||||
@ -45,20 +47,21 @@ public class DeviceManagementConfigRepository {
|
|||||||
this.dataSourceConfig = dataSourceConfig;
|
this.dataSourceConfig = dataSourceConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
@XmlElement(name = "EmailClientConfiguration", required = true)
|
// @XmlElement(name = "EmailClientConfiguration", required = true)
|
||||||
public EmailConfigurations getEmailConfigurations() {
|
// public EmailConfigurations getEmailConfigurations() {
|
||||||
return emailConfigurations;
|
// return emailConfigurations;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void setEmailConfigurations(EmailConfigurations emailConfigurations) {
|
// public void setEmailConfigurations(EmailConfigurations emailConfigurations) {
|
||||||
this.emailConfigurations = emailConfigurations;
|
// this.emailConfigurations = emailConfigurations;
|
||||||
}
|
// }
|
||||||
|
|
||||||
@XmlElement(name = "IdentityConfiguration", required = true)
|
@XmlElement(name = "IdentityConfiguration", required = true)
|
||||||
public IdentityConfigurations getIdentityConfigurations() {
|
public IdentityConfigurations getIdentityConfigurations() {
|
||||||
return identityConfigurations;
|
return identityConfigurations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setIdentityConfigurations(IdentityConfigurations identityConfigurations) {
|
public void setIdentityConfigurations(IdentityConfigurations identityConfigurations) {
|
||||||
this.identityConfigurations = identityConfigurations;
|
this.identityConfigurations = identityConfigurations;
|
||||||
}
|
}
|
||||||
@ -71,4 +74,13 @@ public class DeviceManagementConfigRepository {
|
|||||||
public void setPolicyConfiguration(PolicyConfiguration policyConfiguration) {
|
public void setPolicyConfiguration(PolicyConfiguration policyConfiguration) {
|
||||||
this.policyConfiguration = policyConfiguration;
|
this.policyConfiguration = policyConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "TaskConfiguration", required = true)
|
||||||
|
public TaskConfiguration getTaskConfiguration() {
|
||||||
|
return taskConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTaskConfiguration(TaskConfiguration taskConfiguration) {
|
||||||
|
this.taskConfiguration = taskConfiguration;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,85 +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.core.config.email;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@XmlRootElement(name = "EmailClientConfiguration")
|
|
||||||
public class EmailConfigurations {
|
|
||||||
|
|
||||||
private int minNumOfThread;
|
|
||||||
private int maxNumOfThread;
|
|
||||||
private int keepAliveTime;
|
|
||||||
private int threadQueueCapacity;
|
|
||||||
private String lBHostPortPrefix;
|
|
||||||
private String enrollmentContextPath;
|
|
||||||
|
|
||||||
@XmlElement(name = "minimumThread", required = true)
|
|
||||||
public int getMinNumOfThread() {
|
|
||||||
return minNumOfThread;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMinNumOfThread(int minNumOfThread) {
|
|
||||||
this.minNumOfThread = minNumOfThread;
|
|
||||||
}
|
|
||||||
@XmlElement(name = "maximumThread", required = true)
|
|
||||||
public int getMaxNumOfThread() {
|
|
||||||
return maxNumOfThread;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMaxNumOfThread(int maxNumOfThread) {
|
|
||||||
this.maxNumOfThread = maxNumOfThread;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "keepAliveTime", required = true)
|
|
||||||
public int getKeepAliveTime() {
|
|
||||||
return keepAliveTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setKeepAliveTime(int keepAliveTime) {
|
|
||||||
this.keepAliveTime = keepAliveTime;
|
|
||||||
}
|
|
||||||
@XmlElement(name = "ThreadQueueCapacity", required = true)
|
|
||||||
public int getThreadQueueCapacity() {
|
|
||||||
return threadQueueCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setThreadQueueCapacity(int threadQueueCapacity) {
|
|
||||||
this.threadQueueCapacity = threadQueueCapacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "LBHostPortPrefix", required = true)
|
|
||||||
public String getlBHostPortPrefix() {
|
|
||||||
return lBHostPortPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setlBHostPortPrefix(String lBHostPortPrefix) {
|
|
||||||
this.lBHostPortPrefix = lBHostPortPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "enrollmentContextPath", required = true)
|
|
||||||
public String getEnrollmentContextPath() {
|
|
||||||
return enrollmentContextPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEnrollmentContextPath(String enrollmentContextPath) {
|
|
||||||
this.enrollmentContextPath = enrollmentContextPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,110 +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.core.config.email;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@XmlRootElement(name = "NotificationMessage")
|
|
||||||
public class NotificationMessages {
|
|
||||||
|
|
||||||
private String header;
|
|
||||||
private String body;
|
|
||||||
private String footerLine1;
|
|
||||||
private String footerLine2;
|
|
||||||
private String footerLine3;
|
|
||||||
private String subject;
|
|
||||||
private String url;
|
|
||||||
|
|
||||||
@XmlAttribute(name = "type")
|
|
||||||
public String getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setType(String type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String type;
|
|
||||||
|
|
||||||
@XmlElement(name = "Header", required = true)
|
|
||||||
public String getHeader() {
|
|
||||||
return header;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setHeader(String header) {
|
|
||||||
this.header = header;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "Body", required = true)
|
|
||||||
public String getBody() {
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBody(String body) {
|
|
||||||
this.body = body;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "Subject", required = true)
|
|
||||||
public String getSubject() {
|
|
||||||
return subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubject(String subject) {
|
|
||||||
this.subject = subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "Url", required = true)
|
|
||||||
public String getUrl() {
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUrl(String url) {
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "Footer1")
|
|
||||||
public String getFooterLine1() {
|
|
||||||
return footerLine1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFooterLine1(String footerLine1) {
|
|
||||||
this.footerLine1 = footerLine1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "Footer2")
|
|
||||||
public String getFooterLine2() {
|
|
||||||
return footerLine2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFooterLine2(String footerLine2) {
|
|
||||||
this.footerLine2 = footerLine2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "Footer3")
|
|
||||||
public String getFooterLine3() {
|
|
||||||
return footerLine3;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFooterLine3(String footerLine3) {
|
|
||||||
this.footerLine3 = footerLine3;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will read the configurations related to task. This task will be responsible for adding the operations.
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.core.config.task;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "TaskConfiguration")
|
||||||
|
public class TaskConfiguration {
|
||||||
|
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
private int frequency;
|
||||||
|
private String taskClazz;
|
||||||
|
private List<Operation> operations;
|
||||||
|
|
||||||
|
@XmlElement(name = "Enable", required = true)
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Frequency", required = true)
|
||||||
|
public int getFrequency() {
|
||||||
|
return frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFrequency(int frequency) {
|
||||||
|
this.frequency = frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "TaskClass", required = true)
|
||||||
|
public String getTaskClazz() {
|
||||||
|
return taskClazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTaskClazz(String taskClazz) {
|
||||||
|
this.taskClazz = taskClazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElementWrapper(name="Operations")
|
||||||
|
@XmlElement(name = "Operation", required = true)
|
||||||
|
public List<Operation> getOperations() {
|
||||||
|
return operations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperations(List<Operation> operations) {
|
||||||
|
this.operations = operations;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlRootElement(name = "Operation")
|
||||||
|
public static class Operation {
|
||||||
|
|
||||||
|
|
||||||
|
private String operationName;
|
||||||
|
private int recurrency;
|
||||||
|
|
||||||
|
@XmlElement(name = "Name", required = true)
|
||||||
|
public String getOperationName() {
|
||||||
|
return operationName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperationName(String operationName) {
|
||||||
|
this.operationName = operationName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "RecurrentTimes", required = true)
|
||||||
|
public int getRecurrency() {
|
||||||
|
return recurrency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecurrency(int recurrency) {
|
||||||
|
this.recurrency = recurrency;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -32,6 +32,10 @@ import org.wso2.carbon.device.mgt.core.dao.impl.device.OracleDeviceDAOImpl;
|
|||||||
import org.wso2.carbon.device.mgt.core.dao.impl.device.PostgreSQLDeviceDAOImpl;
|
import org.wso2.carbon.device.mgt.core.dao.impl.device.PostgreSQLDeviceDAOImpl;
|
||||||
import org.wso2.carbon.device.mgt.core.dao.impl.device.SQLServerDeviceDAOImpl;
|
import org.wso2.carbon.device.mgt.core.dao.impl.device.SQLServerDeviceDAOImpl;
|
||||||
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.impl.DeviceDetailsDAOImpl;
|
||||||
|
import org.wso2.carbon.device.mgt.core.search.mgt.dao.SearchDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.core.search.mgt.dao.impl.SearchDAOImpl;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@ -127,6 +131,14 @@ public class DeviceManagementDAOFactory {
|
|||||||
return new ApplicationMappingDAOImpl();
|
return new ApplicationMappingDAOImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DeviceDetailsDAO getDeviceDetailsDAO() {
|
||||||
|
return new DeviceDetailsDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SearchDAO getSearchDAO() {
|
||||||
|
return new SearchDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
public static void init(DataSourceConfig config) {
|
public static void init(DataSourceConfig config) {
|
||||||
dataSource = resolveDataSource(config);
|
dataSource = resolveDataSource(config);
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -27,16 +27,64 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface DeviceTypeDAO {
|
public interface DeviceTypeDAO {
|
||||||
|
|
||||||
void addDeviceType(DeviceType deviceType) throws DeviceManagementDAOException;
|
/**
|
||||||
|
* @param deviceType device that needs to be added
|
||||||
|
* @param providerTenantId provider tenant id whom the device type is associated with.
|
||||||
|
* @param isSharedWithAllTenants is this a shared device type or not.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
void addDeviceType(DeviceType deviceType, int providerTenantId, boolean isSharedWithAllTenants)
|
||||||
|
throws DeviceManagementDAOException;
|
||||||
|
|
||||||
void updateDeviceType(DeviceType deviceType) throws DeviceManagementDAOException;
|
/**
|
||||||
|
* @param deviceType deviceType that needs to be updated.
|
||||||
|
* @param providerTenantId provider tenant id whom the device type is associated with.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
void updateDeviceType(DeviceType deviceType, int providerTenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException;
|
/**
|
||||||
|
* @param tenantId get device type detail of a specific tenant.
|
||||||
|
* @return list of all device types that are associated with the tenant this includes the shared device types.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
List<DeviceType> getDeviceTypes(int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param tenandId of the device type provider.
|
||||||
|
* @return return only the device types that are associated with the provider tenant.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
List<DeviceType> getDeviceTypesByProvider(int tenandId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return sharedWithAllDeviceTypes This returns public shared device types.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
List<DeviceType> getSharedDeviceTypes() throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param id retrieve the device type with its id.
|
||||||
|
* @return the device type associated with the id.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
DeviceType getDeviceType(int id) throws DeviceManagementDAOException;
|
DeviceType getDeviceType(int id) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
DeviceType getDeviceType(String name) throws DeviceManagementDAOException;
|
/**
|
||||||
|
* @param name retreive the device type with it name.
|
||||||
|
* @param tenantId retreive the device type with its tenant id.
|
||||||
|
* @return the device type associated with its name and tenant id.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
DeviceType getDeviceType(String name, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
void removeDeviceType(String type) throws DeviceManagementDAOException;
|
/**
|
||||||
|
* remove the device type from tenant.
|
||||||
|
*
|
||||||
|
* @param name remove the device type with it name.
|
||||||
|
* @param tenantId remove the device type with its tenant id.
|
||||||
|
* @throws DeviceManagementDAOException
|
||||||
|
*/
|
||||||
|
void removeDeviceType(String name, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,8 +47,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
|
|||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " +
|
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " +
|
||||||
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES,APP_IDENTIFIER) " +
|
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID, APP_PROPERTIES, APP_IDENTIFIER, MEMORY_USAGE) " +
|
||||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)");
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
|
|
||||||
stmt.setString(1, application.getName());
|
stmt.setString(1, application.getName());
|
||||||
stmt.setString(2, application.getPlatform());
|
stmt.setString(2, application.getPlatform());
|
||||||
@ -65,6 +65,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
|
|||||||
stmt.setBytes(9, bao.toByteArray());
|
stmt.setBytes(9, bao.toByteArray());
|
||||||
|
|
||||||
stmt.setString(10, application.getApplicationIdentifier());
|
stmt.setString(10, application.getApplicationIdentifier());
|
||||||
|
stmt.setInt(11, application.getMemoryUsage());
|
||||||
stmt.execute();
|
stmt.execute();
|
||||||
|
|
||||||
rs = stmt.getGeneratedKeys();
|
rs = stmt.getGeneratedKeys();
|
||||||
@ -108,8 +109,8 @@ public class ApplicationDAOImpl implements ApplicationDAO {
|
|||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " +
|
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " +
|
||||||
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES,APP_IDENTIFIER) " +
|
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES, APP_IDENTIFIER, MEMORY_USAGE) " +
|
||||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)", new String[] {"id"});
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new String[]{"id"});
|
||||||
|
|
||||||
|
|
||||||
for (Application application : applications) {
|
for (Application application : applications) {
|
||||||
@ -129,6 +130,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
|
|||||||
stmt.setBytes(9, bao.toByteArray());
|
stmt.setBytes(9, bao.toByteArray());
|
||||||
|
|
||||||
stmt.setString(10, application.getApplicationIdentifier());
|
stmt.setString(10, application.getApplicationIdentifier());
|
||||||
|
stmt.setInt(11, application.getMemoryUsage());
|
||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
|
|
||||||
rs = stmt.getGeneratedKeys();
|
rs = stmt.getGeneratedKeys();
|
||||||
@ -206,7 +208,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
|
|||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " +
|
stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " +
|
||||||
"LOCATION_URL, IMAGE_URL, APP_PROPERTIES, TENANT_ID FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? " +
|
"LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, TENANT_ID FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? " +
|
||||||
"AND TENANT_ID = ?");
|
"AND TENANT_ID = ?");
|
||||||
stmt.setString(1, identifier);
|
stmt.setString(1, identifier);
|
||||||
stmt.setInt(2, tenantId);
|
stmt.setInt(2, tenantId);
|
||||||
@ -238,7 +240,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
|
|||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
stmt = conn.prepareStatement("Select ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " +
|
stmt = conn.prepareStatement("Select ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " +
|
||||||
"LOCATION_URL, IMAGE_URL, APP_PROPERTIES, TENANT_ID From DM_APPLICATION app " +
|
"LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, TENANT_ID From DM_APPLICATION app " +
|
||||||
"INNER JOIN " +
|
"INNER JOIN " +
|
||||||
"(Select APPLICATION_ID From DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID=?) APPMAP " +
|
"(Select APPLICATION_ID From DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID=?) APPMAP " +
|
||||||
"ON " +
|
"ON " +
|
||||||
@ -284,6 +286,7 @@ public class ApplicationDAOImpl implements ApplicationDAO {
|
|||||||
application.setLocationUrl(rs.getString("LOCATION_URL"));
|
application.setLocationUrl(rs.getString("LOCATION_URL"));
|
||||||
application.setPlatform(rs.getString("PLATFORM"));
|
application.setPlatform(rs.getString("PLATFORM"));
|
||||||
application.setVersion(rs.getString("VERSION"));
|
application.setVersion(rs.getString("VERSION"));
|
||||||
|
application.setMemoryUsage(rs.getInt("MEMORY_USAGE"));
|
||||||
application.setApplicationIdentifier(rs.getString("APP_IDENTIFIER"));
|
application.setApplicationIdentifier(rs.getString("APP_IDENTIFIER"));
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@ -33,36 +33,99 @@ import java.util.List;
|
|||||||
public class DeviceTypeDAOImpl implements DeviceTypeDAO {
|
public class DeviceTypeDAOImpl implements DeviceTypeDAO {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addDeviceType(DeviceType deviceType) throws DeviceManagementDAOException {
|
public void addDeviceType(DeviceType deviceType, int providerTenantId, boolean isSharedWithAllTenants)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
PreparedStatement stmt = null;
|
PreparedStatement stmt = null;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_TYPE (NAME) VALUES (?)");
|
stmt = conn.prepareStatement(
|
||||||
|
"INSERT INTO DM_DEVICE_TYPE (NAME,PROVIDER_TENANT_ID,SHARED_WITH_ALL_TENANTS) VALUES (?,?,?)");
|
||||||
stmt.setString(1, deviceType.getName());
|
stmt.setString(1, deviceType.getName());
|
||||||
|
stmt.setInt(2, providerTenantId);
|
||||||
|
stmt.setBoolean(3, isSharedWithAllTenants);
|
||||||
stmt.execute();
|
stmt.execute();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DeviceManagementDAOException("Error occurred while registering the device type " +
|
throw new DeviceManagementDAOException(
|
||||||
"'" + deviceType.getName() + "'", e);
|
"Error occurred while registering the device type '" + deviceType.getName() + "'", e);
|
||||||
} finally {
|
} finally {
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateDeviceType(DeviceType deviceType) throws DeviceManagementDAOException {
|
public void updateDeviceType(DeviceType deviceType, int tenantId)
|
||||||
|
throws DeviceManagementDAOException {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException {
|
public List<DeviceType> getDeviceTypes(int tenantId) throws DeviceManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
PreparedStatement stmt = null;
|
PreparedStatement stmt = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
List<DeviceType> deviceTypes = new ArrayList<>();
|
List<DeviceType> deviceTypes = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE";
|
String sql =
|
||||||
|
"SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =" +
|
||||||
|
"? OR SHARED_WITH_ALL_TENANTS = TRUE";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
DeviceType deviceType = new DeviceType();
|
||||||
|
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
|
||||||
|
deviceType.setName(rs.getString("DEVICE_TYPE"));
|
||||||
|
deviceTypes.add(deviceType);
|
||||||
|
}
|
||||||
|
return deviceTypes;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while fetching the registered device types", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DeviceType> getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<DeviceType> deviceTypes = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql =
|
||||||
|
"SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
DeviceType deviceType = new DeviceType();
|
||||||
|
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
|
||||||
|
deviceType.setName(rs.getString("DEVICE_TYPE"));
|
||||||
|
deviceTypes.add(deviceType);
|
||||||
|
}
|
||||||
|
return deviceTypes;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while fetching the registered device types", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DeviceType> getSharedDeviceTypes() throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<DeviceType> deviceTypes = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql =
|
||||||
|
"SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where " +
|
||||||
|
"SHARED_WITH_ALL_TENANTS = TRUE";
|
||||||
stmt = conn.prepareStatement(sql);
|
stmt = conn.prepareStatement(sql);
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
@ -90,7 +153,6 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
|
|||||||
String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE WHERE ID = ?";
|
String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE WHERE ID = ?";
|
||||||
stmt = conn.prepareStatement(sql);
|
stmt = conn.prepareStatement(sql);
|
||||||
stmt.setInt(1, id);
|
stmt.setInt(1, id);
|
||||||
|
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
DeviceType deviceType = null;
|
DeviceType deviceType = null;
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
@ -100,41 +162,44 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
|
|||||||
}
|
}
|
||||||
return deviceType;
|
return deviceType;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DeviceManagementDAOException("Error occurred while fetching the registered device type", e);
|
throw new DeviceManagementDAOException(
|
||||||
|
"Error occurred while fetching the registered device type", e);
|
||||||
} finally {
|
} finally {
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DeviceType getDeviceType(String type) throws DeviceManagementDAOException {
|
public DeviceType getDeviceType(String type, int tenantId) throws
|
||||||
|
DeviceManagementDAOException {
|
||||||
Connection conn;
|
Connection conn;
|
||||||
PreparedStatement stmt = null;
|
PreparedStatement stmt = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
DeviceType deviceType = null;
|
DeviceType deviceType = null;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
String sql = "SELECT ID From DM_DEVICE_TYPE WHERE NAME = ?";
|
String sql = "SELECT ID AS DEVICE_TYPE_ID FROM DM_DEVICE_TYPE WHERE (PROVIDER_TENANT_ID =? OR " +
|
||||||
|
"SHARED_WITH_ALL_TENANTS = TRUE) AND NAME =?";
|
||||||
stmt = conn.prepareStatement(sql);
|
stmt = conn.prepareStatement(sql);
|
||||||
stmt.setString(1, type);
|
stmt.setInt(1, tenantId);
|
||||||
|
stmt.setString(2, type);
|
||||||
rs = stmt.executeQuery();
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
deviceType = new DeviceType();
|
deviceType = new DeviceType();
|
||||||
deviceType.setId(rs.getInt("ID"));
|
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
|
||||||
deviceType.setName(type);
|
deviceType.setName(type);
|
||||||
}
|
}
|
||||||
return deviceType;
|
return deviceType;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new DeviceManagementDAOException("Error occurred while fetch device type id for device type " +
|
throw new DeviceManagementDAOException(
|
||||||
"'" + type + "'", e);
|
"Error occurred while fetch device type id for device type '" + type + "'", e);
|
||||||
} finally {
|
} finally {
|
||||||
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeDeviceType(String type) throws DeviceManagementDAOException {
|
public void removeDeviceType(String type, int tenantId) throws DeviceManagementDAOException {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.device.details.mgt;
|
||||||
|
|
||||||
|
public class DeviceDetailsMgtException extends Exception{
|
||||||
|
private static final long serialVersionUID = -315127931137777499L;
|
||||||
|
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.device.details.mgt;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will manage the storing of device details related generic information such as cpu/memory utilization, battery level,
|
||||||
|
* plugged in to a power source or operation on battery.
|
||||||
|
* In CDM framework, we only keep the snapshot of the device information. So previous details are deleted as soon as new
|
||||||
|
* data is arrived.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface DeviceInformationManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will manage the storing of the device information as key value pairs.
|
||||||
|
* @param deviceInfo - Device info object.
|
||||||
|
* @throws DeviceDetailsMgtException
|
||||||
|
*/
|
||||||
|
void addDeviceInfo(DeviceInfo deviceInfo) throws DeviceDetailsMgtException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will return the device information.
|
||||||
|
* @param deviceIdentifier - Device identifier, device type.
|
||||||
|
* @return - Device information object.
|
||||||
|
* @throws DeviceDetailsMgtException
|
||||||
|
*/
|
||||||
|
DeviceInfo getDeviceInfo(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will manage storing the device location as latitude, longitude, address, zip, country etc..
|
||||||
|
* @param deviceLocation - Device location object.
|
||||||
|
* @throws DeviceDetailsMgtException
|
||||||
|
*/
|
||||||
|
void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will return the device location with latitude, longitude, address etc..
|
||||||
|
* @param deviceIdentifier - Device identifier, device type.
|
||||||
|
* @return Device location object.
|
||||||
|
* @throws DeviceDetailsMgtException
|
||||||
|
*/
|
||||||
|
DeviceLocation getDeviceLocation(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException;
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * This method will manage the storing of device application list.
|
||||||
|
// * @param deviceApplication - Device application list.
|
||||||
|
// * @throws DeviceDetailsMgtException
|
||||||
|
// */
|
||||||
|
// void addDeviceApplications(DeviceApplication deviceApplication) throws DeviceDetailsMgtException;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * This method will return the application list of the device.
|
||||||
|
// * @param deviceIdentifier - Device identifier, device type.
|
||||||
|
// * @return - Device application list with device identifier.
|
||||||
|
// * @throws DeviceDetailsMgtException
|
||||||
|
// */
|
||||||
|
// DeviceApplication getDeviceApplication(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException;
|
||||||
|
}
|
||||||
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.device.details.mgt.dao;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will store device details related generic information such as cpu/memory utilization, battery level,
|
||||||
|
* plugged in to a power source or operation on battery.
|
||||||
|
* In CDM framework, we only keep the snapshot of the device information. So previous details are deleted as soon as new
|
||||||
|
* data is arrived.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public interface DeviceDetailsDAO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will add device information to the database.
|
||||||
|
* @param deviceInfo - Device information object.
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
void addDeviceInformation(DeviceInfo deviceInfo) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will add the device properties to the database.
|
||||||
|
* @param propertyMap - device properties.
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
void addDeviceProperties(Map<String, String> propertyMap, int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will return the device information when device id is provided.
|
||||||
|
* @param deviceId - device Id
|
||||||
|
* @return DeviceInfo
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
DeviceInfo getDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will return the device properties from database.
|
||||||
|
* @param deviceId
|
||||||
|
* @return - device properties map.
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
Map<String, String> getDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will delete the device information from the database.
|
||||||
|
* @param deviceId - Integer.
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
void deleteDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will delete the device properties from database.
|
||||||
|
* @param deviceId - Integer.
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
void deleteDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will add device location to database.
|
||||||
|
* @param deviceLocation - Device location with latitude and longitude.
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will return the device location object when the device id is provided.
|
||||||
|
* @param deviceId - id of the device.
|
||||||
|
* @return - Device location object.
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
DeviceLocation getDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will delete the device location from the database.
|
||||||
|
* @param deviceId
|
||||||
|
* @throws DeviceDetailsMgtDAOException
|
||||||
|
*/
|
||||||
|
void deleteDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * This method will add device application to database.
|
||||||
|
// * @param deviceApplication - Device application
|
||||||
|
// * @throws DeviceDetailsMgtDAOException
|
||||||
|
// */
|
||||||
|
// void addDeviceApplications(DeviceApplication deviceApplication) throws DeviceDetailsMgtDAOException;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * This method will return the device application list once device id is provided.
|
||||||
|
// * @param deviceId
|
||||||
|
// * @return
|
||||||
|
// * @throws DeviceDetailsMgtDAOException
|
||||||
|
// */
|
||||||
|
// DeviceApplication getDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * This method will delete the application list from the database.
|
||||||
|
// * @param deviceId - Integer
|
||||||
|
// * @throws DeviceDetailsMgtDAOException
|
||||||
|
// */
|
||||||
|
// void deleteDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException;
|
||||||
|
}
|
||||||
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.device.details.mgt.dao;
|
||||||
|
|
||||||
|
public class DeviceDetailsMgtDAOException extends Exception{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -3151279311366666499L;
|
||||||
|
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtDAOException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtDAOException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtDAOException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtDAOException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDetailsMgtDAOException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,319 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.device.details.mgt.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class DeviceDetailsDAOImpl implements DeviceDetailsDAO {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(DeviceDetailsDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDeviceInformation(DeviceInfo deviceInfo) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_DETAIL (DEVICE_ID, DEVICE_MODEL, " +
|
||||||
|
"VENDOR, OS_VERSION, BATTERY_LEVEL, INTERNAL_TOTAL_MEMORY, INTERNAL_AVAILABLE_MEMORY, " +
|
||||||
|
"EXTERNAL_TOTAL_MEMORY, EXTERNAL_AVAILABLE_MEMORY, CONNECTION_TYPE, " +
|
||||||
|
"SSID, CPU_USAGE, TOTAL_RAM_MEMORY, AVAILABLE_RAM_MEMORY, PLUGGED_IN) " +
|
||||||
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
|
|
||||||
|
stmt.setInt(1, deviceInfo.getDeviceId());
|
||||||
|
stmt.setString(2, deviceInfo.getDeviceModel());
|
||||||
|
stmt.setString(3, deviceInfo.getVendor());
|
||||||
|
stmt.setString(4, deviceInfo.getOsVersion());
|
||||||
|
stmt.setDouble(5, deviceInfo.getBatteryLevel());
|
||||||
|
stmt.setDouble(6, deviceInfo.getInternalTotalMemory());
|
||||||
|
stmt.setDouble(7, deviceInfo.getInternalAvailableMemory());
|
||||||
|
stmt.setDouble(8, deviceInfo.getExternalTotalMemory());
|
||||||
|
stmt.setDouble(9, deviceInfo.getExternalAvailableMemory());
|
||||||
|
stmt.setString(10, deviceInfo.getConnectionType());
|
||||||
|
stmt.setString(11, deviceInfo.getSsid());
|
||||||
|
stmt.setDouble(12, deviceInfo.getCpuUsage());
|
||||||
|
stmt.setDouble(13, deviceInfo.getTotalRAMMemory());
|
||||||
|
stmt.setDouble(14, deviceInfo.getAvailableRAMMemory());
|
||||||
|
stmt.setBoolean(15, deviceInfo.isPluggedIn());
|
||||||
|
|
||||||
|
stmt.execute();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while inserting device details to database.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDeviceProperties(Map<String, String> propertyMap, int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
if (propertyMap.isEmpty()) {
|
||||||
|
log.warn("Property map of device id :" + deviceId + " is empty.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_INFO (DEVICE_ID, KEY_FIELD, VALUE_FIELD) VALUES (?, ?, ?)");
|
||||||
|
|
||||||
|
for (Map.Entry<String, String> entry : propertyMap.entrySet()) {
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setString(2, entry.getKey());
|
||||||
|
stmt.setString(3, entry.getValue());
|
||||||
|
stmt.addBatch();
|
||||||
|
}
|
||||||
|
stmt.executeBatch();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while inserting device properties to database.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceInfo getDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
DeviceInfo deviceInfo = new DeviceInfo();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
deviceInfo.setDeviceId(rs.getInt("DEVICE_ID"));
|
||||||
|
// deviceInfo.setIMEI(rs.getString("IMEI"));
|
||||||
|
// deviceInfo.setIMSI(rs.getString("IMSI"));
|
||||||
|
deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL"));
|
||||||
|
deviceInfo.setVendor(rs.getString("VENDOR"));
|
||||||
|
deviceInfo.setOsVersion(rs.getString("OS_VERSION"));
|
||||||
|
deviceInfo.setBatteryLevel(rs.getDouble("BATTERY_LEVEL"));
|
||||||
|
deviceInfo.setInternalTotalMemory(rs.getDouble("INTERNAL_TOTAL_MEMORY"));
|
||||||
|
deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY"));
|
||||||
|
deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY"));
|
||||||
|
deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY"));
|
||||||
|
// deviceInfo.setOperator(rs.getString("OPERATOR"));
|
||||||
|
deviceInfo.setConnectionType(rs.getString("CONNECTION_TYPE"));
|
||||||
|
// deviceInfo.setMobileSignalStrength(rs.getDouble("MOBILE_SIGNAL_STRENGTH"));
|
||||||
|
deviceInfo.setSsid(rs.getString("SSID"));
|
||||||
|
deviceInfo.setCpuUsage(rs.getDouble("CPU_USAGE"));
|
||||||
|
deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY"));
|
||||||
|
deviceInfo.setAvailableRAMMemory(rs.getDouble("AVAILABLE_RAM_MEMORY"));
|
||||||
|
deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN"));
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceInfo.setDeviceId(deviceId);
|
||||||
|
return deviceInfo;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while fetching the details of the registered devices.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
map.put(rs.getString("KEY_FIELD"), rs.getString("VALUE_FIELD"));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while fetching the properties of the registered devices.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String query = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(query);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while deleting the device information from the data base.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String query = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(query);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while deleting the device properties from the data base.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_LOCATION (DEVICE_ID, LATITUDE, LONGITUDE, STREET1, " +
|
||||||
|
"STREET2, CITY, ZIP, STATE, COUNTRY) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
|
stmt.setInt(1, deviceLocation.getDeviceId());
|
||||||
|
stmt.setDouble(2, deviceLocation.getLatitude());
|
||||||
|
stmt.setDouble(3, deviceLocation.getLongitude());
|
||||||
|
stmt.setString(4, deviceLocation.getStreet1());
|
||||||
|
stmt.setString(5, deviceLocation.getStreet2());
|
||||||
|
stmt.setString(6, deviceLocation.getCity());
|
||||||
|
stmt.setString(7, deviceLocation.getZip());
|
||||||
|
stmt.setString(8, deviceLocation.getState());
|
||||||
|
stmt.setString(9, deviceLocation.getCountry());
|
||||||
|
stmt.execute();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while adding the device location to database.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceLocation getDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
DeviceLocation location = new DeviceLocation();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT * FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
location.setDeviceId(deviceId);
|
||||||
|
location.setLatitude(rs.getDouble("LATITUDE"));
|
||||||
|
location.setLongitude(rs.getDouble("LONGITUDE"));
|
||||||
|
location.setStreet1(rs.getString("STREET1"));
|
||||||
|
location.setStreet2(rs.getString("STREET2"));
|
||||||
|
location.setCity(rs.getString("CITY"));
|
||||||
|
location.setZip(rs.getString("ZIP"));
|
||||||
|
location.setState(rs.getString("STATE"));
|
||||||
|
location.setCountry(rs.getString("COUNTRY"));
|
||||||
|
}
|
||||||
|
location.setDeviceId(deviceId);
|
||||||
|
|
||||||
|
return location;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while fetching the location of the registered devices.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String query = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(query);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtDAOException("Error occurred while deleting the device location from the data base.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws SQLException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public void addDeviceApplications(DeviceApplication deviceApplication) throws DeviceDetailsMgtDAOException {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public DeviceApplication getDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public void deleteDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,146 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.device.details.mgt.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class DeviceInformationManagerImpl implements DeviceInformationManager {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(DeviceInformationManagerImpl.class);
|
||||||
|
|
||||||
|
private DeviceDetailsDAO deviceDetailsDAO;
|
||||||
|
|
||||||
|
public DeviceInformationManagerImpl() {
|
||||||
|
this.deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDeviceInfo(DeviceInfo deviceInfo) throws DeviceDetailsMgtException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
Device device = DeviceManagementDataHolder.getInstance().
|
||||||
|
getDeviceManagementProvider().getDevice(deviceInfo.getDeviceIdentifier());
|
||||||
|
deviceInfo.setDeviceId(device.getId());
|
||||||
|
|
||||||
|
DeviceManagementDAOFactory.beginTransaction();
|
||||||
|
deviceDetailsDAO.deleteDeviceInformation(deviceInfo.getDeviceId());
|
||||||
|
deviceDetailsDAO.deleteDeviceProperties(deviceInfo.getDeviceId());
|
||||||
|
deviceDetailsDAO.addDeviceInformation(deviceInfo);
|
||||||
|
deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), deviceInfo.getDeviceId());
|
||||||
|
DeviceManagementDAOFactory.commitTransaction();
|
||||||
|
} catch (TransactionManagementException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
throw new DeviceDetailsMgtException("Transactional error occurred while adding the device information.");
|
||||||
|
} catch (DeviceDetailsMgtDAOException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
throw new DeviceDetailsMgtException("Error occurred while adding the device information.");
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
throw new DeviceDetailsMgtException("Error occurred while retrieving the device information.");
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceInfo getDeviceInfo(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
Device device = DeviceManagementDataHolder.getInstance().
|
||||||
|
getDeviceManagementProvider().getDevice(deviceIdentifier);
|
||||||
|
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId());
|
||||||
|
deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(device.getId()));
|
||||||
|
return deviceInfo;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e);
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e);
|
||||||
|
} catch (DeviceDetailsMgtDAOException e) {
|
||||||
|
throw new DeviceDetailsMgtException("Exception occurred while retrieving device details.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
Device device = DeviceManagementDataHolder.getInstance().
|
||||||
|
getDeviceManagementProvider().getDevice(deviceLocation.getDeviceIdentifier());
|
||||||
|
deviceLocation.setDeviceId(device.getId());
|
||||||
|
DeviceManagementDAOFactory.beginTransaction();
|
||||||
|
deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId());
|
||||||
|
deviceDetailsDAO.addDeviceLocation(deviceLocation);
|
||||||
|
DeviceManagementDAOFactory.commitTransaction();
|
||||||
|
} catch (TransactionManagementException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location information.");
|
||||||
|
} catch (DeviceDetailsMgtDAOException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
throw new DeviceDetailsMgtException("Error occurred while adding the device location information.");
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
throw new DeviceDetailsMgtException("Error occurred while getting the device information.");
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceLocation getDeviceLocation(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException {
|
||||||
|
try {
|
||||||
|
Device device = DeviceManagementDataHolder.getInstance().
|
||||||
|
getDeviceManagementProvider().getDevice(deviceIdentifier);
|
||||||
|
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
return deviceDetailsDAO.getDeviceLocation(device.getId());
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e);
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
throw new DeviceDetailsMgtException("Exception occurred while retrieving the device.", e);
|
||||||
|
} catch (DeviceDetailsMgtDAOException e) {
|
||||||
|
throw new DeviceDetailsMgtException("Exception occurred while retrieving device location.", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,116 +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.core.email.sender;
|
|
||||||
|
|
||||||
import org.apache.axiom.om.OMAbstractFactory;
|
|
||||||
import org.apache.axiom.om.OMElement;
|
|
||||||
import org.apache.axis2.AxisFault;
|
|
||||||
import org.apache.axis2.Constants;
|
|
||||||
import org.apache.axis2.addressing.EndpointReference;
|
|
||||||
import org.apache.axis2.client.Options;
|
|
||||||
import org.apache.axis2.client.ServiceClient;
|
|
||||||
import org.apache.axis2.context.ConfigurationContext;
|
|
||||||
import org.apache.axis2.context.MessageContext;
|
|
||||||
import org.apache.axis2.transport.base.BaseConstants;
|
|
||||||
import org.apache.axis2.transport.mail.MailConstants;
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
||||||
import org.wso2.carbon.device.mgt.common.EmailMessageProperties;
|
|
||||||
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
|
||||||
import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations;
|
|
||||||
import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder;
|
|
||||||
import org.wso2.carbon.device.mgt.core.service.EmailService;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
public class EmailServiceProviderImpl implements EmailService {
|
|
||||||
|
|
||||||
private static ThreadPoolExecutor threadPoolExecutor;
|
|
||||||
|
|
||||||
static {
|
|
||||||
EmailConfigurations emailConfig =
|
|
||||||
DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
|
|
||||||
getDeviceManagementConfigRepository().getEmailConfigurations();
|
|
||||||
|
|
||||||
threadPoolExecutor = new ThreadPoolExecutor(emailConfig.getMinNumOfThread(),
|
|
||||||
emailConfig.getMaxNumOfThread(), emailConfig.getKeepAliveTime(), TimeUnit.SECONDS,
|
|
||||||
new LinkedBlockingQueue<Runnable>(emailConfig.getThreadQueueCapacity()));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String EMAIL_URI_SCHEME = "mailto:";
|
|
||||||
private static Log log = LogFactory.getLog(EmailServiceProviderImpl.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException {
|
|
||||||
for (String toAddr : emailMessageProperties.getMailTo()) {
|
|
||||||
threadPoolExecutor
|
|
||||||
.submit(new EmailSender(toAddr, emailMessageProperties.getSubject(),
|
|
||||||
emailMessageProperties.getMessageBody()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class EmailSender implements Runnable {
|
|
||||||
|
|
||||||
String to;
|
|
||||||
String subject;
|
|
||||||
String body;
|
|
||||||
|
|
||||||
EmailSender(String to, String subject, String body) {
|
|
||||||
this.to = to;
|
|
||||||
this.subject = subject;
|
|
||||||
this.body = body;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
Map<String, String> headerMap = new HashMap<String, String>();
|
|
||||||
headerMap.put(MailConstants.MAIL_HEADER_SUBJECT, subject);
|
|
||||||
OMElement payload = OMAbstractFactory.getOMFactory().createOMElement(
|
|
||||||
BaseConstants.DEFAULT_TEXT_WRAPPER, null);
|
|
||||||
payload.setText(body);
|
|
||||||
try {
|
|
||||||
ServiceClient serviceClient;
|
|
||||||
ConfigurationContext configContext = EmailServiceDataHolder.getInstance().
|
|
||||||
getConfigurationContextService().getServerConfigContext();
|
|
||||||
//Set configuration service client if available, else create new service client
|
|
||||||
if (configContext != null) {
|
|
||||||
serviceClient = new ServiceClient(configContext, null);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
serviceClient = new ServiceClient();
|
|
||||||
}
|
|
||||||
Options options = new Options();
|
|
||||||
options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
|
|
||||||
options.setProperty(MessageContext.TRANSPORT_HEADERS, headerMap);
|
|
||||||
options.setProperty(MailConstants.TRANSPORT_MAIL_FORMAT,
|
|
||||||
MailConstants.TRANSPORT_FORMAT_TEXT);
|
|
||||||
options.setTo(new EndpointReference(EMAIL_URI_SCHEME + to));
|
|
||||||
serviceClient.setOptions(options);
|
|
||||||
serviceClient.fireAndForget(payload);
|
|
||||||
log.debug("Sending confirmation mail to " + to);
|
|
||||||
} catch (AxisFault e) {
|
|
||||||
log.error("Error in delivering the message, subject: '" + subject + "', to: '" + to + "'", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.group.mgt;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
||||||
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is used to expose protected methods to the core. Use with internal access only.
|
||||||
|
*/
|
||||||
|
public class DeviceGroupBuilder extends DeviceGroup {
|
||||||
|
|
||||||
|
private int groupId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set device group to be decorated with the builder
|
||||||
|
*
|
||||||
|
* @param deviceGroup to decorate
|
||||||
|
*/
|
||||||
|
public DeviceGroupBuilder(DeviceGroup deviceGroup) {
|
||||||
|
this.setDescription(deviceGroup.getDescription());
|
||||||
|
this.setName(deviceGroup.getName());
|
||||||
|
this.setDateOfCreation(deviceGroup.getDateOfCreation());
|
||||||
|
this.setDateOfLastUpdate(deviceGroup.getDateOfLastUpdate());
|
||||||
|
this.setOwner(deviceGroup.getOwner());
|
||||||
|
this.setUsers(deviceGroup.getUsers());
|
||||||
|
this.setRoles(deviceGroup.getRoles());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUsers(List<GroupUser> users) {
|
||||||
|
super.setUsers(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRoles(List<String> roles) {
|
||||||
|
super.setRoles(roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceGroup getGroup() {
|
||||||
|
return super.getGroup();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getGroupId() {
|
||||||
|
return groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGroupId(int groupId) {
|
||||||
|
this.groupId = groupId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,197 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.group.mgt.dao;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
||||||
|
import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface represents the key operations associated with persisting group related information.
|
||||||
|
*/
|
||||||
|
public interface GroupDAO {
|
||||||
|
/**
|
||||||
|
* Add new Device Group.
|
||||||
|
*
|
||||||
|
* @param deviceGroup to be added.
|
||||||
|
* @param tenantId of the group.
|
||||||
|
* @return sql execution result.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update an existing Device Group.
|
||||||
|
*
|
||||||
|
* @param deviceGroup group to update.
|
||||||
|
* @param oldGroupName of the group.
|
||||||
|
* @param oldOwner of the group.
|
||||||
|
* @param tenantId of the group.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner, int tenantId)
|
||||||
|
throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete an existing Device Group.
|
||||||
|
*
|
||||||
|
* @param groupName to be deleted.
|
||||||
|
* @param owner of the group.
|
||||||
|
* @param tenantId of the group.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
void deleteGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get device group by id.
|
||||||
|
*
|
||||||
|
* @param groupId of Device Group.
|
||||||
|
* @param tenantId of the group.
|
||||||
|
* @return Device Group in tenant with specified name.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
DeviceGroupBuilder getGroup(int groupId, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get device group by name.
|
||||||
|
*
|
||||||
|
* @param groupName of Device Group.
|
||||||
|
* @param owner of the group.
|
||||||
|
* @param tenantId of the group.
|
||||||
|
* @return Device Group in tenant with specified name.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
DeviceGroupBuilder getGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of Device Groups in tenant.
|
||||||
|
*
|
||||||
|
* @param startIndex for pagination.
|
||||||
|
* @param rowCount for pagination.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @return List of all Device Groups in tenant.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
List<DeviceGroupBuilder> getGroups(int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of Device Groups in tenant.
|
||||||
|
*
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @return List of all Device Groups in tenant.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
int getGroupCount(int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of Groups that matches with the given DeviceGroup name.
|
||||||
|
*
|
||||||
|
* @param groupName of the Device Group.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @return List of DeviceGroup that matches with the given DeviceGroup name.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
List<DeviceGroupBuilder> findInGroups(String groupName, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check group already existed with given name.
|
||||||
|
*
|
||||||
|
* @param groupName of the Device Group.
|
||||||
|
* @param owner of the Device Group.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @return existence of group with name
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
boolean isGroupExist(String groupName, String owner, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add device to a given Device Group.
|
||||||
|
*
|
||||||
|
* @param groupName of the Device Group.
|
||||||
|
* @param owner of the Device Group.
|
||||||
|
* @param deviceId of the device.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
void addDevice(String groupName, String owner, int deviceId, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove device from the Device Group.
|
||||||
|
*
|
||||||
|
* @param groupName of the Device Group.
|
||||||
|
* @param owner of the Device Group.
|
||||||
|
* @param deviceId of the device.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
void removeDevice(String groupName, String owner, int deviceId, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check device is belonging to a Device Group.
|
||||||
|
*
|
||||||
|
* @param groupName of the Device Group.
|
||||||
|
* @param owner of the Device Group.
|
||||||
|
* @param deviceId of the device.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
boolean isDeviceMappedToGroup(String groupName, String owner, int deviceId, int tenantId)
|
||||||
|
throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of devices in a Device Group.
|
||||||
|
*
|
||||||
|
* @param groupName of the Device Group.
|
||||||
|
* @param owner of the Device Group.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @return device count.
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
int getDeviceCount(String groupName, String owner, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all devices of a given tenant and device group.
|
||||||
|
*
|
||||||
|
* @param groupName of the group.
|
||||||
|
* @param owner of the Device Group.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @return list of device in group
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
List<Device> getDevices(String groupName, String owner, int tenantId) throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get paginated result of devices of a given tenant and device group.
|
||||||
|
*
|
||||||
|
* @param groupName of the group.
|
||||||
|
* @param owner of the Device Group.
|
||||||
|
* @param startIndex for pagination.
|
||||||
|
* @param rowCount for pagination.
|
||||||
|
* @param tenantId of user's tenant.
|
||||||
|
* @return list of device in group
|
||||||
|
* @throws GroupManagementDAOException
|
||||||
|
*/
|
||||||
|
List<Device> getDevices(String groupName, String owner, int startIndex, int rowCount, int tenantId)
|
||||||
|
throws GroupManagementDAOException;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,465 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.group.mgt.dao;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.Device;
|
||||||
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents implementation of GroupDAO
|
||||||
|
*/
|
||||||
|
public class GroupDAOImpl implements GroupDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs;
|
||||||
|
int groupId = -1;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, "
|
||||||
|
+ "OWNER, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?)";
|
||||||
|
stmt = conn.prepareStatement(sql, new String[]{"ID"});
|
||||||
|
stmt.setString(1, deviceGroup.getDescription());
|
||||||
|
stmt.setString(2, deviceGroup.getName());
|
||||||
|
stmt.setLong(3, new Date().getTime());
|
||||||
|
stmt.setLong(4, new Date().getTime());
|
||||||
|
stmt.setString(5, deviceGroup.getOwner());
|
||||||
|
stmt.setInt(6, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
groupId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return groupId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" +
|
||||||
|
deviceGroup.getName() + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, DATE_OF_LAST_UPDATE = ?, OWNER = ? "
|
||||||
|
+ "WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, deviceGroup.getDescription());
|
||||||
|
stmt.setString(2, deviceGroup.getName());
|
||||||
|
stmt.setLong(3, deviceGroup.getDateOfLastUpdate());
|
||||||
|
stmt.setString(4, deviceGroup.getOwner());
|
||||||
|
stmt.setString(5, oldGroupName);
|
||||||
|
stmt.setString(6, oldOwner);
|
||||||
|
stmt.setInt(7, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" +
|
||||||
|
deviceGroup.getName() + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = (SELECT ID AS GROUP_ID FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while removing mappings for group '" + groupName +
|
||||||
|
"'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "DELETE FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while deleting group '" + groupName + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceGroupBuilder getGroup(int groupId, int tenantId) throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER "
|
||||||
|
+ "FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, groupId);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
return GroupManagementDAOUtil.loadGroup(resultSet);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while obtaining information of Device Group '" +
|
||||||
|
groupId + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DeviceGroupBuilder getGroup(String groupName, String owner, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER "
|
||||||
|
+ "FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
return GroupManagementDAOUtil.loadGroup(resultSet);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while obtaining information of Device Group '" +
|
||||||
|
groupName + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DeviceGroupBuilder> getGroups(int startIndex, int rowCount, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
List<DeviceGroupBuilder> deviceGroupList = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER "
|
||||||
|
+ "FROM DM_GROUP WHERE TENANT_ID = ? LIMIT ?, ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
//noinspection JpaQueryApiInspection
|
||||||
|
stmt.setInt(2, startIndex);
|
||||||
|
//noinspection JpaQueryApiInspection
|
||||||
|
stmt.setInt(3, rowCount);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
deviceGroupList = new ArrayList<>();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
return deviceGroupList;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getGroupCount(int tenantId) throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP WHERE TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
return resultSet.getInt("GROUP_COUNT");
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while getting group count'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DeviceGroupBuilder> findInGroups(String groupName, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
List<DeviceGroupBuilder> deviceGroups = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER "
|
||||||
|
+ "FROM DM_GROUP WHERE GROUP_NAME LIKE ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, "%" + groupName + "%");
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
deviceGroups.add(GroupManagementDAOUtil.loadGroup(resultSet));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while listing Device Groups by name '" +
|
||||||
|
groupName + "' in tenant '" + tenantId + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
return deviceGroups;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isGroupExist(String groupName, String owner, int tenantId) throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT GROUP_NAME FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
return resultSet.next();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" +
|
||||||
|
groupName + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDevice(String groupName, String owner, int deviceId, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "INSERT INTO DM_DEVICE_GROUP_MAP(DEVICE_ID, GROUP_ID, TENANT_ID) " +
|
||||||
|
"VALUES (?, (SELECT ID as GROUP_ID FROM DM_GROUP " +
|
||||||
|
"WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?), ?)";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setString(2, groupName);
|
||||||
|
stmt.setString(3, owner);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
stmt.setInt(5, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
stmt.getGeneratedKeys();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while adding device to Group '" + groupName + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeDevice(String groupName, String owner, int deviceId, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ? AND GROUP_ID = (SELECT ID as GROUP_ID " +
|
||||||
|
"FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setString(2, groupName);
|
||||||
|
stmt.setString(3, owner);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
stmt.setInt(5, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
stmt.getGeneratedKeys();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while removing device from Group '" +
|
||||||
|
groupName + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDeviceMappedToGroup(String groupName, String owner, int deviceId, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT dm.ID FROM DM_DEVICE_GROUP_MAP dm, (SELECT ID as GROUP_ID FROM DM_GROUP " +
|
||||||
|
"WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) dg " +
|
||||||
|
"WHERE dm.GROUP_ID = dg.GROUP_ID AND dm.ID = ? AND dm.TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, deviceId);
|
||||||
|
stmt.setInt(5, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
return resultSet.next();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" +
|
||||||
|
groupName + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getDeviceCount(String groupName, String owner, int tenantId) throws GroupManagementDAOException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
try {
|
||||||
|
Connection conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT COUNT(gm.ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP gm, (SELECT ID as GROUP_ID " +
|
||||||
|
"FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) dg " +
|
||||||
|
"WHERE dm.GROUP_ID = dg.GROUP_ID AND dm.TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
resultSet = stmt.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
return resultSet.getInt("DEVICE_COUNT");
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" +
|
||||||
|
groupName + "'", e);
|
||||||
|
} finally {
|
||||||
|
GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getDevices(String groupName, String owner, int tenantId) throws GroupManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " +
|
||||||
|
"(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, " +
|
||||||
|
"t.NAME AS DEVICE_TYPE FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, " +
|
||||||
|
"d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (SELECT dgm.DEVICE_ID " +
|
||||||
|
"FROM DM_DEVICE_GROUP_MAP dgm, DM_GROUP dg WHERE dgm.GROUP_ID = dg.ID AND dg.GROUP_NAME = ? " +
|
||||||
|
"AND dg.OWNER = ? AND dg.TENANT_ID = ?) dgm1 WHERE d.ID = dgm1.DEVICE_ID " +
|
||||||
|
"AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
stmt.setInt(5, tenantId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Device> getDevices(String groupName, String owner, int startIndex, int rowCount, int tenantId)
|
||||||
|
throws GroupManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Device> devices = null;
|
||||||
|
try {
|
||||||
|
conn = GroupManagementDAOFactory.getConnection();
|
||||||
|
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
|
||||||
|
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
|
||||||
|
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " +
|
||||||
|
"(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, " +
|
||||||
|
"t.NAME AS DEVICE_TYPE FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, " +
|
||||||
|
"d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (SELECT dgm.DEVICE_ID " +
|
||||||
|
"FROM DM_DEVICE_GROUP_MAP dgm, DM_GROUP dg WHERE dgm.GROUP_ID = dg.ID AND dg.GROUP_NAME = ? " +
|
||||||
|
"AND dg.OWNER = ? AND dg.TENANT_ID = ?) dgm1 WHERE d.ID = dgm1.DEVICE_ID " +
|
||||||
|
"AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " +
|
||||||
|
"WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?, ?";
|
||||||
|
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, groupName);
|
||||||
|
stmt.setString(2, owner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
stmt.setInt(5, tenantId);
|
||||||
|
//noinspection JpaQueryApiInspection
|
||||||
|
stmt.setInt(6, startIndex);
|
||||||
|
//noinspection JpaQueryApiInspection
|
||||||
|
stmt.setInt(7, rowCount);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
devices = new ArrayList<>();
|
||||||
|
while (rs.next()) {
|
||||||
|
Device device = DeviceManagementDAOUtil.loadDevice(rs);
|
||||||
|
devices.add(device);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new GroupManagementDAOException("Error occurred while retrieving information of all " +
|
||||||
|
"registered devices", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.group.mgt.dao;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom exception class for group management data access related exceptions.
|
||||||
|
*/
|
||||||
|
public class GroupManagementDAOException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 2021891706072918864L;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new exception with the specified detail message and nested exception.
|
||||||
|
*
|
||||||
|
* @param message error message
|
||||||
|
* @param nestedException exception
|
||||||
|
*/
|
||||||
|
public GroupManagementDAOException(String message, Exception nestedException) {
|
||||||
|
super(message, nestedException);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new exception with the specified detail message and cause.
|
||||||
|
*
|
||||||
|
* @param message the detail message.
|
||||||
|
* @param cause the cause of this exception.
|
||||||
|
*/
|
||||||
|
public GroupManagementDAOException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new exception with the specified detail message
|
||||||
|
*
|
||||||
|
* @param message the detail message.
|
||||||
|
*/
|
||||||
|
public GroupManagementDAOException(String message) {
|
||||||
|
super(message);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new exception with the specified and cause.
|
||||||
|
*
|
||||||
|
* @param cause the cause of this exception.
|
||||||
|
*/
|
||||||
|
public GroupManagementDAOException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.message = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,211 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.group.mgt.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents factory for group management data operations
|
||||||
|
*/
|
||||||
|
public class GroupManagementDAOFactory {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(GroupManagementDAOFactory.class);
|
||||||
|
private static DataSource dataSource;
|
||||||
|
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get instance of GroupDAO
|
||||||
|
*
|
||||||
|
* @return instance of GroupDAO implementation
|
||||||
|
*/
|
||||||
|
public static GroupDAO getGroupDAO() {
|
||||||
|
return new GroupDAOImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize factory with datasource configs
|
||||||
|
*
|
||||||
|
* @param config data source configuration
|
||||||
|
*/
|
||||||
|
public static void init(DataSourceConfig config) {
|
||||||
|
dataSource = resolveDataSource(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize factory with existing datasource
|
||||||
|
*
|
||||||
|
* @param dtSource an existing datasource
|
||||||
|
*/
|
||||||
|
public static void init(DataSource dtSource) {
|
||||||
|
dataSource = dtSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Begin transaction with datasource for write data
|
||||||
|
*
|
||||||
|
* @throws TransactionManagementException
|
||||||
|
*/
|
||||||
|
public static void beginTransaction() throws TransactionManagementException {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
||||||
|
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
|
||||||
|
"transaction is already active is a sign of improper transaction handling");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
conn = dataSource.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
currentConnection.set(conn);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new TransactionManagementException("Error occurred while retrieving config.datasource connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open connection to the datasource for read data
|
||||||
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
public static void openConnection() throws SQLException {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn != null) {
|
||||||
|
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
||||||
|
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
|
||||||
|
"transaction is already active is a sign of improper transaction handling");
|
||||||
|
}
|
||||||
|
conn = dataSource.getConnection();
|
||||||
|
currentConnection.set(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current connection to datasource
|
||||||
|
*
|
||||||
|
* @return current connection
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn == null) {
|
||||||
|
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||||
|
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||||
|
"'beginTransaction'/'openConnection' methods");
|
||||||
|
}
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Commit current transaction to the datasource
|
||||||
|
*/
|
||||||
|
public static void commitTransaction() {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn == null) {
|
||||||
|
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||||
|
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||||
|
"'beginTransaction'/'openConnection' methods");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
conn.commit();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.error("Error occurred while committing the transaction", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rollback current transaction on failure
|
||||||
|
*/
|
||||||
|
public static void rollbackTransaction() {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn == null) {
|
||||||
|
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||||
|
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||||
|
"'beginTransaction'/'openConnection' methods");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.warn("Error occurred while roll-backing the transaction", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close data connection associated with current transaction
|
||||||
|
*/
|
||||||
|
public static void closeConnection() {
|
||||||
|
Connection conn = currentConnection.get();
|
||||||
|
if (conn == null) {
|
||||||
|
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||||
|
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||||
|
"'beginTransaction'/'openConnection' methods");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.warn("Error occurred while close the connection");
|
||||||
|
}
|
||||||
|
currentConnection.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve data source from the data source definition
|
||||||
|
*
|
||||||
|
* @param config data source configuration
|
||||||
|
* @return data source resolved from the data source definition
|
||||||
|
*/
|
||||||
|
private static DataSource resolveDataSource(DataSourceConfig config) {
|
||||||
|
DataSource dataSource = null;
|
||||||
|
if (config == null) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"Device Management Repository data source configuration " + "is null and " +
|
||||||
|
"thus, is not initialized");
|
||||||
|
}
|
||||||
|
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
|
||||||
|
if (jndiConfig != null) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Initializing Device Management Repository data source using the JNDI " +
|
||||||
|
"Lookup Definition");
|
||||||
|
}
|
||||||
|
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
|
||||||
|
jndiConfig.getJndiProperties();
|
||||||
|
if (jndiPropertyList != null) {
|
||||||
|
Hashtable<Object, Object> jndiProperties = new Hashtable<>();
|
||||||
|
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
||||||
|
jndiProperties.put(prop.getName(), prop.getValue());
|
||||||
|
}
|
||||||
|
dataSource = GroupManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
|
||||||
|
} else {
|
||||||
|
dataSource = GroupManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* 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.core.group.mgt.dao;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
||||||
|
import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder;
|
||||||
|
|
||||||
|
import javax.naming.InitialContext;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents utilities required to work with group management data
|
||||||
|
*/
|
||||||
|
public final class GroupManagementDAOUtil {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(GroupManagementDAOUtil.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleanup resources used to transaction
|
||||||
|
*
|
||||||
|
* @param stmt Prepared statement used
|
||||||
|
* @param rs Obtained results set
|
||||||
|
*/
|
||||||
|
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
|
||||||
|
if (rs != null) {
|
||||||
|
try {
|
||||||
|
rs.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.warn("Error occurred while closing result set", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (stmt != null) {
|
||||||
|
try {
|
||||||
|
stmt.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.warn("Error occurred while closing prepared statement", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lookup datasource using name and jndi properties
|
||||||
|
*
|
||||||
|
* @param dataSourceName Name of datasource to lookup
|
||||||
|
* @param jndiProperties Hash table of JNDI Properties
|
||||||
|
* @return datasource looked
|
||||||
|
*/
|
||||||
|
public static DataSource lookupDataSource(String dataSourceName,
|
||||||
|
final Hashtable<Object, Object> jndiProperties) {
|
||||||
|
try {
|
||||||
|
if (jndiProperties == null || jndiProperties.isEmpty()) {
|
||||||
|
return (DataSource) InitialContext.doLookup(dataSourceName);
|
||||||
|
}
|
||||||
|
final InitialContext context = new InitialContext(jndiProperties);
|
||||||
|
return (DataSource) context.lookup(dataSourceName);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DeviceGroupBuilder loadGroup(ResultSet resultSet) throws SQLException {
|
||||||
|
DeviceGroupBuilder group = new DeviceGroupBuilder(new DeviceGroup());
|
||||||
|
group.setDescription(resultSet.getString("DESCRIPTION"));
|
||||||
|
group.setName(resultSet.getString("GROUP_NAME"));
|
||||||
|
group.setDateOfCreation(resultSet.getLong("DATE_OF_CREATE"));
|
||||||
|
group.setDateOfLastUpdate(resultSet.getLong("DATE_OF_LAST_UPDATE"));
|
||||||
|
group.setOwner(resultSet.getString("OWNER"));
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -19,10 +19,28 @@
|
|||||||
package org.wso2.carbon.device.mgt.core.internal;
|
package org.wso2.carbon.device.mgt.core.internal;
|
||||||
|
|
||||||
import org.apache.axis2.context.ConfigurationContext;
|
import org.apache.axis2.context.ConfigurationContext;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.context.CarbonContext;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.context.RegistryType;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.registry.api.Collection;
|
||||||
|
import org.wso2.carbon.registry.api.Registry;
|
||||||
|
import org.wso2.carbon.registry.api.RegistryException;
|
||||||
|
import org.wso2.carbon.registry.api.Resource;
|
||||||
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
|
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
|
||||||
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
|
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FilenameFilter;
|
||||||
|
|
||||||
public class DeviceManagementAxis2ConfigContextObserver implements Axis2ConfigurationContextObserver {
|
public class DeviceManagementAxis2ConfigContextObserver implements Axis2ConfigurationContextObserver {
|
||||||
|
|
||||||
|
private static final String EMAIL_TEMPLATE_DIR_RELATIVE_REGISTRY_PATH = "email-templates";
|
||||||
|
private static final Log log = LogFactory.getLog(DeviceManagementAxis2ConfigContextObserver.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void creatingConfigurationContext(int tenantId) {
|
public void creatingConfigurationContext(int tenantId) {
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user