You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/04/23 02:26:15 UTC
[incubator-servicecomb-java-chassis] 01/05: [SCB-487] prepare
HttpUtils for parse download file name from http header content-disposition
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit dddbc5da7553494534ab7640ed67475541db151d
Author: wujimin <wu...@huawei.com>
AuthorDate: Sun Apr 22 11:02:35 2018 +0800
[SCB-487] prepare HttpUtils for parse download file name from http header content-disposition
---
.../foundation/common/http/HttpUtils.java | 85 +++++++++++++++++
.../foundation/common/http/TestHttpUtils.java | 104 +++++++++++++++++++++
2 files changed, 189 insertions(+)
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpUtils.java
new file mode 100644
index 0000000..deb6a97
--- /dev/null
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpUtils.java
@@ -0,0 +1,85 @@
+/*
+ * 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.foundation.common.http;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+
+import org.springframework.util.StringUtils;
+
+public final class HttpUtils {
+ private HttpUtils() {
+ }
+
+ /**
+ * paramName is not case sensitive
+ * @param headerValue example: attachment;filename=a.txt
+ *
+ */
+ //
+ public static String parseParamFromHeaderValue(String headerValue, String paramName) {
+ if (StringUtils.isEmpty(headerValue)) {
+ return null;
+ }
+
+ for (String value : headerValue.split(";")) {
+ int idx = value.indexOf('=');
+ if (idx == -1) {
+ continue;
+ }
+
+ if (paramName.equalsIgnoreCase(value.substring(0, idx))) {
+ return value.substring(idx + 1);
+ }
+ }
+ return null;
+ }
+
+ public static String uriEncode(String value) {
+ return uriEncode(value, StandardCharsets.UTF_8.name());
+ }
+
+ public static String uriEncode(String value, String enc) {
+ try {
+ return URLEncoder.encode(value, enc).replace("+", "%20");
+ } catch (UnsupportedEncodingException e) {
+ throw new IllegalStateException(String.format("uriEncode failed, value=\"%s\", enc=\"%s\".", value, enc), e);
+ }
+ }
+
+ public static String uriDecode(String value) {
+ return uriDecode(value, StandardCharsets.UTF_8.name());
+ }
+
+ public static String uriDecode(String value, String enc) {
+ try {
+ return URLDecoder.decode(value, enc);
+ } catch (UnsupportedEncodingException e) {
+ throw new IllegalStateException(String.format("uriDecode failed, value=\"%s\", enc=\"%s\".", value, enc), e);
+ }
+ }
+
+ public static String parseFileNameFromHeaderValue(String headerValue) {
+ String fileName = parseParamFromHeaderValue(headerValue, "filename");
+ fileName = StringUtils.isEmpty(fileName) ? "default" : fileName;
+ fileName = uriDecode(fileName);
+ return new File(fileName).getName();
+ }
+}
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/http/TestHttpUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/http/TestHttpUtils.java
new file mode 100644
index 0000000..18f8906
--- /dev/null
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/http/TestHttpUtils.java
@@ -0,0 +1,104 @@
+/*
+ * 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.foundation.common.http;
+
+import java.io.UnsupportedEncodingException;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+
+public class TestHttpUtils {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void parseParamFromHeaderValue_normal() {
+ Assert.assertEquals("v", HttpUtils.parseParamFromHeaderValue("xx;k=v", "k"));
+ }
+
+ @Test
+ public void parseParamFromHeaderValue_normal_ignoreCase() {
+ Assert.assertEquals("v", HttpUtils.parseParamFromHeaderValue("xx;K=v", "k"));
+ }
+
+ @Test
+ public void parseParamFromHeaderValue_null() {
+ Assert.assertNull(HttpUtils.parseParamFromHeaderValue(null, "k"));
+ }
+
+ @Test
+ public void parseParamFromHeaderValue_noKv() {
+ Assert.assertNull(HttpUtils.parseParamFromHeaderValue("xx", "k"));
+ }
+
+ @Test
+ public void parseParamFromHeaderValue_noV() {
+ Assert.assertEquals("", HttpUtils.parseParamFromHeaderValue("xx;k=", "k"));
+ }
+
+ @Test
+ public void parseParamFromHeaderValue_keyNotFound() {
+ Assert.assertNull(HttpUtils.parseParamFromHeaderValue("xx;k=", "kk"));
+ }
+
+ @Test
+ public void uriEncode_chineseAndSpace() {
+ String encoded = HttpUtils.uriEncode("测 试");
+ Assert.assertEquals("%E6%B5%8B%20%E8%AF%95", encoded);
+ Assert.assertEquals("测 试", HttpUtils.uriDecode(encoded));
+ }
+
+ @Test
+ public void uriEncode_failed() {
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage(Matchers.is("uriEncode failed, value=\"\", enc=\"notExistEnc\"."));
+ expectedException.expectCause(Matchers.instanceOf(UnsupportedEncodingException.class));
+
+ HttpUtils.uriEncode("", "notExistEnc");
+ }
+
+ @Test
+ public void uriDecode_failed() {
+ expectedException.expect(IllegalStateException.class);
+ expectedException
+ .expectMessage(Matchers.is("uriDecode failed, value=\"%E6%B5%8B%20%E8%AF%95\", enc=\"notExistEnc\"."));
+ expectedException.expectCause(Matchers.instanceOf(UnsupportedEncodingException.class));
+
+ HttpUtils.uriDecode("%E6%B5%8B%20%E8%AF%95", "notExistEnc");
+ }
+
+ @Test
+ public void parseFileNameFromHeaderValue() {
+ String fileName = "测 试.txt";
+ String encoded = HttpUtils.uriEncode(fileName);
+ Assert.assertEquals(fileName, HttpUtils.parseFileNameFromHeaderValue("xx;filename=" + encoded));
+ }
+
+ @Test
+ public void parseFileNameFromHeaderValue_defaultName() {
+ Assert.assertEquals("default", HttpUtils.parseFileNameFromHeaderValue("xx"));
+ }
+
+ @Test
+ public void parseFileNameFromHeaderValue_ignorePath() {
+ Assert.assertEquals("a.txt", HttpUtils.parseFileNameFromHeaderValue("xx;filename=../../a.txt"));
+ }
+}
--
To stop receiving notification emails like this one, please contact
liubao@apache.org.