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/servicecomb-samples.git


The following commit(s) were added to refs/heads/master by this push:
     new 8a5d53c  replace spring 5 deprecated classes (#41)
8a5d53c is described below

commit 8a5d53cf0204b79762afcfebb26fdce4dbb5a261
Author: bao liu <[email protected]>
AuthorDate: Tue Feb 11 09:00:39 2020 +0800

    replace spring 5 deprecated classes (#41)
---
 .../samples/porter/gateway/AuthHandler.java        | 57 +++++++++-------------
 .../samples/porter/gateway/UserServiceClient.java  | 39 +++++++++++++++
 .../src/main/resources/ui/js/upload.js             |  2 +-
 3 files changed, 64 insertions(+), 34 deletions(-)

diff --git 
a/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/AuthHandler.java
 
b/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/AuthHandler.java
index 13a7e9a..4d1b8ec 100644
--- 
a/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/AuthHandler.java
+++ 
b/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/AuthHandler.java
@@ -17,26 +17,24 @@
 
 package org.apache.servicecomb.samples.porter.gateway;
 
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.servicecomb.core.Handler;
 import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.foundation.common.utils.BeanUtils;
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
-import 
org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncRestTemplate;
 import org.apache.servicecomb.samples.porter.user.api.SessionInfo;
 import org.apache.servicecomb.swagger.invocation.AsyncResponse;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
-import org.springframework.http.ResponseEntity;
-import org.springframework.util.concurrent.ListenableFuture;
-import org.springframework.util.concurrent.ListenableFutureCallback;
 
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
 
 
 public class AuthHandler implements Handler {
-  private CseAsyncRestTemplate restTemplate = new CseAsyncRestTemplate();
+  private UserServiceClient userServiceClient = 
BeanUtils.getBean("UserServiceClient");
 
   // session expires in 10 minutes, cache for 1 seconds to get rid of 
concurrent scenarios.
   private Cache<String, String> sessionCache = CacheBuilder.newBuilder()
@@ -71,34 +69,27 @@ public class AuthHandler implements Handler {
       }
 
       // In edge, handler is executed in reactively. Must have no blocking 
logic.
-      ListenableFuture<ResponseEntity<SessionInfo>> sessionInfoFuture =
-          
restTemplate.getForEntity("cse://user-service/v1/user/session?sessionId=" + 
sessionId, SessionInfo.class);
-      sessionInfoFuture.addCallback(
-          new ListenableFutureCallback<ResponseEntity<SessionInfo>>() {
-            @Override
-            public void onFailure(Throwable ex) {
-              asyncResponse.complete(Response.failResp(new 
InvocationException(403, "", "session is not valid.")));
-            }
-
-            @Override
-            public void onSuccess(ResponseEntity<SessionInfo> result) {
-              SessionInfo sessionInfo = result.getBody();
-              if (sessionInfo == null) {
-                asyncResponse.complete(Response.failResp(new 
InvocationException(403, "", "session is not valid.")));
-                return;
-              }
-              try {
-                // session info stored in InvocationContext. Microservices can 
get it. 
-                invocation.addContext("session-id", sessionId);
-                String sessionInfoStr = 
JsonUtils.writeValueAsString(sessionInfo);
-                invocation.addContext("session-info", sessionInfoStr);
-                invocation.next(asyncResponse);
-                sessionCache.put(sessionId, sessionInfoStr);
-              } catch (Exception e) {
-                asyncResponse.complete(Response.failResp(new 
InvocationException(500, "", e.getMessage())));
-              }
-            }
-          });
+      CompletableFuture<SessionInfo> result = 
userServiceClient.getGetSessionOperation().getSession(sessionId);
+      result.whenComplete((info, e) -> {
+        if (result.isCompletedExceptionally()) {
+          asyncResponse.complete(Response.failResp(new 
InvocationException(403, "", "session is not valid.")));
+        } else {
+          if (info == null) {
+            asyncResponse.complete(Response.failResp(new 
InvocationException(403, "", "session is not valid.")));
+            return;
+          }
+          try {
+            // session info stored in InvocationContext. Microservices can get 
it. 
+            invocation.addContext("session-id", sessionId);
+            String sessionInfoStr = JsonUtils.writeValueAsString(info);
+            invocation.addContext("session-info", sessionInfoStr);
+            invocation.next(asyncResponse);
+            sessionCache.put(sessionId, sessionInfoStr);
+          } catch (Exception ee) {
+            asyncResponse.complete(Response.failResp(new 
InvocationException(500, "", ee.getMessage())));
+          }
+        }
+      });
     }
   }
 }
diff --git 
a/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/UserServiceClient.java
 
b/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/UserServiceClient.java
new file mode 100644
index 0000000..701a357
--- /dev/null
+++ 
b/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/UserServiceClient.java
@@ -0,0 +1,39 @@
+/*
+ * 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.samples.porter.gateway;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.servicecomb.provider.pojo.RpcReference;
+import org.apache.servicecomb.samples.porter.user.api.SessionInfo;
+import org.springframework.stereotype.Component;
+
+interface GetSessionOperation {
+  CompletableFuture<SessionInfo> getSession(String sessionId);
+}
+
+
+@Component("UserServiceClient")
+public class UserServiceClient {
+  @RpcReference(microserviceName = "user-service", schemaId = "user")
+  private GetSessionOperation getSessionOperation;
+
+  public GetSessionOperation getGetSessionOperation() {
+    return getSessionOperation;
+  }
+}
diff --git 
a/porter_lightweight/gateway-service/src/main/resources/ui/js/upload.js 
b/porter_lightweight/gateway-service/src/main/resources/ui/js/upload.js
index 7d66555..34b9c34 100644
--- a/porter_lightweight/gateway-service/src/main/resources/ui/js/upload.js
+++ b/porter_lightweight/gateway-service/src/main/resources/ui/js/upload.js
@@ -27,7 +27,7 @@ function uploadAction() {
         success: function (data) {
             console.log(data);
             var error = document.getElementById("error");
-            error.textContent="Upload Successfully";
+            error.textContent="Upload Successfully, file id=" + data;
             error.hidden=false;
         },
         error: function(data) {

Reply via email to