1
2
3
4
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 import org.apache.struts.action.RedirectingActionForward;
17 import org.repoweb.model.Artifact;
18 /***
19 * Transform an URI to action call.
20 *
21 * <p>
22 * Read URI such as : repository/junit/jars/junti-3.8.1.jar.
23 * </p>
24 */
25 public class UriBeautifier extends Action {
26 public ActionForward execute(ActionMapping mapping, ActionForm objForm,
27 HttpServletRequest request, HttpServletResponse res) {
28 final String infoPath = mapping.getPath();
29 final String downloadPath = mapping.getParameter();
30 final String requestURI = request.getRequestURI();
31
32
33 final int infoIndex = requestURI.indexOf(infoPath);
34 final int downloadIndex = requestURI.indexOf(downloadPath);
35 if (infoIndex == -1 && downloadIndex == -1) {
36 ActionErrors errors = new ActionErrors();
37 errors.add(ActionErrors.GLOBAL_ERROR,
38 new ActionError("global.bad.url", requestURI));
39 saveErrors(request, errors);
40 return mapping.findForward("badURI");
41 }
42 boolean isInfoUri =
43 infoIndex != -1 && (downloadIndex == -1 || infoIndex < downloadIndex);
44
45
46 String uri;
47 if (isInfoUri) {
48 uri = requestURI.substring(infoIndex + infoPath.length());
49 }
50 else {
51 uri = requestURI.substring(downloadIndex + downloadPath.length());
52 }
53
54
55 String forward = "allGroups";
56 if (uri.length() != 0) {
57 forward = decodeGroup((DynaActionForm)objForm, uri.substring(1));
58 }
59
60
61 if (isInfoUri) {
62 return mapping.findForward(forward);
63 }
64 else {
65 return mapping.findForward("downloadAritfact");
66 }
67 }
68
69
70 private String decodeGroup(DynaActionForm form, String uri) {
71 final String group = nextToken(uri);
72 form.set(FindArtifact.SEARCH_BY_GROUP_KEY, group);
73
74 if (uri == group) {
75 return "oneGroup";
76 }
77
78 return decodeType(form, uri.substring(group.length() + 1));
79 }
80
81
82 private String decodeType(DynaActionForm form, String uri) {
83 final String type = nextToken(uri);
84
85 form.set(FindArtifact.SEARCH_BY_TYPE_KEY,
86 (type.endsWith("s") ? type.substring(0, type.length() - 1) : type));
87
88 if (uri == type) {
89 return "oneGroupByType";
90 }
91
92 return decodeFilename(form, uri.substring(type.length() + 1));
93 }
94
95
96 private String decodeFilename(DynaActionForm form, String uri) {
97 form.set(FindArtifact.SEARCH_BY_FILE_KEY, uri);
98
99 return "oneAritfact";
100 }
101
102
103 private static String nextToken(String str) {
104 final int slashIdx = str.indexOf("/");
105 if (slashIdx == -1) {
106 return str;
107 }
108 return str.substring(0, slashIdx);
109 }
110
111
112 public static ActionForward buildActionForwardTo(Artifact artifact) {
113 return new RedirectingActionForward("/do/maven/" + artifact.getGroupId() + "/"
114 + artifact.getType() + "s/" + artifact.getFileName());
115 }
116 }