2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.web
.mvc
.provisioning
;
19 import java
.util
.Iterator
;
21 import java
.util
.SortedMap
;
22 import java
.util
.StringTokenizer
;
23 import java
.util
.TreeMap
;
25 import javax
.servlet
.http
.HttpServletRequest
;
26 import javax
.servlet
.http
.HttpServletResponse
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.build
.BasicNameVersion
;
30 import org
.argeo
.slc
.build
.BuildConstants
;
31 import org
.argeo
.slc
.build
.ModularDistribution
;
32 import org
.springframework
.web
.servlet
.handler
.HandlerInterceptorAdapter
;
34 public class ModularDistributionInterceptor
extends HandlerInterceptorAdapter
{
35 private Set
<ModularDistribution
> modularDistributions
;
38 public boolean preHandle(HttpServletRequest request
,
39 HttpServletResponse response
, Object handler
) throws Exception
{
40 String path
= request
.getPathInfo();
41 StringTokenizer stS
= new StringTokenizer(path
, "/");
42 String distributionName
= stS
.nextToken();
43 String distributionVersion
= stS
.nextToken();
45 SortedMap
<String
, ModularDistribution
> choices
= new TreeMap
<String
, ModularDistribution
>();
46 distribs
: for (Iterator
<ModularDistribution
> it
= modularDistributions
47 .iterator(); it
.hasNext();) {
48 ModularDistribution md
= it
.next();
49 if (md
.getName().equals(distributionName
)) {
50 if (distributionVersion
.equals(BuildConstants
.RELEASE
)
51 && md
.getVersion().contains(BuildConstants
.SNAPSHOT
))
54 else if (distributionVersion
.equals(BuildConstants
.LATEST
))
55 choices
.put(md
.getVersion(), md
);
56 else if (distributionVersion
.equals(md
.getVersion())) {
57 choices
.put(md
.getVersion(), md
);
63 if (choices
.size() == 0)
64 throw new SlcException("Cannot find distribution for "
65 + new BasicNameVersion(distributionName
,
66 distributionVersion
));
68 ModularDistribution modularDistribution
= choices
.get(choices
71 request
.setAttribute("modularDistribution", modularDistribution
);
76 public void setModularDistributions(
77 Set
<ModularDistribution
> modularDistributions
) {
78 this.modularDistributions
= modularDistributions
;