1 18 package org.apache.roller.webservices.adminapi; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import javax.servlet.http.HttpServletRequest ; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.roller.config.RollerConfig; 26 import org.apache.roller.model.Roller; 27 import org.apache.roller.model.RollerFactory; 28 import org.apache.roller.ui.core.RollerContext; 29 import org.apache.roller.webservices.adminapi.sdk.EntrySet; 30 31 import java.util.regex.Pattern ; 32 import java.util.regex.Matcher ; 33 import org.apache.roller.config.RollerRuntimeConfig; 34 import org.apache.roller.webservices.adminapi.sdk.MissingElementException; 35 import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException; 36 import org.jdom.Document; 37 import org.jdom.JDOMException; 38 import org.jdom.input.SAXBuilder; 39 40 45 abstract class Handler { 46 protected static final String ENDPOINT = "/aapp"; 47 48 static class URI { 49 private static Pattern PATHINFO_PATTERN = Pattern.compile("^/(users|weblogs|members)(?:/(.*))?$"); 50 51 private String type; 52 private String entryId; 53 54 public URI(HttpServletRequest request) throws BadRequestException { 55 String pi = request.getPathInfo(); 56 57 if (pi == null || pi.length() == 0) { 58 type = null; 59 entryId = null; 60 } else { 61 Matcher m = PATHINFO_PATTERN.matcher(pi); 62 if (!m.matches()) { 63 throw new BadRequestException("ERROR: Invalid path info: " + pi); 64 } 65 66 type = m.group(1); 67 entryId = m.group(2); 68 } 69 } 70 71 public String getType() { 72 return type; 73 } 74 75 public String getEntryId() { 76 return entryId; 77 } 78 79 public boolean isIntrospection() { 80 return getEntryId() == null && type == null; 81 } 82 83 public boolean isCollection() { 84 return getEntryId() == null && type != null; 85 } 86 87 public boolean isEntry() { 88 return getEntryId() != null && type != null; 89 } 90 } 91 92 protected static final Log logger = LogFactory.getFactory().getInstance(Handler.class); 93 94 private HttpServletRequest request; 95 private Roller roller; 96 private RollerContext rollerContext; 97 private String userName; 98 private URI uri; 99 private String urlPrefix; 100 101 102 public static Handler getHandler(HttpServletRequest req) throws HandlerException { 103 boolean enabled = RollerConfig.getBooleanProperty("webservices.adminprotocol.enabled"); 104 if (!enabled) { 105 throw new NotAllowedException("ERROR: Admin protocol not enabled"); 106 } 107 108 URI uri = new URI(req); 109 Handler handler; 110 111 if (uri.isIntrospection()) { 112 handler = new IntrospectionHandler(req); 113 } else if (uri.isCollection() || uri.isEntry()) { 114 String type = uri.getType(); 115 if (type.equals(EntrySet.Types.WEBLOGS)) { 116 handler = new RollerWeblogHandler(req); 117 } else if (type.equals(EntrySet.Types.USERS)) { 118 handler = new RollerUserHandler(req); 119 } else if (type.equals(EntrySet.Types.MEMBERS)) { 120 handler = new RollerMemberHandler(req); 121 } else { 122 throw new BadRequestException("ERROR: Unknown type: " + uri.getType()); 123 } 124 } else { 125 throw new BadRequestException("ERROR: Unknown URI type"); 126 } 127 128 return handler; 129 } 130 131 public Handler(HttpServletRequest request) throws HandlerException { 132 this.request = request; 133 this.uri = new URI(request); 134 this.rollerContext = RollerContext.getRollerContext(); 135 this.roller = RollerFactory.getRoller(); 136 this.urlPrefix = RollerRuntimeConfig.getAbsoluteContextURL() + "/roller-services" + ENDPOINT; 138 139 Authenticator auth = new BasicAuthenticator(request); 142 auth.authenticate(); 143 setUserName(auth.getUserName()); 144 } 145 146 150 public String getUserName() { 151 return userName; 152 } 153 154 private void setUserName(String userName) { 155 this.userName = userName; 156 } 157 158 159 public abstract EntrySet processGet() throws HandlerException; 160 161 public abstract EntrySet processPost(Reader r) throws HandlerException; 162 163 public abstract EntrySet processPut(Reader r) throws HandlerException; 164 165 public abstract EntrySet processDelete() throws HandlerException; 166 167 protected URI getUri() { 168 return uri; 169 } 170 171 protected HttpServletRequest getRequest() { 172 return request; 173 } 174 175 protected RollerContext getRollerContext() { 176 return rollerContext; 177 } 178 179 protected Roller getRoller() { 180 return roller; 181 } 182 183 protected String getUrlPrefix() { 184 return urlPrefix; 185 } 186 187 protected abstract EntrySet getEntrySet(Document d) throws MissingElementException, UnexpectedRootElementException; 188 189 protected EntrySet getEntrySet(Reader r) throws HandlerException { 190 try { 191 SAXBuilder builder = new SAXBuilder(); 192 Document d = builder.build(r); 193 EntrySet c = getEntrySet(d); 194 195 return c; 196 } catch (UnexpectedRootElementException ure) { 197 throw new BadRequestException("ERROR: Failed to parse content", ure); 198 } catch (MissingElementException mee) { 199 throw new BadRequestException("ERROR: Failed to parse content", mee); 200 } catch (JDOMException jde) { 201 throw new BadRequestException("ERROR: Failed to parse content", jde); 202 } catch (IOException ioe) { 203 throw new InternalException("ERROR: Failed to parse content", ioe); 204 } 205 } 206 } 207 208 | Popular Tags |