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
Show file tree
Hide file tree
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
51 changes: 24 additions & 27 deletions hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/*
* Copyright 2017 HugeGraph Authors
*
* * Copyright 2017 HugeGraph Authors
* *
* * 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.
* 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 com.baidu.hugegraph.api;
Expand Down Expand Up @@ -62,7 +59,7 @@ public class ProfileAPI {
@GET
@Timed
@Produces(MediaType.APPLICATION_JSON)
public String get(@Context Application application) {
public String getProfile(@Context Application application) {
if (SERVER_PROFILES != null) {
return SERVER_PROFILES;
}
Expand All @@ -72,11 +69,11 @@ public String get(@Context Application application) {
profiles.put("doc", DOC);
profiles.put("api-doc", API_DOC);
Set<String> apis = new HashSet<>();
for (Class<?> aClass : application.getClasses()) {
if (!isAnnotatedPathClass(aClass)) {
for (Class<?> clazz : application.getClasses()) {
if (!isAnnotatedPathClass(clazz)) {
continue;
}
Resource resource = Resource.from(aClass);
Resource resource = Resource.from(clazz);
String fullName = resource.getName();
APICategory apiCategory = getCategory(fullName);
apis.add(apiCategory.category);
Expand All @@ -90,18 +87,18 @@ public String get(@Context Application application) {
@Path("apis")
@Timed
@Produces(MediaType.APPLICATION_JSON)
public String showAll(@Context Application application) {
public String showAllAPIs(@Context Application application) {
if (API_PROFILES != null) {
return API_PROFILES;
}

Map<String, Map<String, List<APIProfile>>> apiProfiles = new HashMap<>();
for (Class<?> aClass : application.getClasses()) {
if (!isAnnotatedPathClass(aClass)) {
for (Class<?> clazz : application.getClasses()) {
if (!isAnnotatedPathClass(clazz)) {
continue;
}

Resource resource = Resource.from(aClass);
Resource resource = Resource.from(clazz);
String fullName = resource.getName();
APICategory apiCategory = getCategory(fullName);

Expand Down Expand Up @@ -134,8 +131,8 @@ private static boolean isAnnotatedPathClass(Class rc) {
if (rc.isAnnotationPresent(Path.class)) {
return true;
}
for (Class i : rc.getInterfaces()) {
if (i.isAnnotationPresent(Path.class)) {
for (Class clazz : rc.getInterfaces()) {
if (clazz.isAnnotationPresent(Path.class)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,26 @@ public <V> Number valueToNumber(V value) {
}

public <V> Date valueToDate(V value) {
if (!this.isDate()) {
return null;
}
if (value instanceof Date) {
return (Date) value;
}
if (this.isDate()) {
if (value instanceof Number) {
return new Date(((Number) value).longValue());
} else if (value instanceof String) {
return DateUtil.parse((String) value);
}
} else if (value instanceof Number) {
return new Date(((Number) value).longValue());
} else if (value instanceof String) {
return DateUtil.parse((String) value);
}
return null;
}

public <V> UUID valueToUUID(V value) {
if (!this.isUUID()) {
return null;
}
if (value instanceof UUID) {
return (UUID) value;
}
if (this.isUUID() && value instanceof String) {
} else if (value instanceof String) {
return java.util.UUID.fromString((String) value);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.baidu.hugegraph.unit.core.BackendMutationTest;
import com.baidu.hugegraph.unit.core.CassandraTest;
import com.baidu.hugegraph.unit.core.ConditionQueryFlattenTest;
import com.baidu.hugegraph.unit.core.DataTypeTest;
import com.baidu.hugegraph.unit.core.DirectionsTest;
import com.baidu.hugegraph.unit.core.EdgeIdTest;
import com.baidu.hugegraph.unit.core.JsonUtilTest;
Expand All @@ -47,6 +48,7 @@
CacheManagerTest.class,

VersionTest.class,
DataTypeTest.class,
DirectionsTest.class,
SerialEnumTest.class,
BackendMutationTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.unit.core;

import java.util.Date;
import java.util.UUID;

import org.junit.Test;

import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.testutil.Utils;
import com.baidu.hugegraph.type.define.DataType;

public class DataTypeTest {

@Test
public void testString() {
Assert.assertEquals("object", DataType.OBJECT.string());
Assert.assertEquals("boolean", DataType.BOOLEAN.string());
Assert.assertEquals("byte", DataType.BYTE.string());
Assert.assertEquals("int", DataType.INT.string());
Assert.assertEquals("long", DataType.LONG.string());
Assert.assertEquals("float", DataType.FLOAT.string());
Assert.assertEquals("double", DataType.DOUBLE.string());
Assert.assertEquals("text", DataType.TEXT.string());
Assert.assertEquals("blob", DataType.BLOB.string());
Assert.assertEquals("date", DataType.DATE.string());
Assert.assertEquals("uuid", DataType.UUID.string());
}

@Test
public void testValueToNumber() {
Assert.assertNull(DataType.BOOLEAN.valueToNumber(1));
Assert.assertNull(DataType.INT.valueToNumber("not number"));
Linary marked this conversation as resolved.
Show resolved Hide resolved

Assert.assertEquals((byte) 1, DataType.BYTE.valueToNumber(1));
Assert.assertEquals(1, DataType.INT.valueToNumber(1));
Assert.assertEquals(1, DataType.INT.valueToNumber(1.0F));
Linary marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertEquals(1, DataType.INT.valueToNumber("1"));
Assert.assertEquals(1L, DataType.LONG.valueToNumber(1));
Assert.assertEquals(1.0F, DataType.FLOAT.valueToNumber(1));
Assert.assertEquals(1.0D, DataType.DOUBLE.valueToNumber(1));
}

@Test
public void testValueToDate() {
Date date = Utils.date("2019-01-01 12:00:00");
Assert.assertEquals(date, DataType.DATE.valueToDate(date));
Assert.assertEquals(date,
DataType.DATE.valueToDate("2019-01-01 12:00:00"));
Assert.assertEquals(date, DataType.DATE.valueToDate(date.getTime()));

Assert.assertNull(DataType.TEXT.valueToDate("2019-01-01 12:00:00"));
Assert.assertNull(DataType.DATE.valueToDate(true));
}

@Test
public void testValueToUUID() {
UUID uuid = UUID.randomUUID();
Assert.assertEquals(uuid, DataType.UUID.valueToUUID(uuid));
Assert.assertEquals(uuid, DataType.UUID.valueToUUID(uuid.toString()));

Assert.assertNull(DataType.TEXT.valueToUUID("2019-01-01 12:00:00"));
Assert.assertNull(DataType.UUID.valueToUUID(true));
}
}