Skip to content

Commit

Permalink
Merge 72289dd into a2bbd88
Browse files Browse the repository at this point in the history
  • Loading branch information
acsukesh committed Jun 25, 2018
2 parents a2bbd88 + 72289dd commit 682a0c7
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 36 deletions.
Expand Up @@ -24,9 +24,16 @@ public abstract class AbstractParamProcessor implements ParamValueProcessor {

protected JavaType targetType;

public AbstractParamProcessor(String paramPath, JavaType targetType) {
protected Object defaultValue;

public Object getDefaultValue() {
return defaultValue;
}

public AbstractParamProcessor(String paramPath, JavaType targetType, Object defaultValue) {
this.paramPath = paramPath;
this.targetType = targetType;
this.defaultValue = defaultValue;
}

public String getParameterPath() {
Expand Down
Expand Up @@ -28,14 +28,15 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;

import io.swagger.models.parameters.CookieParameter;
import io.swagger.models.parameters.Parameter;

public class CookieProcessorCreator implements ParamValueProcessorCreator {
public static final String PARAMTYPE = "cookie";

public static class CookieProcessor extends AbstractParamProcessor {
public CookieProcessor(String paramPath, JavaType targetType) {
super(paramPath, targetType);
public CookieProcessor(String paramPath, JavaType targetType, Object defaultValue) {
super(paramPath, targetType, defaultValue);
}

@Override
Expand All @@ -49,6 +50,12 @@ public Object getValue(HttpServletRequest request) throws Exception {
for (Cookie cookie : cookies) {
if (paramPath.equals(cookie.getName())) {
value = cookie.getValue();
if (value == null || value.equals("")) {
Object defaultValue = getDefaultValue();
if (defaultValue != null) {
value = defaultValue.toString();
}
}
}
}

Expand All @@ -73,6 +80,6 @@ public CookieProcessorCreator() {
@Override
public ParamValueProcessor create(Parameter parameter, Type genericParamType) {
JavaType targetType = TypeFactory.defaultInstance().constructType(genericParamType);
return new CookieProcessor(parameter.getName(), targetType);
return new CookieProcessor(parameter.getName(), targetType, ((CookieParameter) parameter).getDefaultValue());
}
}
Expand Up @@ -37,8 +37,8 @@ public class FormProcessorCreator implements ParamValueProcessorCreator {
public static final String PARAMTYPE = "formData";

public static class FormProcessor extends AbstractParamProcessor {
public FormProcessor(String paramPath, JavaType targetType) {
super(paramPath, targetType);
public FormProcessor(String paramPath, JavaType targetType, Object defaultValue) {
super(paramPath, targetType, defaultValue);
}

@Override
Expand Down Expand Up @@ -76,18 +76,18 @@ public ParamValueProcessor create(Parameter parameter, Type genericParamType) {
JavaType targetType = TypeFactory.defaultInstance().constructType(genericParamType);

if (isPart(parameter)) {
return new PartProcessor(parameter.getName(), targetType);
return new PartProcessor(parameter.getName(), targetType, ((FormParameter) parameter).getDefaultValue());
}
return new FormProcessor(parameter.getName(), targetType);
return new FormProcessor(parameter.getName(), targetType, ((FormParameter) parameter).getDefaultValue());
}

private boolean isPart(Parameter parameter) {
return new FileProperty().getType().equals(((FormParameter) parameter).getType());
}

private static class PartProcessor extends AbstractParamProcessor {
PartProcessor(String paramPath, JavaType targetType) {
super(paramPath, targetType);
PartProcessor(String paramPath, JavaType targetType, Object defaultValue) {
super(paramPath, targetType, defaultValue);
}

@Override
Expand Down
Expand Up @@ -31,6 +31,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;

import io.swagger.models.parameters.HeaderParameter;
import io.swagger.models.parameters.Parameter;

public class HeaderProcessorCreator implements ParamValueProcessorCreator {
Expand All @@ -39,8 +40,8 @@ public class HeaderProcessorCreator implements ParamValueProcessorCreator {
public static final String PARAMTYPE = "header";

public static class HeaderProcessor extends AbstractParamProcessor {
public HeaderProcessor(String paramPath, JavaType targetType) {
super(paramPath, targetType);
public HeaderProcessor(String paramPath, JavaType targetType, Object defaultValue) {
super(paramPath, targetType, defaultValue);
}

@Override
Expand All @@ -55,6 +56,12 @@ public Object getValue(HttpServletRequest request) throws Exception {
value = Collections.list(headerValues);
} else {
value = request.getHeader(paramPath);
if (value == null || value.equals("")) {
Object defaultValue = getDefaultValue();
if (defaultValue != null) {
value = defaultValue;
}
}
}

return convertValue(value, targetType);
Expand Down Expand Up @@ -83,6 +90,6 @@ public HeaderProcessorCreator() {
@Override
public ParamValueProcessor create(Parameter parameter, Type genericParamType) {
JavaType targetType = TypeFactory.defaultInstance().constructType(genericParamType);
return new HeaderProcessor(parameter.getName(), targetType);
return new HeaderProcessor(parameter.getName(), targetType, ((HeaderParameter) parameter).getDefaultValue());
}
}
Expand Up @@ -30,13 +30,14 @@
import com.fasterxml.jackson.databind.type.TypeFactory;

import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;

public class PathProcessorCreator implements ParamValueProcessorCreator {
public static final String PARAMTYPE = "path";

public static class PathProcessor extends AbstractParamProcessor {
public PathProcessor(String paramPath, JavaType targetType) {
super(paramPath, targetType);
public PathProcessor(String paramPath, JavaType targetType, Object defaultValue) {
super(paramPath, targetType, defaultValue);
}

@Override
Expand Down Expand Up @@ -72,6 +73,6 @@ public PathProcessorCreator() {
@Override
public ParamValueProcessor create(Parameter parameter, Type genericParamType) {
JavaType targetType = TypeFactory.defaultInstance().constructType(genericParamType);
return new PathProcessor(parameter.getName(), targetType);
return new PathProcessor(parameter.getName(), targetType, ((PathParameter) parameter).getDefaultValue());
}
}
Expand Up @@ -27,13 +27,14 @@
import com.fasterxml.jackson.databind.type.TypeFactory;

import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.QueryParameter;

public class QueryProcessorCreator implements ParamValueProcessorCreator {
public static final String PARAMTYPE = "query";

public static class QueryProcessor extends AbstractParamProcessor {
public QueryProcessor(String paramPath, JavaType targetType) {
super(paramPath, targetType);
public QueryProcessor(String paramPath, JavaType targetType, Object defaultValue) {
super(paramPath, targetType, defaultValue);
}

@Override
Expand All @@ -43,6 +44,12 @@ public Object getValue(HttpServletRequest request) throws Exception {
value = request.getParameterValues(paramPath);
} else {
value = request.getParameter(paramPath);
if (value == null || value.equals("")) {
Object defaultValue = getDefaultValue();
if (defaultValue != null) {
value = defaultValue;
}
}
}

return convertValue(value, targetType);
Expand All @@ -66,6 +73,6 @@ public QueryProcessorCreator() {
@Override
public ParamValueProcessor create(Parameter parameter, Type genericParamType) {
JavaType targetType = TypeFactory.defaultInstance().constructType(genericParamType);
return new QueryProcessor(parameter.getName(), targetType);
return new QueryProcessor(parameter.getName(), targetType, ((QueryParameter) parameter).getDefaultValue());
}
}
Expand Up @@ -45,7 +45,7 @@ public class TestCookieProcessor {
RestClientRequest clientRequest;

private CookieProcessor createProcessor(String name, Class<?> type) {
return new CookieProcessor(name, TypeFactory.defaultInstance().constructType(type));
return new CookieProcessor(name, TypeFactory.defaultInstance().constructType(type), null);
}

private void createClientRequest() {
Expand Down
Expand Up @@ -48,7 +48,7 @@ public class TestFormProcessor {
RestClientRequest clientRequest;

private FormProcessor createProcessor(String name, Class<?> type) {
return new FormProcessor(name, TypeFactory.defaultInstance().constructType(type));
return new FormProcessor(name, TypeFactory.defaultInstance().constructType(type), null);
}

private void createClientRequest() {
Expand Down Expand Up @@ -147,7 +147,8 @@ public void testGetValueList() throws Exception {
};

ParamValueProcessor processor =
new FormProcessor("name", TypeFactory.defaultInstance().constructCollectionType(List.class, String.class));
new FormProcessor("name", TypeFactory.defaultInstance().constructCollectionType(List.class, String.class),
null);
Object value = processor.getValue(request);
Assert.assertThat((List<String>) value, Matchers.contains("value"));
}
Expand All @@ -163,7 +164,7 @@ public void testGetValueSet() throws Exception {
};

ParamValueProcessor processor =
new FormProcessor("name", TypeFactory.defaultInstance().constructCollectionType(Set.class, String.class));
new FormProcessor("name", TypeFactory.defaultInstance().constructCollectionType(Set.class, String.class), null);
Object value = processor.getValue(request);
Assert.assertThat((Set<String>) value, Matchers.contains("value"));
}
Expand Down
Expand Up @@ -49,7 +49,7 @@ public class TestHeaderProcessor {
RestClientRequest clientRequest;

private HeaderProcessor createProcessor(String name, Class<?> type) {
return new HeaderProcessor(name, TypeFactory.defaultInstance().constructType(type));
return new HeaderProcessor(name, TypeFactory.defaultInstance().constructType(type), null);
}

private void createClientRequest() {
Expand Down Expand Up @@ -132,7 +132,8 @@ public void testGetValueList() throws Exception {
};

HeaderProcessor processor =
new HeaderProcessor("h1", TypeFactory.defaultInstance().constructCollectionType(List.class, String.class));
new HeaderProcessor("h1", TypeFactory.defaultInstance().constructCollectionType(List.class, String.class),
null);
Object value = processor.getValue(request);
Assert.assertThat((List<String>) value, Matchers.contains("h1v"));
}
Expand All @@ -148,7 +149,7 @@ public void testGetValueSet() throws Exception {
};

HeaderProcessor processor =
new HeaderProcessor("h1", TypeFactory.defaultInstance().constructCollectionType(Set.class, String.class));
new HeaderProcessor("h1", TypeFactory.defaultInstance().constructCollectionType(Set.class, String.class), null);
Object value = processor.getValue(request);
Assert.assertThat((Set<String>) value, Matchers.contains("h1v"));
}
Expand Down
Expand Up @@ -43,7 +43,7 @@ public class TestPathProcessor {
ParamValueProcessor processor;

private void createProcessor(String name, Class<?> type) {
processor = new PathProcessor(name, TypeFactory.defaultInstance().constructType(type));
processor = new PathProcessor(name, TypeFactory.defaultInstance().constructType(type), null);
}

private void prepareGetValue(String name, Class<?> type) {
Expand Down
Expand Up @@ -34,7 +34,7 @@ public class TestQueryProcessor {
HttpServletRequest request;

private ParamValueProcessor createProcessor(String name, Class<?> type) {
return new QueryProcessor(name, TypeFactory.defaultInstance().constructType(type));
return new QueryProcessor(name, TypeFactory.defaultInstance().constructType(type), null);
}

@Test
Expand Down
Expand Up @@ -83,6 +83,8 @@ public static void run() {
testController(templateUrlWithServiceName, microserviceName);

testController();

testDefaultValues(templateUrlWithServiceName, microserviceName);
}
HttpHeaders headers = new HttpHeaders();
headers.set("Accept-Encoding", "gzip");
Expand All @@ -103,12 +105,11 @@ public static void run() {
@SuppressWarnings("unchecked")
Map<String, Double> metrics = restTemplate.getForObject(prefix + "/metrics", Map.class);

// TestMgr.check(true, metrics.get("jvm(name=heapUsed,statistic=gauge)") != 0);
// TestMgr.check(true, metrics.get("jvm(name=heapUsed,statistic=gauge)") != 0);
TestMgr.check(true, metrics.size() > 0);
TestMgr.check(true,
metrics.get(
"servicecomb.invocation(operation=springmvc.codeFirst.saySomething,role=PRODUCER,stage=total,statistic=count,status=200,transport=highway)")
>= 0);
"servicecomb.invocation(operation=springmvc.codeFirst.saySomething,role=PRODUCER,stage=total,statistic=count,status=200,transport=highway)") >= 0);

//prometheus integration test
try {
Expand Down Expand Up @@ -217,4 +218,20 @@ private static void testConfigurationDuplicate() {
TestMgr.check(DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.test.duplicate1", "wrong").get(),
"newer");
}

private static void testDefaultValues(RestTemplate template, String microserviceName) {
String prefix = "cse://" + microserviceName;

TestMgr.check("hi test your age is : 20",
template.getForObject(prefix + "/default/sayhi",
String.class));

TestMgr.check("20",
template.getForObject(prefix + "/default/add",
String.class));

TestMgr.check("hei test",
template.getForObject(prefix + "/default/sayhei",
String.class));
}
}
@@ -0,0 +1,54 @@
/*
* 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.servicecomb.demo.springmvc.server;

import javax.ws.rs.core.MediaType;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

@RestSchema(schemaId = "default")
@RequestMapping(path = "/springmvc/default", produces = MediaType.APPLICATION_JSON)
public class DefaultValues {
@GetMapping(path = "/add")
public int add(@RequestParam(name = "a", defaultValue = "10") int a,
@RequestParam(name = "b", defaultValue = "10") int b) {
return a + b;
}

@RequestMapping(path = "/sayhei", method = RequestMethod.GET)
public String sayHei(@RequestHeader(name = "name", defaultValue = "test") String name) {
return "hei " + name;
}

@GetMapping(path = "/sayhi")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", paramType = "query", dataType = "string", defaultValue = "test"),
@ApiImplicitParam(name = "age", paramType = "query", dataType = "integer", defaultValue = "20")
})
public String sayHi(String name, int age) {
return "hi " + name + " your age is : " + age;
}
}

0 comments on commit 682a0c7

Please sign in to comment.