01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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. For additional information regarding
15: * copyright in this work, please see the NOTICE file in the top level
16: * directory of this distribution.
17: */
18: package org.apache.roller.webservices.adminapi;
19:
20: import java.net.MalformedURLException;
21: import java.net.URL;
22: import java.util.regex.Pattern;
23: import java.util.regex.Matcher;
24:
25: /**
26: * This class generates Atom Publishing Protocol (APP) URls.
27: */
28: public class AppUrl {
29: private static final String ENDPOINT = "/app";
30: private static Pattern ID_PATTERN = Pattern
31: .compile("^http://.*/(.*)/(?:entries|resources)$");
32: private static Pattern ENDPOINT_PATTERN = Pattern
33: .compile("^(http://.*)/.*/(?:entries|resources)$");
34:
35: private URL entryUrl;
36: private URL resourceUrl;
37: private String handle;
38:
39: public AppUrl(String urlPrefix, String handle)
40: throws MalformedURLException {
41: //TODO: is this the right thing to do? hardcode roller-services?
42: entryUrl = new URL(urlPrefix + "/roller-services" + ENDPOINT
43: + "/" + handle + "/entries");
44: resourceUrl = new URL(urlPrefix + "/roller-services" + ENDPOINT
45: + "/" + handle + "/resources");
46: }
47:
48: public AppUrl(URL url) throws MalformedURLException {
49: handle = parseHandle(url);
50: URL endpoint = parseEndpoint(url);
51:
52: entryUrl = new URL(endpoint + "/" + handle + "/entries");
53: resourceUrl = new URL(endpoint + "/" + handle + "/resources");
54: }
55:
56: private String parseHandle(URL url) {
57: String urlString = url.toString();
58: String handle = null;
59:
60: Matcher m = ID_PATTERN.matcher(urlString);
61:
62: if (m.matches()) {
63: handle = m.group(1);
64: }
65:
66: return handle;
67: }
68:
69: private URL parseEndpoint(URL url) throws MalformedURLException {
70: String urlString = url.toString();
71: String endpointString = null;
72:
73: Matcher m = ENDPOINT_PATTERN.matcher(urlString);
74:
75: if (m.matches()) {
76: endpointString = m.group(1);
77: }
78:
79: URL endpoint = null;
80: if (endpointString != null) {
81: endpoint = new URL(endpointString);
82: }
83:
84: return endpoint;
85: }
86:
87: public URL getEntryUrl() {
88: return entryUrl;
89: }
90:
91: public URL getResourceUrl() {
92: return resourceUrl;
93: }
94:
95: public String getHandle() {
96: return handle;
97: }
98: }
|