Skip to content

Commit

Permalink
[ISSUE #784] Fix words misspell, optimize admin http method code (#792)
Browse files Browse the repository at this point in the history
close #784
  • Loading branch information
gzchen008 authored Feb 25, 2022
1 parent cde95c0 commit bab7e11
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
12 changes: 6 additions & 6 deletions eventmesh-admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ EventMesh Administration Module for EventMesh. It manages Admin Service, Configu

## Administration Client Manager APIs

### POST /topicmanage
### POST /topicManage
- Create a new topic if does not exist
- Exposed POST endpoint to create a new topic if it does not exist.
* Url - http://localhost:8081/topicmanage
* Url - http://localhost:8081/topicManage
* sample request payload
```json
{
Expand All @@ -23,12 +23,12 @@ EventMesh Administration Module for EventMesh. It manages Admin Service, Configu
"created_time": "2021-09-03",
}
```
### DELETE /topicmanage/(string: topic)
### DELETE /topicManage/(string: topic)
- Delete a specific topic.
- Exposed DELETE endpoint to remove a specific topic
* URL -
```url
http://localhost:8081/topicmanage/mytopic1
http://localhost:8081/topicManage/mytopic1
```

* Response -
Expand All @@ -39,12 +39,12 @@ EventMesh Administration Module for EventMesh. It manages Admin Service, Configu
}
```

### GET /topicmanage
### GET /topicManage
- Retrieve a list of topics
- Exposed GET endpoint to retrieve all topics
* URL -
```url
http://localhost:8081/topicmanage
http://localhost:8081/topicManage
```
* Response

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.eventmesh.admin.rocketmq;

public class Constants {

public static final String TOPIC_MANAGE_PATH = "/topicManage";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.eventmesh.admin.rocketmq;

public enum HttpMethod {

GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.eventmesh.admin.rocketmq.controller;

import static org.apache.eventmesh.admin.rocketmq.Constants.TOPIC_MANAGE_PATH;

import org.apache.eventmesh.admin.rocketmq.handler.TopicsHandler;

import java.io.IOException;
Expand All @@ -35,7 +37,7 @@ public AdminController() {

public void run(HttpServer server) throws IOException {

server.createContext("/topicmanage", new TopicsHandler());
server.createContext(TOPIC_MANAGE_PATH, new TopicsHandler());

logger.info("EventMesh-Admin Controller server context created successfully");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.eventmesh.admin.rocketmq.handler;

import static org.apache.eventmesh.admin.rocketmq.Constants.TOPIC_MANAGE_PATH;

import org.apache.eventmesh.admin.rocketmq.request.TopicCreateRequest;
import org.apache.eventmesh.admin.rocketmq.response.TopicResponse;
import org.apache.eventmesh.admin.rocketmq.util.JsonUtils;
Expand All @@ -41,7 +43,7 @@ public class TopicsHandler implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {

// create a new topic
if (RequestMapping.postMapping("/topicmanage", httpExchange)) {
if (RequestMapping.postMapping(TOPIC_MANAGE_PATH, httpExchange)) {
createTopicHandler(httpExchange);
return;
}
Expand All @@ -60,7 +62,7 @@ public void createTopicHandler(HttpExchange httpExchange) throws IOException {
try {
String params = NetUtils.parsePostBody(httpExchange);
TopicCreateRequest topicCreateRequest =
JsonUtils.toObject(params, TopicCreateRequest.class);
JsonUtils.toObject(params, TopicCreateRequest.class);
String topic = topicCreateRequest.getName();

if (StringUtils.isBlank(topic)) {
Expand All @@ -74,7 +76,7 @@ public void createTopicHandler(HttpExchange httpExchange) throws IOException {
TopicResponse topicResponse = null;
if (topicResponse != null) {
logger.info("create a new topic: {}", topic);
httpExchange.getResponseHeaders().add("Content-Type", "appication/json");
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.sendResponseHeaders(200, 0);
result = JsonUtils.toJson(topicResponse);
logger.info(result);
Expand All @@ -88,7 +90,7 @@ public void createTopicHandler(HttpExchange httpExchange) throws IOException {
return;
}
} catch (Exception e) {
httpExchange.getResponseHeaders().add("Content-Type", "appication/json");
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.sendResponseHeaders(500, 0);
result = String.format("create topic failed! Server side error");
logger.error(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.eventmesh.admin.rocketmq.util;

import org.apache.eventmesh.admin.rocketmq.HttpMethod;

import org.apache.http.Consts;

import java.io.IOException;
Expand All @@ -29,8 +31,8 @@ public class NetUtils {
public static String parsePostBody(HttpExchange exchange)
throws IOException {
StringBuilder body = new StringBuilder();
if ("post".equalsIgnoreCase(exchange.getRequestMethod())
|| "put".equalsIgnoreCase(exchange.getRequestMethod())) {
if (HttpMethod.POST.name().equalsIgnoreCase(exchange.getRequestMethod())
|| HttpMethod.PUT.name().equalsIgnoreCase(exchange.getRequestMethod())) {
try (InputStreamReader reader =
new InputStreamReader(exchange.getRequestBody(), Consts.UTF_8)) {
char[] buffer = new char[256];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package org.apache.eventmesh.admin.rocketmq.util;

import org.apache.eventmesh.admin.rocketmq.HttpMethod;

import com.sun.net.httpserver.HttpExchange;

public class RequestMapping {

public static boolean postMapping(String value, HttpExchange httpExchange) {
if ("post".equalsIgnoreCase(httpExchange.getRequestMethod())) {
if (HttpMethod.POST.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
Expand All @@ -31,7 +33,7 @@ public static boolean postMapping(String value, HttpExchange httpExchange) {
}

public static boolean getMapping(String value, HttpExchange httpExchange) {
if ("get".equalsIgnoreCase(httpExchange.getRequestMethod())) {
if (HttpMethod.GET.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
Expand All @@ -40,7 +42,7 @@ public static boolean getMapping(String value, HttpExchange httpExchange) {
}

public static boolean putMapping(String value, HttpExchange httpExchange) {
if ("put".equalsIgnoreCase(httpExchange.getRequestMethod())) {
if (HttpMethod.PUT.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
Expand All @@ -49,7 +51,7 @@ public static boolean putMapping(String value, HttpExchange httpExchange) {
}

public static boolean deleteMapping(String value, HttpExchange httpExchange) {
if ("delete".equalsIgnoreCase(httpExchange.getRequestMethod())) {
if (HttpMethod.DELETE.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
Expand Down

0 comments on commit bab7e11

Please sign in to comment.