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