KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > AppUrl


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. 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 JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 /**
25  * This class generates Atom Publishing Protocol (APP) URls.
26  */

27 public class AppUrl {
28     private static final String JavaDoc ENDPOINT = "/app";
29     private static Pattern JavaDoc ID_PATTERN = Pattern.compile("^http://.*/(.*)/(?:entries|resources)$");
30     private static Pattern JavaDoc ENDPOINT_PATTERN = Pattern.compile("^(http://.*)/.*/(?:entries|resources)$");
31     
32     private URL JavaDoc entryUrl;
33     private URL JavaDoc resourceUrl;
34     private String JavaDoc handle;
35     
36     public AppUrl(String JavaDoc urlPrefix, String JavaDoc handle) throws MalformedURLException JavaDoc {
37         //TODO: is this the right thing to do? hardcode roller-services?
38
entryUrl = new URL JavaDoc(urlPrefix + "/roller-services" + ENDPOINT + "/" + handle + "/entries");
39         resourceUrl = new URL JavaDoc(urlPrefix + "/roller-services" + ENDPOINT + "/" + handle + "/resources");
40     }
41
42     public AppUrl(URL JavaDoc url) throws MalformedURLException JavaDoc {
43         handle = parseHandle(url);
44         URL JavaDoc endpoint = parseEndpoint(url);
45         
46         entryUrl = new URL JavaDoc(endpoint + "/" + handle + "/entries");
47         resourceUrl = new URL JavaDoc(endpoint + "/" + handle + "/resources");
48     }
49     
50     private String JavaDoc parseHandle(URL JavaDoc url) {
51         String JavaDoc urlString = url.toString();
52         String JavaDoc handle = null;
53         
54         Matcher JavaDoc m = ID_PATTERN.matcher(urlString);
55         
56         if (m.matches()) {
57             handle = m.group(1);
58         }
59         
60         return handle;
61     }
62     
63     private URL JavaDoc parseEndpoint(URL JavaDoc url) throws MalformedURLException JavaDoc {
64         String JavaDoc urlString = url.toString();
65         String JavaDoc endpointString = null;
66         
67         Matcher JavaDoc m = ENDPOINT_PATTERN.matcher(urlString);
68         
69         if (m.matches()) {
70             endpointString = m.group(1);
71         }
72         
73         URL JavaDoc endpoint = null;
74         if (endpointString != null) {
75             endpoint = new URL JavaDoc(endpointString);
76         }
77         
78         return endpoint;
79     }
80     
81     
82     public URL JavaDoc getEntryUrl() {
83         return entryUrl;
84     }
85
86     public URL JavaDoc getResourceUrl() {
87         return resourceUrl;
88     }
89     
90     public String JavaDoc getHandle() {
91         return handle;
92     }
93 }
94
Popular Tags