View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.samples.spring.web;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import javax.annotation.Resource;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.springframework.stereotype.Component;
28  import org.springframework.web.bind.annotation.RequestMapping;
29  import org.springframework.web.bind.annotation.RequestMethod;
30  import org.springframework.web.servlet.ModelAndView;
31  import org.springframework.web.servlet.mvc.AbstractController;
32  
33  import org.apache.shiro.SecurityUtils;
34  import org.apache.shiro.session.Session;
35  import org.apache.shiro.subject.Subject;
36  
37  /**
38   * Controller used to dynamically build a JNLP file used to launch the Shiro
39   * Spring WebStart sample application.
40   *
41   * @since 0.1
42   */
43  @Component
44  @RequestMapping("/s/shiro.jnlp")
45  public class JnlpController extends AbstractController {
46  
47      /*--------------------------------------------
48      |             C O N S T A N T S             |
49      ============================================*/
50  
51      /*--------------------------------------------
52      |    I N S T A N C E   V A R I A B L E S    |
53      ============================================*/
54      private String jnlpView = "shiro.jnlp";
55  
56      /*--------------------------------------------
57      |         C O N S T R U C T O R S           |
58      ============================================*/
59  
60      /*--------------------------------------------
61      |  A C C E S S O R S / M O D I F I E R S    |
62      ============================================*/
63  
64      public void setJnlpView(String jnlpView) {
65          this.jnlpView = jnlpView;
66      }
67  
68      /*--------------------------------------------
69      |               M E T H O D S               |
70      ============================================*/
71  
72      @RequestMapping(method = RequestMethod.GET)
73      protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
74  
75          Subject subject = SecurityUtils.getSubject();
76          Session session = null;
77  
78          if (subject != null) {
79              session = subject.getSession();
80          }
81          if (session == null) {
82              String msg = "Expected a non-null Shiro session.";
83              throw new IllegalArgumentException(msg);
84          }
85  
86          StringBuilder sb = new StringBuilder();
87          sb.append("http://");
88          sb.append(request.getServerName());
89          if (request.getServerPort() != 80) {
90              sb.append(":");
91              sb.append(request.getServerPort());
92          }
93          sb.append(request.getContextPath());
94  
95          // prevent JNLP caching by setting response headers
96          response.setHeader("cache-control", "no-cache");
97          response.setHeader("pragma", "no-cache");
98  
99          Map<String, Object> model = new HashMap<String, Object>();
100         model.put("codebaseUrl", sb.toString());
101         model.put("sessionId", session.getId());
102         return new ModelAndView(jnlpView, model);
103     }
104 }