2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.web
.mvc
.provisioning
;
18 import java
.util
.Iterator
;
20 import java
.util
.SortedMap
;
21 import java
.util
.StringTokenizer
;
22 import java
.util
.TreeMap
;
24 import javax
.servlet
.http
.HttpServletRequest
;
25 import javax
.servlet
.http
.HttpServletResponse
;
27 import org
.argeo
.slc
.BasicNameVersion
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.build
.BuildConstants
;
30 import org
.argeo
.slc
.build
.ModularDistribution
;
31 import org
.springframework
.web
.servlet
.handler
.HandlerInterceptorAdapter
;
33 public class ModularDistributionInterceptor
extends HandlerInterceptorAdapter
{
34 private Set
<ModularDistribution
> modularDistributions
;
37 public boolean preHandle(HttpServletRequest request
,
38 HttpServletResponse response
, Object handler
) throws Exception
{
39 String path
= request
.getPathInfo();
40 StringTokenizer stS
= new StringTokenizer(path
, "/");
41 String distributionName
= stS
.nextToken();
42 String distributionVersion
= stS
.nextToken();
44 SortedMap
<String
, ModularDistribution
> choices
= new TreeMap
<String
, ModularDistribution
>();
45 distribs
: for (Iterator
<ModularDistribution
> it
= modularDistributions
46 .iterator(); it
.hasNext();) {
47 ModularDistribution md
= it
.next();
48 if (md
.getName().equals(distributionName
)) {
49 if (distributionVersion
.equals(BuildConstants
.RELEASE
)
50 && md
.getVersion().contains(BuildConstants
.SNAPSHOT
))
53 else if (distributionVersion
.equals(BuildConstants
.LATEST
))
54 choices
.put(md
.getVersion(), md
);
55 else if (distributionVersion
.equals(md
.getVersion())) {
56 choices
.put(md
.getVersion(), md
);
62 if (choices
.size() == 0)
63 throw new SlcException("Cannot find distribution for "
64 + new BasicNameVersion(distributionName
,
65 distributionVersion
));
67 ModularDistribution modularDistribution
= choices
.get(choices
70 request
.setAttribute("modularDistribution", modularDistribution
);
75 public void setModularDistributions(
76 Set
<ModularDistribution
> modularDistributions
) {
77 this.modularDistributions
= modularDistributions
;