diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/pom.xml b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/pom.xml new file mode 100644 index 0000000000..d5282a76a8 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/pom.xml @@ -0,0 +1,307 @@ + + + + + + + org.wso2.carbon.devicemgt-plugins + mb-extensions + 4.0.82-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.andes.extensions.device.mgt.api + war + WSO2 Carbon - MQTT Topics Management API + WSO2 Carbon - MQTT Topics Management API + http://wso2.org + + + + + maven-compiler-plugin + + 1.7 + 1.7 + + + + maven-war-plugin + + WEB-INF/lib/*cxf*.jar + api#mqtt-topics#v1.0 + + + + + + + + deploy + + compile + + + org.apache.maven.plugins + maven-antrun-plugin + 1.7 + + + compile + + run + + + + + + + + + + + + + + + + + + client + + test + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + test + + java + + + + + + + + + + + + org.apache.cxf + cxf-rt-frontend-jaxws + provided + + + org.apache.cxf + cxf-rt-frontend-jaxrs + provided + + + org.apache.cxf + cxf-rt-transports-http + provided + + + junit + junit + test + + + commons-httpclient.wso2 + commons-httpclient + provided + + + javax.ws.rs + jsr311-api + + + + org.wso2.carbon + org.wso2.carbon.utils + provided + + + org.wso2.carbon + org.wso2.carbon.logging + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + provided + + + org.apache.axis2.wso2 + axis2 + + + org.apache.axis2.transport + axis2-transport-mail + + + org.apache.ws.commons.axiom.wso2 + axiom + + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.core + provided + + + org.apache.axis2.wso2 + axis2-client + + + + + org.wso2.carbon.identity.inbound.auth.oauth2 + org.wso2.carbon.identity.oauth.stub + provided + + + org.apache.axis2.wso2 + axis2 + + + org.apache.axis2.wso2 + axis2-client + + + org.apache.axis2.transport + axis2-transport-mail + + + + + org.json.wso2 + json + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.certificate.mgt.core + provided + + + org.apache.axis2.wso2 + axis2 + + + + + io.swagger + swagger-annotations + + + io.swagger + swagger-core + + + com.fasterxml.jackson.module + jackson-module-jaxb-annotations + + + org.slf4j + slf4j-api + + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-core + + + + + io.swagger + swagger-jaxrs + + + com.fasterxml.jackson.module + jackson-module-jaxb-annotations + + + org.slf4j + slf4j-api + + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-core + + + + + javax.servlet + servlet-api + + + + org.wso2.carbon.devicemgt + org.wso2.carbon.apimgt.annotations + provided + + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-annotations + + + org.hibernate + hibernate-validator + + + javax.ws.rs + javax.ws.rs-api + + + org.wso2.carbon.messaging + org.wso2.carbon.andes.mqtt.stub + + + org.apache.axis2.wso2 + axis2 + + + org.apache.axis2.wso2 + axis2-client + + + org.apache.axis2.transport + axis2-transport-mail + + + org.apache.ws.commons.axiom.wso2 + axiom + + + + + + diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/ApiOriginFilter.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/ApiOriginFilter.java new file mode 100644 index 0000000000..d5f093ef9a --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/ApiOriginFilter.java @@ -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.andes.extensions.device.mgt.jaxrs; + +import javax.servlet.*; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class ApiOriginFilter implements Filter { + + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + HttpServletResponse res = (HttpServletResponse) response; + res.addHeader("Access-Control-Allow-Origin", "*"); + res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); + res.addHeader("Access-Control-Allow-Headers", "Content-Type"); + chain.doFilter(request, response); + } + + public void destroy() { + //do nothing + } + + public void init(FilterConfig filterConfig) throws ServletException { + //do nothing + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/BasePaginatedResult.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/BasePaginatedResult.java new file mode 100644 index 0000000000..7341f66861 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/BasePaginatedResult.java @@ -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.andes.extensions.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; + +public class BasePaginatedResult { + + /** + * Number of Resources returned. + */ + @ApiModelProperty( + value = "Number of total resources.", + example = "1") + @JsonProperty("count") + private long count; + + public long getCount() { + return count; + } + + public void setCount(long count) { + this.count = count; + } +} \ No newline at end of file diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/ErrorListItem.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/ErrorListItem.java new file mode 100644 index 0000000000..ebd09a021d --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/ErrorListItem.java @@ -0,0 +1,77 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import javax.validation.constraints.NotNull; + +@ApiModel(description = "") +public class ErrorListItem { + + @NotNull + private String code = null; + @NotNull + private String message = null; + + @ApiModelProperty(required = true, value = "") + @JsonProperty("code") + public String getCode() { + return code; + } + public void setCode(String code) { + this.code = code; + } + + public ErrorListItem() {} + + public ErrorListItem(String code, String msg) { + this.code = code; + this.message = msg; + } + + + /** + * Description about individual errors occurred + **/ + @ApiModelProperty(required = true, value = "Description about individual errors occurred") + @JsonProperty("message") + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("errorItem {\n"); + + sb.append(" code: ").append(code).append("\n"); + sb.append(" message: ").append(message).append("\n"); + sb.append("}\n"); + return sb.toString(); + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/ErrorResponse.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/ErrorResponse.java new file mode 100644 index 0000000000..b8e090d386 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/ErrorResponse.java @@ -0,0 +1,193 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.List; + +@ApiModel(description = "") +public class ErrorResponse { + + private Long code = null; + private String message = null; + private String description = null; + private String moreInfo = null; + private List errorItems = new ArrayList<>(); + + public ErrorResponse() { + } + + @JsonProperty(value = "code") + @ApiModelProperty(required = true, value = "") + public Long getCode() { + return code; + } + + public void setCode(Long code) { + this.code = code; + } + + @JsonProperty(value = "message") + @ApiModelProperty(required = true, value = "ErrorResponse message.") + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @JsonProperty(value = "description") + @ApiModelProperty(value = "A detail description about the error message.") + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @JsonProperty(value = "moreInfo") + @ApiModelProperty(value = "Preferably an url with more details about the error.") + public String getMoreInfo() { + return moreInfo; + } + + public void setMoreInfo(String moreInfo) { + this.moreInfo = moreInfo; + } + + public void addErrorListItem(ErrorListItem item) { + this.errorItems.add(item); + } + + /** + * If there are more than one error list them out. \nFor example, list out validation errors by each field. + */ + @JsonProperty(value = "errorItems") + @ApiModelProperty(value = "If there are more than one error list them out. \n" + + "For example, list out validation errors by each field.") + public List getErrorItems() { + return errorItems; + } + + public void setErrorItems(List error) { + this.errorItems = error; + } + + @Override + public String toString() { +// StringBuilder sb = new StringBuilder(); +// sb.append("{"); +// boolean cont = false; +// if (code != null) { +// cont = true; +// sb.append(" \"code\": ").append(code); +// } +// if (message != null) { +// if (cont) { +// sb.append(","); +// } +// cont = true; +// sb.append(" \"message\": \"").append(message).append("\""); +// } +// if (description != null) { +// if (cont) { +// sb.append(","); +// } +// cont = true; +// sb.append(" \"description\": ").append(description).append("\""); +// } +// if (moreInfo != null) { +// if (cont) { +// sb.append(","); +// } +// cont = true; +// sb.append(" \"moreInfo\": \"").append(moreInfo).append("\""); +// } +// if (error != null && error.size() > 0) { +// if (cont) { +// sb.append(","); +// } +// sb.append(" \"errorItems\": ").append(error); +// } +// sb.append("}"); +// return sb.toString(); + return null; + } + + public static class ErrorResponseBuilder { + + private Long code = null; + private String message = null; + private String description = null; + private String moreInfo = null; + private List error; + + + public ErrorResponseBuilder() { + this.error = new ArrayList<>(); + } + + public ErrorResponseBuilder setCode(long code) { + this.code = code; + return this; + } + + public ErrorResponseBuilder setMessage(String message) { + this.message = message; + return this; + } + + public ErrorResponseBuilder setDescription(String description) { + this.description = description; + return this; + } + + public ErrorResponseBuilder setMoreInfo(String moreInfo) { + this.moreInfo = moreInfo; + return this; + } + + public ErrorResponseBuilder addErrorItem(String code, String msg) { + ErrorListItem item = new ErrorListItem(); + item.setCode(code); + item.setMessage(msg); + this.error.add(item); + return this; + } + + public ErrorResponse build() { + ErrorResponse errorResponse = new ErrorResponse(); + errorResponse.setCode(code); + errorResponse.setMessage(message); + errorResponse.setErrorItems(error); + errorResponse.setDescription(description); + errorResponse.setMoreInfo(moreInfo); + return errorResponse; + } + } + +} + + diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/SubscriptionList.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/SubscriptionList.java new file mode 100644 index 0000000000..80a7d8cadd --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/SubscriptionList.java @@ -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.andes.extensions.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.andes.core.types.xsd.MQTTSubscription; +import org.wso2.carbon.andes.core.types.xsd.Subscription; + +import java.util.ArrayList; +import java.util.List; + +public class SubscriptionList extends BasePaginatedResult { + + private List subscriptions = new ArrayList<>(); + + @ApiModelProperty(value = "List of subscriptions returned") + @JsonProperty("subscriptions") + public List getList() { + return subscriptions; + } + + public void setList(List subscriptions) { + this.subscriptions = subscriptions; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" subscriptions: [").append(subscriptions).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + +} + diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/TopicPaginationRequest.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/TopicPaginationRequest.java new file mode 100644 index 0000000000..233b84bb36 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/beans/TopicPaginationRequest.java @@ -0,0 +1,114 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.beans; + +import java.util.Date; + +/** + * This class holds required parameters for a querying a paginated device response. + */ +public class TopicPaginationRequest { + + private int startIndex; + private int rowCount; + private Date since; + private String topic_name; + private int remaining_messages; + private String active; + private String durable; + private String subscriber_name; + private String identifier; + + public TopicPaginationRequest(int start, int rowCount) { + this.startIndex = start; + this.rowCount = rowCount; + } + + public int getStartIndex() { + return startIndex; + } + + public void setStartIndex(int startIndex) { + this.startIndex = startIndex; + } + + public int getRowCount() { + return rowCount; + } + + public void setRowCount(int rowCount) { + this.rowCount = rowCount; + } + + public Date getSince() { + return since; + } + + public void setSince(Date since) { + this.since = since; + } + + public String getTopic_name() { + return topic_name; + } + + public void setTopic_name(String topic_name) { + this.topic_name = topic_name; + } + + public int getRemaining_messages() { + return remaining_messages; + } + + public void setRemaining_messages(int remaining_messages) { + this.remaining_messages = remaining_messages; + } + + public String getActive() { + return active; + } + + public void setActive(String active) { + this.active = active; + } + + public String getDurable() { + return durable; + } + + public void setDurable(String durable) { + this.durable = durable; + } + + public String getSubscriber_name() { + return subscriber_name; + } + + public void setSubscriber_name(String subscriber_name) { + this.subscriber_name = subscriber_name; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/common/GsonMessageBodyHandler.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/common/GsonMessageBodyHandler.java new file mode 100644 index 0000000000..804244945e --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/common/GsonMessageBodyHandler.java @@ -0,0 +1,91 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.common; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import javax.ws.rs.Consumes; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; +import java.io.*; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; + +@Provider +@Produces(APPLICATION_JSON) +@Consumes(APPLICATION_JSON) +public class GsonMessageBodyHandler implements MessageBodyWriter, MessageBodyReader { + + public static final String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; + private Gson gson; + private static final String UTF_8 = "UTF-8"; + + public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { + return true; + } + + private Gson getGson() { + if (gson == null) { + final GsonBuilder gsonBuilder = new GsonBuilder(); + gson = gsonBuilder.setDateFormat(DATE_FORMAT).create(); + } + return gson; + } + + public Object readFrom(Class objectClass, Type type, Annotation[] annotations, MediaType mediaType, + MultivaluedMap stringStringMultivaluedMap, InputStream entityStream) + throws IOException, WebApplicationException { + + InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8"); + + try { + return getGson().fromJson(reader, type); + } finally { + reader.close(); + } + } + + public boolean isWriteable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { + return true; + } + + public long getSize(Object o, Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { + return -1; + } + + public void writeTo(Object object, Class aClass, Type type, Annotation[] annotations, MediaType mediaType, + MultivaluedMap stringObjectMultivaluedMap, OutputStream entityStream) + throws IOException, WebApplicationException { + + OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); + try { + getGson().toJson(object, type, writer); + } finally { + writer.close(); + } + } +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/config/MQTTStubConfigurations.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/config/MQTTStubConfigurations.java new file mode 100644 index 0000000000..d170dc94c7 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/config/MQTTStubConfigurations.java @@ -0,0 +1,49 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Configurations related to MQTT Stub. + */ +@XmlRootElement(name = "MQTTConfiguration") +public class MQTTStubConfigurations { + private String adminUsername; + private String adminPassword; + + @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; + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/AuthenticationException.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/AuthenticationException.java new file mode 100644 index 0000000000..ce6a5c2c1e --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/AuthenticationException.java @@ -0,0 +1,74 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.exception; + +public class AuthenticationException extends Exception { + + private static final long serialVersionUID = -3151279311929070297L; + + private String errorMessage; + private int errorCode; + + public AuthenticationException(int errorCode, String message) { + super(message); + this.errorCode = errorCode; + } + + public AuthenticationException(int errorCode, String message, Throwable cause) { + super(message, cause); + this.errorCode = errorCode; + } + + public int getErrorCode() { + return errorCode; + } + + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public AuthenticationException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public AuthenticationException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public AuthenticationException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public AuthenticationException() { + super(); + } + + public AuthenticationException(Throwable cause) { + super(cause); + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/MQTTConfigurationException.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/MQTTConfigurationException.java new file mode 100644 index 0000000000..d11ae3d70d --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/MQTTConfigurationException.java @@ -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.andes.extensions.device.mgt.jaxrs.exception; + +/** + * This class is used to handle MQTT configuration related exceptions + */ +public class MQTTConfigurationException extends Exception { + + private static final long serialVersionUID = -3151279311929070288L; + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + setErrorMessage(errorMessage); + } + + public MQTTConfigurationException(String errorMessage) { + super(errorMessage); + } + + public MQTTConfigurationException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public MQTTConfigurationException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public MQTTConfigurationException() { + super(); + } + + public MQTTConfigurationException(Throwable cause) { + super(cause); + } +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/TopicManagementException.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/TopicManagementException.java new file mode 100644 index 0000000000..5e78f80919 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/exception/TopicManagementException.java @@ -0,0 +1,44 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.exception; + +public class TopicManagementException extends Exception { + + private static final long serialVersionUID = -3151279311929070297L; + + public TopicManagementException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public TopicManagementException(String message, Throwable cause) { + super(message, cause); + } + + public TopicManagementException(String msg) { + super(msg); + } + + public TopicManagementException() { + super(); + } + + public TopicManagementException(Throwable cause) { + super(cause); + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/service/MQTTManagementAdminService.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/service/MQTTManagementAdminService.java new file mode 100644 index 0000000000..614b30f202 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/service/MQTTManagementAdminService.java @@ -0,0 +1,150 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.service; + +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Info; +import io.swagger.annotations.ExtensionProperty; +import io.swagger.annotations.Extension; +import io.swagger.annotations.Tag; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.ResponseHeader; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.apimgt.annotations.api.Scope; +import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.util.Constants; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@SwaggerDefinition( + info = @Info( + version = "1.0.0", + title = "", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = "name", value = "MQTTManagementAdmin"), + @ExtensionProperty(name = "context", value = "/api/mqtt-topics/v1.0/admin/topics"), + }) + } + ), + tags = { + @Tag(name = "device_management", description = "") + } +) +@Path("/admin/topics") +@Api(value = "MQTT Management Administrative Service", description = "This an API intended to be used by " + + "'internal' components to log in as an admin user and view MQTT topics dashboard. " + + "Further, this is strictly restricted to admin users only ") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@Scopes( + scopes = { + @Scope( + name = "Getting Details of Topics", + description = "Getting Details of Topics", + key = "perm:admin:topics:view", + permissions = {"/device-mgt/topics/view"} + ) + } +) +public interface MQTTManagementAdminService { + + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting Details of Topics", + notes = "Get the details of a topic by searching via the topic name, and details.", + response = Device.class, + responseContainer = "List", + tags = "MQTT Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:topics:view") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of topics.", + response = Device.class, + responseContainer = "List", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource was last modified.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest version of the " + + "requested resource.\n"), + @ApiResponse( + code = 401, + message = "Unauthorized.\n The unauthorized access to the requested resource.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found.\n The specified topic does not exist", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the topic list.", + response = ErrorResponse.class) + }) + Response getFilteredSubscriptions( + + @QueryParam("topic_name") String topic_name, + @QueryParam("active") String active, + @QueryParam("durable") String durable, + @ApiParam( + name = "If-Modified-Since", + value = "Checks if the requested variant was modified, since the specified date-time. \n" + + "Provide the value in the following format: EEE, d MMM yyyy HH:mm:ss Z. \n" + + "Example: Mon, 05 Jan 2014 15:10:00 +0200", + required = false) + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.", + required = false, + defaultValue = "0") + @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Provide how many activity details you require from the starting pagination index/offset.", + required = false, + defaultValue = "5") + @QueryParam("limit") int limit); +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/service/impl/MQTTManagementAdminServiceImpl.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/service/impl/MQTTManagementAdminServiceImpl.java new file mode 100644 index 0000000000..dc62c7a401 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/service/impl/MQTTManagementAdminServiceImpl.java @@ -0,0 +1,153 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.service.impl; + +import org.apache.axis2.AxisFault; +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.transport.http.HTTPConstants; +import org.apache.axis2.transport.http.HttpTransportProperties; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.CarbonConstants; +import org.wso2.carbon.andes.core.types.xsd.MQTTSubscription; +import org.wso2.carbon.andes.core.types.xsd.Subscription; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.beans.SubscriptionList; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.exception.MQTTConfigurationException; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.service.MQTTManagementAdminService; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.util.MQTTMgtAPIUtils; +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.util.RequestValidationUtil; +import org.wso2.carbon.andes.mqtt.stub.AndesMQTTAdminServiceBrokerManagerAdminException; +import org.wso2.carbon.andes.mqtt.stub.AndesMQTTAdminServiceStub; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.user.api.UserStoreException; + +import javax.servlet.ServletConfig; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.rmi.RemoteException; +import java.util.*; + +@Path("/admin/topics") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class MQTTManagementAdminServiceImpl implements MQTTManagementAdminService { + + private static final Log log = LogFactory.getLog(MQTTManagementAdminServiceImpl.class); + private static final String PROTOCOL_TYPE = "MQTT"; + private static final String DESTINATION_TYPE = "TOPIC"; + private + @Context + ServletConfig config; + private + @Context + HttpServletRequest request; + + @Override + @GET + public Response getFilteredSubscriptions( + @QueryParam("name") String topic_name, + @QueryParam("active") String active, + @QueryParam("durable") String durable, + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + try { + + String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + RequestValidationUtil.validatePaginationParameters(offset, limit); + AndesMQTTAdminServiceStub andesAdminStub = getAndesMQTTAdminServiceStub(config); + MQTTSubscription mqttSubscription = new MQTTSubscription(); + mqttSubscription.setFilteredNamePattern(""); + mqttSubscription.setDurable(false); + mqttSubscription.setActive(true); + mqttSubscription.setProtocolType(PROTOCOL_TYPE); + mqttSubscription.setDestinationType(DESTINATION_TYPE); + mqttSubscription.setFilteredNameByExactMatch(false); + mqttSubscription.setIdentifierPattern(""); + mqttSubscription.setIdentifierPatternByExactMatch(false); + mqttSubscription.setOwnNodeId("All"); + mqttSubscription.setPageNumber(offset); + mqttSubscription.setSubscriptionCountPerPage(limit); + + if (topic_name != null && !topic_name.isEmpty()) { + mqttSubscription.setFilteredNamePattern(topic_name); + } + if (active != null && !active.isEmpty()) { + mqttSubscription.setActive(Boolean.parseBoolean(active)); + } + if (durable != null && !durable.isEmpty()) { + mqttSubscription.setDurable(Boolean.parseBoolean(durable)); + } + + Subscription[] filteredNormalTopicSubscriptionList = andesAdminStub.getFilteredSubscriptions( + mqttSubscription, tenantDomain); + SubscriptionList topics = new SubscriptionList(); + + if (filteredNormalTopicSubscriptionList != null) { + topics.setList(Arrays.asList(filteredNormalTopicSubscriptionList)); + topics.setCount(filteredNormalTopicSubscriptionList.length); + } + + return Response.status(Response.Status.OK).entity(topics).build(); + } catch (RemoteException | AndesMQTTAdminServiceBrokerManagerAdminException | + UserStoreException | MQTTConfigurationException e) { + String msg = "Error occurred at server side while fetching topic list."; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + } + + /** + * Gets the AndesAdminServices stub. + * + * @param config the servlet configuration + * @return an AndesAdminServiceStub + * @throws AxisFault, UserStoreException + */ + private static AndesMQTTAdminServiceStub getAndesMQTTAdminServiceStub(ServletConfig config) + throws AxisFault, UserStoreException, MQTTConfigurationException { + + String hostName = System.getProperty("mqtt.broker.host"); + String mqttPort = System.getProperty("mqtt.broker.https.port"); + + if (hostName == null || mqttPort == null) { + throw new MQTTConfigurationException("MQTT hostname/port configuration is not available in system " + + "properties"); + } + + String backendServerURL = "https://" + hostName + ":" + mqttPort + + "/services/AndesMQTTAdminService.AndesMQTTAdminServiceHttpsSoap11Endpoint/"; + ConfigurationContext configContext = + (ConfigurationContext) config.getServletContext().getAttribute(CarbonConstants.CONFIGURATION_CONTEXT); + AndesMQTTAdminServiceStub stub = new AndesMQTTAdminServiceStub(configContext, backendServerURL); + + HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties.Authenticator(); + basicAuthentication.setUsername(MQTTMgtAPIUtils.getUserRealm().getRealmConfiguration().getAdminUserName()); + basicAuthentication.setPassword(MQTTMgtAPIUtils.getUserRealm().getRealmConfiguration().getAdminPassword()); + stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, basicAuthentication); + + return stub; + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/Constants.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/Constants.java new file mode 100644 index 0000000000..be969bb61b --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/Constants.java @@ -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.andes.extensions.device.mgt.jaxrs.util; + +/** + * Holds the constants used by DeviceImpl Management Admin web application. + */ +public class Constants { + + public static final String SCOPE = "scope"; + + public final class ErrorMessages { + private ErrorMessages () { throw new AssertionError(); } + + public static final String STATUS_BAD_REQUEST_MESSAGE_DEFAULT = "Bad Request"; + + } + + public final class DeviceConstants { + private DeviceConstants () { throw new AssertionError(); } + + public static final String APPLICATION_JSON = "application/json"; + public static final String HEADER_CONTENT_TYPE = "Content-Type"; + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/InputValidationException.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/InputValidationException.java new file mode 100644 index 0000000000..51d511dd68 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/InputValidationException.java @@ -0,0 +1,35 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.util; + +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.beans.ErrorResponse; + +import javax.ws.rs.BadRequestException; +import javax.ws.rs.core.Response; +import java.io.Serializable; + +public class InputValidationException extends BadRequestException implements Serializable { + + private static final long serialVersionUID = 147843579458906890L; + + public InputValidationException(ErrorResponse error) { + super(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/MQTTMgtAPIUtils.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/MQTTMgtAPIUtils.java new file mode 100644 index 0000000000..725c79450d --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/MQTTMgtAPIUtils.java @@ -0,0 +1,110 @@ +/* + * 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.andes.extensions.device.mgt.jaxrs.util; + +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.device.mgt.analytics.dashboard.GadgetDataService; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.user.api.AuthorizationManager; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.core.service.RealmService; + +import javax.ws.rs.core.MediaType; + +/** + * MDMAPIUtils class provides utility function used by CDM REST-API classes. + */ +public class MQTTMgtAPIUtils { + + public static final MediaType DEFAULT_CONTENT_TYPE = MediaType.APPLICATION_JSON_TYPE; + private static final String NOTIFIER_FREQUENCY = "notifierFrequency"; + private static Log log = LogFactory.getLog(MQTTMgtAPIUtils.class); + + public static RealmService getRealmService() throws UserStoreException { + RealmService realmService; + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); + if (realmService == null) { + String msg = "Realm service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return realmService; + } + + /** + * Getting the current tenant's user realm + */ + public static UserRealm getUserRealm() throws UserStoreException { + RealmService realmService; + UserRealm realm; + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); + + if (realmService == null) { + throw new IllegalStateException("Realm service not initialized"); + } + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + realm = realmService.getTenantUserRealm(tenantId); + return realm; + } + + public static AuthorizationManager getAuthorizationManager() throws UserStoreException { + RealmService realmService; + AuthorizationManager authorizationManager; + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); + if (realmService == null) { + throw new IllegalStateException("Realm service is not initialized."); + } + int tenantId = ctx.getTenantId(); + authorizationManager = realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); + + return authorizationManager; + } + + public static int getTenantId(String tenantDomain) throws DeviceManagementException { + RealmService realmService = + (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); + if (realmService == null) { + throw new IllegalStateException("Realm service has not been initialized."); + } + try { + return realmService.getTenantManager().getTenantId(tenantDomain); + } catch (UserStoreException e) { + throw new DeviceManagementException("Error occured while trying to " + + "obtain tenant id of currently logged in user"); + } + } + + public static String getAuthenticatedUser() { + PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + String username = threadLocalCarbonContext.getUsername(); + String tenantDomain = threadLocalCarbonContext.getTenantDomain(); + if (username != null && username.endsWith(tenantDomain)) { + return username.substring(0, username.lastIndexOf("@")); + } + return username; + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/RequestValidationUtil.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/RequestValidationUtil.java new file mode 100644 index 0000000000..481b5d1002 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/jaxrs/util/RequestValidationUtil.java @@ -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.andes.extensions.device.mgt.jaxrs.util; + +import org.wso2.carbon.andes.extensions.device.mgt.jaxrs.beans.*; + +public class RequestValidationUtil { + public static void validatePaginationParameters(int offset, int limit) { + if (offset < 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter offset is s " + + "negative value.").build()); + } + if (limit < 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter limit is a " + + "negative value.").build()); + } + if (limit > 100) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter limit should" + + " be less than or equal to 100.").build()); + } + + } + +} diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml new file mode 100644 index 0000000000..ed2ed21624 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/META-INF/webapp-classloading.xml @@ -0,0 +1,35 @@ + + + + + + + + + false + + + CXF,Carbon + diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml new file mode 100644 index 0000000000..7262c117b9 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/WEB-INF/web.xml b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..ec7a36b5f4 --- /dev/null +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.api/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,119 @@ + + + + Admin-Webapp + + JAX-WS/JAX-RS Device Management Endpoint + JAX-WS/JAX-RS Servlet + CXFServlet + + org.apache.cxf.transport.servlet.CXFServlet + + + + swagger.security.filter + ApiAuthorizationFilterImpl + + 1 + + + CXFServlet + /* + + + 60 + + + + doAuthentication + false + + + + + managed-api-enabled + true + + + managed-api-owner + admin + + + isSharedWithAllTenants + true + + + + + + DeviceMgt-Admin + /* + + + CONFIDENTIAL + + + + + ApiOriginFilter + org.wso2.carbon.andes.extensions.device.mgt.jaxrs.ApiOriginFilter + + + + HttpHeaderSecurityFilter + org.apache.catalina.filters.HttpHeaderSecurityFilter + + hstsEnabled + false + + + + + ContentTypeBasedCachePreventionFilter + org.wso2.carbon.ui.filters.cache.ContentTypeBasedCachePreventionFilter + + patterns + text/html" ,application/json" ,text/plain + + + filterAction + enforce + + + httpHeaders + Cache-Control: no-store, no-cache, must-revalidate, private + + + + + HttpHeaderSecurityFilter + /* + + + + ContentTypeBasedCachePreventionFilter + /* + + + + ApiOriginFilter + /* + + + diff --git a/components/extensions/mb-extensions/pom.xml b/components/extensions/mb-extensions/pom.xml index bddf02d415..1e18e57b23 100644 --- a/components/extensions/mb-extensions/pom.xml +++ b/components/extensions/mb-extensions/pom.xml @@ -34,6 +34,7 @@ org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization + org.wso2.carbon.andes.extensions.device.mgt.api diff --git a/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/pom.xml b/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/pom.xml new file mode 100644 index 0000000000..5e700478b8 --- /dev/null +++ b/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/pom.xml @@ -0,0 +1,126 @@ + + + + + + + + org.wso2.carbon.devicemgt-plugins + extensions-feature + 4.0.82-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.andes.extensions.device.mgt.api.feature + pom + WSO2 Carbon - MQTT Topics Management API Feature + http://wso2.org + This feature contains the API feature for the MQTT dashboard + + + + + maven-resources-plugin + + + copy-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + build.properties + p2.inf + + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-jaxrs-war + package + + copy + + + + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.andes.extensions.device.mgt.api + war + true + + ${project.build.directory}/maven-shared-archive-resources/webapps/ + + api#mqtt-topics#v1.0.war + + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + p2-feature-generation + package + + p2-feature-gen + + + org.wso2.carbon.andes.extensions.device.mgt.api + ../../../features/etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + org.eclipse.equinox.p2.type.group:true + + + + + + + + + + + + + + + + + diff --git a/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/src/main/resources/build.properties b/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/src/main/resources/build.properties new file mode 100644 index 0000000000..ddedd58dc4 --- /dev/null +++ b/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/src/main/resources/build.properties @@ -0,0 +1,19 @@ +# +# Copyright (c) 2005-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. +# + +custom = true diff --git a/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/src/main/resources/p2.inf b/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/src/main/resources/p2.inf new file mode 100644 index 0000000000..b1f1b35b3f --- /dev/null +++ b/features/extensions-feature/org.wso2.carbon.andes.extensions.device.mgt.api.feature/src/main/resources/p2.inf @@ -0,0 +1,3 @@ +instructions.configure = \ +org.eclipse.equinox.p2.touchpoint.natives.mkdir(path:${installFolder}/../../deployment/server/webapps/);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.andes.extensions.device.mgt.api_${feature.version}/webapps/,target:${installFolder}/../../deployment/server/webapps/,overwrite:true);\ \ No newline at end of file diff --git a/features/extensions-feature/pom.xml b/features/extensions-feature/pom.xml index e4607dda73..9c75a166a3 100644 --- a/features/extensions-feature/pom.xml +++ b/features/extensions-feature/pom.xml @@ -38,6 +38,7 @@ org.wso2.carbon.appmgt.mdm.restconnector.feature org.wso2.carbon.device.mgt.adapter.feature org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization.feature + org.wso2.carbon.andes.extensions.device.mgt.api.feature org.wso2.extension.siddhi.execution.json.feature org.wso2.carbon.device.mgt.notification.listener.feature org.wso2.gpl.siddhi.extension.geo.script.feature diff --git a/pom.xml b/pom.xml index 52c409da0f..9da2c8b573 100644 --- a/pom.xml +++ b/pom.xml @@ -832,6 +832,11 @@ jackson-jaxrs ${jackson.version} + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-annotations + ${jackson-annotations.version} + @@ -1055,6 +1060,11 @@ org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization ${carbon.devicemgt.plugins.version} + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.andes.extensions.device.mgt.api + ${carbon.devicemgt.plugins.version} + commons-lang commons-lang @@ -1075,10 +1085,15 @@ feign-gson ${io.github.openfeign.version} + + io.github.openfeign + feign-slf4j + ${io.github.openfeign.version} + - io.github.openfeign - feign-slf4j - ${io.github.openfeign.version} + org.wso2.carbon.messaging + org.wso2.carbon.andes.mqtt.stub + ${carbon.andes.mqtt.version} @@ -1116,6 +1131,7 @@ [1.6.1.wso2v11, 1.7.0) 1.6.1.wso2v11 + 1.1.0-wso2v10 1.3 @@ -1141,7 +1157,7 @@ 1.1.1 - 3.0.106 + 3.0.112-SNAPSHOT [3.0.0, 4.0.0) @@ -1259,7 +1275,10 @@ [0.0.0,1.0.0) + 3.2.16 3.2.25 + 2.6.1.wso2v1 + 3.2.16 9.3.1