Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api white list and rate limiter for gc #522

Merged
merged 10 commits into from
May 31, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
package com.baidu.hugegraph.api.profile;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.inject.Singleton;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -62,6 +62,7 @@ public class ProfileAPI {
@Timed
@Produces(MediaType.APPLICATION_JSON)
public String getProfile(@Context Application application) {
// May init multi times by multi threads, but no effect on the results
if (SERVER_PROFILES != null) {
return SERVER_PROFILES;
}
Expand All @@ -71,14 +72,14 @@ public String getProfile(@Context Application application) {
profiles.put("version", CoreVersion.VERSION.toString());
profiles.put("doc", DOC);
profiles.put("api_doc", API_DOC);
Set<String> apis = new HashSet<>();
Set<String> apis = new TreeSet<>();
for (Class<?> clazz : application.getClasses()) {
if (!isAnnotatedPathClass(clazz)) {
continue;
}
Resource resource = Resource.from(clazz);
APICategory apiCategory = getCategory(resource.getName());
apis.add(apiCategory.category);
APICategory apiCategory = APICategory.parse(resource.getName());
apis.add(apiCategory.dir);
}
profiles.put("apis", apis);
SERVER_PROFILES = JsonUtil.toJson(profiles);
Expand All @@ -101,7 +102,7 @@ public String showAllAPIs(@Context Application application) {
}

Resource resource = Resource.from(clazz);
APICategory apiCategory = getCategory(resource.getName());
APICategory apiCategory = APICategory.parse(resource.getName());

String url = resource.getPath();
// List all methods of this resource
Expand All @@ -122,26 +123,18 @@ public String showAllAPIs(@Context Application application) {
return API_PROFILES;
}

private static boolean isAnnotatedPathClass(Class rc) {
if (rc.isAnnotationPresent(Path.class)) {
private static boolean isAnnotatedPathClass(Class<?> clazz) {
if (clazz.isAnnotationPresent(Path.class)) {
return true;
}
for (Class clazz : rc.getInterfaces()) {
if (clazz.isAnnotationPresent(Path.class)) {
for (Class<?> i : clazz.getInterfaces()) {
if (i.isAnnotationPresent(Path.class)) {
return true;
}
}
return false;
}

private static APICategory getCategory(String fullName) {
String[] parts = StringUtils.split(fullName, ".");
E.checkState(parts.length >= 2, "Invalid api name");
String dir = parts[parts.length - 2];
String category = parts[parts.length - 1];
return new APICategory(dir, category);
}

private static class APIProfiles {

@JsonProperty("apis")
Expand Down Expand Up @@ -225,5 +218,14 @@ public APICategory(String dir, String category) {
this.dir = dir;
this.category = category;
}

public static APICategory parse(String fullName) {
String[] parts = StringUtils.split(fullName, ".");
E.checkState(parts.length >= 2, "Invalid api name");
String dir = parts[parts.length - 2];
String category = parts[parts.length - 1];
return new APICategory(dir, category);
}

Linary marked this conversation as resolved.
Show resolved Hide resolved
}
}