Source code: org/repoweb/action/UriBeautifier.java
1 /*
2 * REPOWEB, repository manager.
3 *
4 * Terms of license - http://opensource.org/licenses/apachepl.php
5 */
6 package org.repoweb.action;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 import org.apache.struts.action.Action;
10 import org.apache.struts.action.ActionError;
11 import org.apache.struts.action.ActionErrors;
12 import org.apache.struts.action.ActionForm;
13 import org.apache.struts.action.ActionForward;
14 import org.apache.struts.action.ActionMapping;
15 import org.apache.struts.action.DynaActionForm;
16 /**
17 * Transform an URI to action call.
18 *
19 * <p>
20 * Read URI such as : repository/junit/jars/junti-3.8.1.jar.
21 * </p>
22 */
23 public class UriBeautifier extends Action {
24 public ActionForward execute(ActionMapping mapping, ActionForm objForm,
25 HttpServletRequest request, HttpServletResponse res) {
26 final String rootPath = mapping.getPath();
27 final String requestURI = request.getRequestURI();
28 DynaActionForm form = (DynaActionForm)objForm;
29
30 // Manage Bad URI
31 if (requestURI.indexOf(rootPath) == -1) {
32 ActionErrors errors = new ActionErrors();
33 errors.add(ActionErrors.GLOBAL_ERROR,
34 new ActionError("global.bad.url", requestURI));
35 saveErrors(request, errors);
36 return mapping.findForward("badURI");
37 }
38
39 // Manage normal case !
40 final String uri =
41 requestURI.substring(requestURI.indexOf(rootPath) + rootPath.length());
42
43 String forward = "allGroups";
44 if (uri.length() != 0) {
45 forward = decodeGroup(form, uri.substring(1));
46 }
47
48 return mapping.findForward(forward);
49 }
50
51
52 private String decodeGroup(DynaActionForm form, String uri) {
53 final int slashIdx = uri.indexOf("/");
54
55 final String group = uri.substring(0, (slashIdx != -1) ? slashIdx : uri.length());
56 form.set(FindArtifact.SEARCH_BY_GROUP_KEY, group);
57
58 if (slashIdx == -1) {
59 return "oneGroup";
60 }
61
62 return decodeType(form, uri.substring(slashIdx + 1));
63 }
64
65
66 private String decodeType(DynaActionForm form, String uri) {
67 final int slashIdx = uri.indexOf("/");
68
69 final String type =
70 uri.substring(0, ((slashIdx != -1) ? slashIdx : uri.length()));
71 form.set(FindArtifact.SEARCH_BY_TYPE_KEY,
72 (type.endsWith("s") ? type.substring(0, type.length() - 1) : type));
73
74 if (slashIdx == -1) {
75 return "oneGroupByType";
76 }
77
78 return decodeFilename(form, uri.substring(slashIdx + 1));
79 }
80
81
82 private String decodeFilename(DynaActionForm form, String uri) {
83 form.set(FindArtifact.SEARCH_BY_FILE_KEY, uri);
84
85 return "oneAritfact";
86 }
87 }