Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -36,7 +36,7 @@
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.vertx.ext.web.impl.Utils;
import io.vertx.ext.web.impl.MimeTypesUtils;

public class RestOperationMeta {
private static final Logger LOGGER = LoggerFactory.getLogger(RestOperationMeta.class);
Expand Down Expand Up @@ -209,7 +209,7 @@ public ProduceProcessor ensureFindProduceProcessor(String acceptType) {
return defaultProcessor;
}

List<String> mimeTyps = Utils.getSortedAcceptableMimeTypes(acceptType.toLowerCase(Locale.US));
List<String> mimeTyps = MimeTypesUtils.getSortedAcceptableMimeTypes(acceptType.toLowerCase(Locale.US));
for (String mime : mimeTyps) {
ProduceProcessor processor = this.produceProcessorMap.get(mime);
if (null != processor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ public String call() {
protected final Func0<String> getTpsAndLatency = new Func0<String>() {
@Override
public String call() {
Collection<HystrixCommandMetrics> instances = HystrixCommandMetrics.getInstances();
List<TpsAndLatencyData> tpsAndLatencyData = HystrixCommandMetrics.getInstances().stream().map(instance ->
new TpsAndLatencyData(instance.getRollingCount(HystrixEventType.SUCCESS),
instance.getRollingCount(HystrixEventType.FAILURE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,21 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.servo.Metric;

@Component
public class SimpleMetricsContentConvertor implements MetricsContentConvertor {

private static final Logger logger = LoggerFactory.getLogger(SimpleMetricsContentConvertor.class);
private final ObjectMapper mapper = new ObjectMapper();

@Override
public Map<String, String> convert(List<Metric> metrics) {
Map<String, String> pickedMetrics = new HashMap<>();
for (Metric metric : metrics) {
if (isTotalRequestInstanceLevelMetric(metric.getConfig().getName())) {
pickedMetrics.put(metric.getConfig().getName().replace(" INSTANCE_LEVEL", ""), metric.getValue().toString());
} else if ("RequestQueueRelated".equals(metric.getConfig().getName())) {
String instanceContent = metric.getValue().toString()
String instanceContent = metric.getValue()
.toString()
.substring(metric.getValue().toString().indexOf("InstanceLevel={") + "InstanceLevel={".length());
instanceContent = instanceContent.substring(0, instanceContent.indexOf('}'));
String[] keyAndValueStrings = instanceContent.split(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.servicecomb.foundation.metrics.output.servo.MetricsContentFormatter;

public class TestFileOutputMetricObserverAndContentConvertor {
@SuppressWarnings("unchecked")
@Test
public void testMetricObserverUpdateImpl() {

Expand Down Expand Up @@ -75,6 +76,7 @@ public Map<String, String> format(Map<String, String> input) {
FileOutputMetricObserver observer = new FileOutputMetricObserver(output, convertor, formatter);
observer.updateImpl(metrics);

@SuppressWarnings("rawtypes")
ArgumentCaptor<Map> outputMetrics = ArgumentCaptor.forClass(Map.class);
verify(output).output(outputMetrics.capture());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package io.servicecomb.foundation.vertx;

import io.vertx.core.AsyncResultHandler;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;

public interface AsyncResultCallback<T> extends AsyncResultHandler<T> {
public interface AsyncResultCallback<T> extends Handler<AsyncResult<T>> {
default void success(T data) {
handle(Future.succeededFuture(data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.parsetools.RecordParser;
import io.vertx.core.parsetools.impl.RecordParserImpl;

/**
* TcpParser
Expand Down Expand Up @@ -70,7 +69,7 @@ public TcpParser(TcpBufferHandler output) {
* 在解析出错时,通过重新创建parser对象,将整个缓冲区重置
*/
protected void reset() {
parser = RecordParserImpl.newFixed(TCP_HEADER_LENGTH, this::onParse);
parser = RecordParser.newFixed(TCP_HEADER_LENGTH, this::onParse);
status = ParseStatus.TCP_HEADER;

parser.handle(Buffer.buffer(0));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.vertx.ext.web.impl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add license header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;

// copy from old io.vertx.ext.web.impl.Utils
// because new vertx deleted getSortedAcceptableMimeTypes, and did not log the reason
public class MimeTypesUtils {
private static final Pattern COMMA_SPLITTER = Pattern.compile(" *, *");

private static final Pattern SEMICOLON_SPLITTER = Pattern.compile(" *; *");

private static final Pattern EQUAL_SPLITTER = Pattern.compile(" *= *");

private static final Comparator<String> ACCEPT_X_COMPARATOR = new Comparator<String>() {
float getQuality(String s) {
if (s == null) {
return 0;
}

String[] params = SEMICOLON_SPLITTER.split(s);
for (int i = 1; i < params.length; i++) {
String[] q = EQUAL_SPLITTER.split(params[1]);
if ("q".equals(q[0])) {
return Float.parseFloat(q[1]);
}
}
return 1;
}

@Override
public int compare(String o1, String o2) {
float f1 = getQuality(o1);
float f2 = getQuality(o2);
return Float.compare(f2, f1);
}
};

public static List<String> getSortedAcceptableMimeTypes(String acceptHeader) {
// accept anything when accept is not present
if (acceptHeader == null) {
return Collections.emptyList();
}

// parse
String[] items = COMMA_SPLITTER.split(acceptHeader);
// sort on quality
Arrays.sort(items, ACCEPT_X_COMPARATOR);

List<String> list = new ArrayList<>(items.length);

for (String item : items) {
// find any ; e.g.: "application/json;q=0.8"
int space = item.indexOf(';');

if (space != -1) {
list.add(item.substring(0, space));
} else {
list.add(item);
}
}

return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.net.impl.NetSocketImpl;
import io.vertx.core.net.impl.SocketAddressImpl;
import mockit.Deencapsulation;
Expand Down Expand Up @@ -71,6 +72,23 @@ public NetClient connect(int port, String host, Handler<AsyncResult<NetSocket>>
@Override
public void close() {
}

@Override
public NetClient connect(int port, String host, String serverName,
Handler<AsyncResult<NetSocket>> connectHandler) {
return null;
}

@Override
public NetClient connect(SocketAddress remoteAddress, Handler<AsyncResult<NetSocket>> connectHandler) {
return null;
}

@Override
public NetClient connect(SocketAddress remoteAddress, String serverName,
Handler<AsyncResult<NetSocket>> connectHandler) {
return null;
}
};
TcpClientConnection oTcpClient =
new TcpClientConnection(Mockito.mock(Context.class), oNetClient, "highway://127.2.0.1:8080",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed 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 io.vertx.ext.web.impl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


import static org.junit.Assert.assertEquals;

import java.util.Collections;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

public class TestMimeTypesUtils {
@Test
public void testSortedAcceptableMimeTypes1() {
String accept = "text/html";
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(accept);
assertEquals(1, types.size());
assertEquals("text/html", types.get(0));
}

@Test
public void testSortedAcceptableMimeTypes2() {
String accept = "text/html, application/json";
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(accept);
assertEquals(2, types.size());
assertEquals("text/html", types.get(0));
assertEquals("application/json", types.get(1));
}

@Test
public void testSortedAcceptableMimeTypes3() {
String accept = "text/html,application/json";
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(accept);
assertEquals(2, types.size());
assertEquals("text/html", types.get(0));
assertEquals("application/json", types.get(1));
}

@Test
public void testSortedAcceptableMimeTypes4() {
String accept = "text/html; q=0.8,application/json; q=0.9";
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(accept);
assertEquals(2, types.size());
assertEquals("application/json", types.get(0));
assertEquals("text/html", types.get(1));
}

@Test
public void testSortedAcceptableMimeTypes5() {
String accept = "text/html;q=0.8,application/json;q=0.9";
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(accept);
assertEquals(2, types.size());
assertEquals("application/json", types.get(0));
assertEquals("text/html", types.get(1));
}

@Test
public void testSortedAcceptableMimeTypes6() {
String accept = "text/html; q=0.8,application/json; q=0.9, text/plain";
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(accept);
assertEquals(3, types.size());
assertEquals("text/plain", types.get(0));
assertEquals("application/json", types.get(1));
assertEquals("text/html", types.get(2));
}

@Test
public void testSortedAcceptableMimeTypes7() {
String accept = "text/html;q=0.8,application/json;q=0.9,text/plain";
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(accept);
assertEquals(3, types.size());
assertEquals("text/plain", types.get(0));
assertEquals("application/json", types.get(1));
assertEquals("text/html", types.get(2));
}

@Test
public void getSortedAcceptableMimeTypesNull() {
List<String> types = MimeTypesUtils.getSortedAcceptableMimeTypes(null);
Assert.assertSame(Collections.emptyList(), types);
}
}
4 changes: 2 additions & 2 deletions java-chassis-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<properties>
<jackson.version>2.8.10</jackson.version>
<vertx.version>3.3.3</vertx.version>
<vertx.version>3.5.0</vertx.version>
<tec.zkclient.version>0.8</tec.zkclient.version>
<spring.version>4.3.5.RELEASE</spring.version>
<slf4j.version>1.7.7</slf4j.version>
Expand Down Expand Up @@ -765,7 +765,7 @@
<artifactId>handler-flowcontrol-qps</artifactId>
<version>0.4.1-SNAPSHOT</version>
</dependency>
<dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>handler-publickey-auth</artifactId>
<version>0.4.1-SNAPSHOT</version>
Expand Down