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/06/11 08:29:11 UTC

[incubator-servicecomb-java-chassis] 02/04: [SCB-203] read upload config

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 7e6897a6fd977fc8aacd44e89a21eb4aaa5d0a6d
Author: wujimin <wu...@huawei.com>
AuthorDate: Thu Jun 7 09:43:53 2018 +0800

    [SCB-203] read upload config
---
 common/common-rest/pom.xml                         |   2 +-
 .../apache/servicecomb/common/rest/RestConst.java  |  12 +++
 .../servicecomb/common/rest/UploadConfig.java      | 102 +++++++++++++++++++++
 .../servicecomb/common/rest/TestUploadConfig.java  |  86 +++++++++++++++++
 .../rest/vertx/AbstractVertxHttpDispatcher.java    |  19 ++--
 5 files changed, 210 insertions(+), 11 deletions(-)

diff --git a/common/common-rest/pom.xml b/common/common-rest/pom.xml
index d94f447..c3a80de 100644
--- a/common/common-rest/pom.xml
+++ b/common/common-rest/pom.xml
@@ -51,7 +51,7 @@
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
-      <artifactId>foundation-metrics</artifactId>
+      <artifactId>foundation-test-scaffolding</artifactId>
     </dependency>
   </dependencies>
 </project>
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
index 4937213..c3b72ab 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java
@@ -58,4 +58,16 @@ public final class RestConst {
   public static final String CONSUMER_HEADER = "servicecomb-rest-consumer-header";
 
   public static final String READ_STREAM_PART = "servicecomb-readStreamPart";
+
+  public static final String UPLOAD_DIR = "cse.uploads.directory";
+
+  // limit of one upload file, only available for servlet rest transport
+  public static final String UPLOAD_MAX_FILE_SIZE = "cse.uploads.maxFileSize";
+
+  // limit of upload request body
+  public static final String UPLOAD_MAX_SIZE = "cse.uploads.maxSize";
+
+  // the size threshold after which files will be written to disk
+  // only available for servlet rest transport
+  public static final String UPLOAD_FILE_SIZE_THRESHOLD = "cse.uploads.fileSizeThreshold";
 }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/UploadConfig.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/UploadConfig.java
