1. Endy Muhardin
  2. belajar-restful

Commits

Endy Muhardin  committed d78df29

menampilkan informasi aplikasi dan profile

  • Participants
  • Parent commits 30c85df
  • Branches master

Comments (0)

Files changed (5)

File belajar-restful-web/src/main/java/com/artivisi/belajar/restful/ui/controller/HomepageController.java

View file
  • Ignore whitespace
 
 import com.artivisi.belajar.restful.service.BelajarRestfulService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.MessageSource;
 import org.springframework.http.HttpStatus;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.core.session.SessionRegistry;
 import org.springframework.security.core.userdetails.User;
 import org.springframework.stereotype.Controller;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.context.support.WebApplicationContextUtils;
+
+import javax.servlet.http.HttpServletRequest;
 
 /**
  *
 
     @Autowired private BelajarRestfulService belajarRestfulService;
     @Autowired private SessionRegistry sessionRegistry;
+    @Autowired private MessageSource messageSource;
     
     @RequestMapping("/homepage/userinfo")
     @ResponseBody
     public Map<String, String> userInfo(){
         Map<String, String> hasil = new HashMap<String, String>();
-        
+
         Authentication auth = SecurityContextHolder.getContext().getAuthentication();
         if(auth != null){
             Object principal = auth.getPrincipal();
         
         return hasil;
     }
+
+    @RequestMapping("/homepage/appinfo")
+    @ResponseBody
+    public Map<String, String> appInfo(HttpServletRequest request){
+
+        ApplicationContext ctx = WebApplicationContextUtils
+                .getWebApplicationContext(request.getSession().getServletContext());
+
+        Map<String, String> hasil = new HashMap<String, String>();
+
+        hasil.put("profileDefault", StringUtils.arrayToCommaDelimitedString(ctx.getEnvironment().getDefaultProfiles()));
+        hasil.put("profileActive", StringUtils.arrayToCommaDelimitedString(ctx.getEnvironment().getActiveProfiles()));
+        hasil.put("namaAplikasi", messageSource.getMessage("app.name", null, "undefined", null));
+        hasil.put("versiAplikasi", messageSource.getMessage("app.version", null, "x.x.x", null));
+
+        return hasil;
+    }
     
     @RequestMapping("/homepage/sessioninfo")
     @ResponseBody

File belajar-restful-web/src/main/webapp/WEB-INF/web.xml

View file
  • Ignore whitespace
             /WEB-INF/spring-security.xml
         </param-value>
     </context-param>
+    <context-param>
+        <param-name>spring.profiles.default</param-name>
+        <param-value>development</param-value>
+    </context-param>
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

File belajar-restful-web/src/main/webapp/js/aplikasi-controller.js

View file
  • Ignore whitespace
             $scope.userinfo = data;
         });
     }])
-    .controller('AboutController', ['$scope', function($scope){
-        $scope.appName = "Aplikasi Belajar";
-        $scope.appVersion = "Versi 1.0.0";
+    .controller('AboutController', ['$http', '$scope', function($http, $scope){
+        $scope.appinfo = {};
+        $http.get('/homepage/appinfo').success(function(data){
+            $scope.appinfo = data;
+        });
     }])
     .controller('ApplicationSessionsController', ['ApplicationSessionsService', '$scope', function(ApplicationSessionsService, $scope){
         $scope.refresh = function(){

File belajar-restful-web/src/main/webapp/pages/about.html

View file
  • Ignore whitespace
 <div class="well well-large">
     <h1>About</h1>
                     
-    <p class="lead">Nama Aplikasi : {{appName}}</p>
-    <p>Versi Aplikasi : {{appVersion}}</p>
+    <p class="lead">Nama Aplikasi : {{appinfo.namaAplikasi}}</p>
+    <p>Versi Aplikasi : {{appinfo.versiAplikasi}}</p>
+    <p>Profil Aktif: {{appinfo.profileActive}}</p>
+    <p>Profil Default: {{appinfo.profileDefault}}</p>
 </div>

File belajar-restful-web/src/test/java/com/artivisi/belajar/restful/ui/controller/HomepageControllerTestIT.java

View file
  • Ignore whitespace
+/**
+ * Copyright (C) 2011 ArtiVisi Intermedia <info@artivisi.com>
+ *
+ * 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 com.artivisi.belajar.restful.ui.controller;
+
+import com.artivisi.belajar.restful.domain.Role;
+import com.artivisi.belajar.restful.domain.User;
+import com.jayway.restassured.authentication.FormAuthConfig;
+import org.junit.Test;
+
+import static com.jayway.restassured.RestAssured.given;
+import static com.jayway.restassured.RestAssured.with;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItems;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class HomepageControllerTestIT {
+
+    private String target = "http://localhost:10000/homepage";
+    private String login = "http://localhost:10000/j_spring_security_check";
+    private String username = "endy";
+    private String password = "123";
+
+
+    @Test
+    public void testGetAppinfo() {
+        with().header("Accept", "application/json")
+                .auth().form(username, password, new FormAuthConfig(login, "j_username", "j_password"))
+                .expect()
+                .statusCode(200)
+                .body("profileDefault", equalTo("development"),
+                    "profileActive", equalTo(""),
+                    "namaAplikasi", equalTo("Aplikasi Belajar"),
+                    "versiAplikasi", containsString("belajar-restful-")
+                )
+                .when()
+                .get(target + "/" + "appinfo");
+    }
+
+}