]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/provisioning/ModularDistributionInterceptor.java
Update headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / provisioning / ModularDistributionInterceptor.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.web.mvc.provisioning;
17
18 import java.util.Iterator;
19 import java.util.Set;
20 import java.util.SortedMap;
21 import java.util.StringTokenizer;
22 import java.util.TreeMap;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
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;
32
33 public class ModularDistributionInterceptor extends HandlerInterceptorAdapter {
34 private Set<ModularDistribution> modularDistributions;
35
36 @Override
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();
43
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))
51 continue distribs;
52
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);
57 break distribs;
58 }
59 }
60 }
61
62 if (choices.size() == 0)
63 throw new SlcException("Cannot find distribution for "
64 + new BasicNameVersion(distributionName,
65 distributionVersion));
66
67 ModularDistribution modularDistribution = choices.get(choices
68 .firstKey());
69
70 request.setAttribute("modularDistribution", modularDistribution);
71
72 return true;
73 }
74
75 public void setModularDistributions(
76 Set<ModularDistribution> modularDistributions) {
77 this.modularDistributions = modularDistributions;
78 }
79
80 }