new file mode 100644
index 0000000..b487a96
--- /dev/null
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/UploadConfig.java
@@ -0,0 +1,102 @@
+/*
+ * 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.common.rest;
+
+import javax.servlet.MultipartConfigElement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.netflix.config.DynamicPropertyFactory;
+
+public class UploadConfig {
+  private static final Logger LOGGER = LoggerFactory.getLogger(UploadConfig.class);
+
+  /**
+   * null means not support upload
+   */
+  private String location;
+
+  /**
+   * limit of one upload file, only available for servlet rest transport
+   */
+  private long maxFileSize;
+
+  /**
+   * limit of upload request body
+   */
+  private long maxSize;
+
+  /**
+   * the size threshold after which files will be written to disk, only available for servlet rest transport
+   */
+  private int fileSizeThreshold;
+
+  public UploadConfig() {
+    location = DynamicPropertyFactory.getInstance().getStringProperty(RestConst.UPLOAD_DIR, null).get();
+    maxFileSize = DynamicPropertyFactory.getInstance().getLongProperty(RestConst.UPLOAD_MAX_FILE_SIZE, -1L).get();
+    maxSize = DynamicPropertyFactory.getInstance().getLongProperty(RestConst.UPLOAD_MAX_SIZE, -1L).get();
+    fileSizeThreshold = DynamicPropertyFactory.getInstance().getIntProperty(RestConst.UPLOAD_FILE_SIZE_THRESHOLD, 0)
+        .get();
+  }
+
+  public String getLocation() {
+    return location;
+  }
+
+  public void setLocation(String location) {
+    this.location = location;
+  }
+
+  public long getMaxFileSize() {
+    return maxFileSize;
+  }
+
+  public void setMaxFileSize(long maxFileSize) {
+    this.maxFileSize = maxFileSize;
+  }
+
+  public long getMaxSize() {
+    return maxSize;
+  }
+
+  public void setMaxSize(long maxSize) {
+    this.maxSize = maxSize;
+  }
+
+  public int getFileSizeThreshold() {
+    return fileSizeThreshold;
+  }
+
+  public void setFileSizeThreshold(int fileSizeThreshold) {
+    this.fileSizeThreshold = fileSizeThreshold;
+  }
+
+  public MultipartConfigElement toMultipartConfigElement() {
+    String location = DynamicPropertyFactory.getInstance().getStringProperty(RestConst.UPLOAD_DIR, null).get();
+    if (location == null) {
+      LOGGER.info("{} is null, not support upload.", RestConst.UPLOAD_DIR);
+      return null;
+    }
+
+    return new MultipartConfigElement(
+        location,
+        DynamicPropertyFactory.getInstance().getLongProperty(RestConst.UPLOAD_MAX_FILE_SIZE, -1L).get(),
+        DynamicPropertyFactory.getInstance().getLongProperty(RestConst.UPLOAD_MAX_SIZE, -1L).get(),
+        DynamicPropertyFactory.getInstance().getIntProperty(RestConst.UPLOAD_FILE_SIZE_THRESHOLD, 0).get());
+  }
+}
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestUploadConfig.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestUploadConfig.java
new file mode 100644
index 0000000..00d6f74
--- /dev/null
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestUploadConfig.java
@@ -0,0 +1,86 @@
+/*
+ * 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.common.rest;
+
+import javax.servlet.MultipartConfigElement;
+
+import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestUploadConfig {
+  @Before
+  public void setUp() {
+    ArchaiusUtils.resetConfig();
+  }
+
+  @After
+  public void tearDown() {
+    ArchaiusUtils.resetConfig();
+  }
+
+  @Test
+  public void getMultipartConfig_notSupport() {
+    UploadConfig uploadConfig = new UploadConfig();
+
+    Assert.assertNull(uploadConfig.toMultipartConfigElement());
+    Assert.assertEquals(-1L, uploadConfig.getMaxFileSize());
+    Assert.assertEquals(-1L, uploadConfig.getMaxSize());
+    Assert.assertEquals(0, uploadConfig.getFileSizeThreshold());
+  }
+
+  @Test
+  public void getMultipartConfig_default() {
+    ArchaiusUtils.setProperty(RestConst.UPLOAD_DIR, "upload");
+
+    UploadConfig uploadConfig = new UploadConfig();
+    MultipartConfigElement multipartConfigElement = uploadConfig.toMultipartConfigElement();
+
+    Assert.assertEquals("upload", uploadConfig.getLocation());
+    Assert.assertEquals(-1L, uploadConfig.getMaxFileSize());
+    Assert.assertEquals(-1L, uploadConfig.getMaxSize());
+    Assert.assertEquals(0, uploadConfig.getFileSizeThreshold());
+
+    Assert.assertEquals("upload", multipartConfigElement.getLocation());
+    Assert.assertEquals(-1L, multipartConfigElement.getMaxFileSize());
+    Assert.assertEquals(-1L, multipartConfigElement.getMaxRequestSize());
+    Assert.assertEquals(0, multipartConfigElement.getFileSizeThreshold());
+  }
+
+  @Test
+  public void getMultipartConfig_config() {
+    ArchaiusUtils.setProperty(RestConst.UPLOAD_DIR, "upload");
+    ArchaiusUtils.setProperty(RestConst.UPLOAD_MAX_FILE_SIZE, 1);
+    ArchaiusUtils.setProperty(RestConst.UPLOAD_MAX_SIZE, 2);
+    ArchaiusUtils.setProperty(RestConst.UPLOAD_FILE_SIZE_THRESHOLD, 3);
+
+    UploadConfig uploadConfig = new UploadConfig();
+    MultipartConfigElement multipartConfigElement = uploadConfig.toMultipartConfigElement();
+
+    Assert.assertEquals("upload", uploadConfig.getLocation());
+    Assert.assertEquals(1, uploadConfig.getMaxFileSize());
+    Assert.assertEquals(2, uploadConfig.getMaxSize());
+    Assert.assertEquals(3, uploadConfig.getFileSizeThreshold());
+
+    Assert.assertEquals("upload", multipartConfigElement.getLocation());
+    Assert.assertEquals(1, multipartConfigElement.getMaxFileSize());
+    Assert.assertEquals(2, multipartConfigElement.getMaxRequestSize());
+    Assert.assertEquals(3, multipartConfigElement.getFileSizeThreshold());
+  }
+}
diff --git a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/AbstractVertxHttpDispatcher.java b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/AbstractVertxHttpDispatcher.java
index 7999161..5d357c1 100644
--- a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/AbstractVertxHttpDispatcher.java
+++ b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/AbstractVertxHttpDispatcher.java
@@ -17,17 +17,14 @@
 
 package org.apache.servicecomb.transport.rest.vertx;
 
-import static io.vertx.ext.web.handler.BodyHandler.DEFAULT_BODY_LIMIT;
-
 import java.util.List;
 
+import org.apache.servicecomb.common.rest.UploadConfig;
 import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
 import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.netflix.config.DynamicPropertyFactory;
-
 import io.vertx.ext.web.handler.BodyHandler;
 
 public abstract class AbstractVertxHttpDispatcher implements VertxHttpDispatcher {
@@ -38,13 +35,15 @@ public abstract class AbstractVertxHttpDispatcher implements VertxHttpDispatcher
   protected BodyHandler createBodyHandler() {
     RestBodyHandler bodyHandler = new RestBodyHandler();
 
-    String uploadsDirectory =
-        DynamicPropertyFactory.getInstance().getStringProperty("cse.uploads.directory", null).get();
-    bodyHandler.setUploadsDirectory(uploadsDirectory);
+    UploadConfig uploadConfig = new UploadConfig();
+
+    bodyHandler.setUploadsDirectory(uploadConfig.getLocation());
     bodyHandler.setDeleteUploadedFilesOnEnd(true);
-    bodyHandler.setBodyLimit(
-        DynamicPropertyFactory.getInstance().getLongProperty("cse.uploads.maxSize", DEFAULT_BODY_LIMIT).get());
-    LOGGER.info("set uploads directory to {}.", uploadsDirectory);
+    bodyHandler.setBodyLimit(uploadConfig.getMaxSize());
+
+    if (uploadConfig.toMultipartConfigElement() != null) {
+      LOGGER.info("set uploads directory to {}.", uploadConfig.getLocation());
+    }
 
     return bodyHandler;
   }

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.