001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.web;
018:
019: import info.jtrac.domain.Item;
020: import info.jtrac.domain.Space;
021: import info.jtrac.domain.User;
022: import info.jtrac.exception.InvalidRefIdException;
023: import info.jtrac.util.ItemUtils;
024: import info.jtrac.util.XmlUtils;
025: import java.io.ByteArrayOutputStream;
026: import java.io.InputStream;
027: import java.util.List;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import org.dom4j.Document;
031: import org.dom4j.Element;
032: import org.springframework.web.servlet.mvc.multiaction.MethodNameResolver;
033: import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
034:
035: /**
036: * Spring MultiActionController that handles REST requests
037: * returns XML messages
038: */
039: public class RestMultiActionController extends
040: AbstractMultiActionController {
041:
042: /**
043: * custom MethodNameResolver is configured that checks the value of an expected
044: * paramter called "method" in the request and formats the value that may be
045: * in the form of "namespace.subnamespace.action" into "namespaceSubnamespaceAction"
046: * or more like a java method name
047: */
048: public RestMultiActionController() {
049: setMethodNameResolver(new MethodNameResolver() {
050: public String getHandlerMethodName(
051: HttpServletRequest request)
052: throws NoSuchRequestHandlingMethodException {
053: String temp = request.getParameter("method");
054: if (temp == null) {
055: return null;
056: }
057: StringBuffer sb = new StringBuffer();
058: for (int i = 0; i < temp.length(); i++) {
059: char c = temp.charAt(i);
060: if (c == '.') {
061: i++;
062: c = temp.charAt(i);
063: sb.append(Character.toUpperCase(c));
064: } else {
065: sb.append(c);
066: }
067: }
068: return sb.toString();
069: }
070: });
071: }
072:
073: private void writeXml(Document document,
074: HttpServletResponse response) throws Exception {
075: applyCacheSeconds(response, 0, true);
076: response.setContentType("text/xml");
077: document.write(response.getWriter());
078: }
079:
080: private String getContent(HttpServletRequest request)
081: throws Exception {
082: InputStream is = request.getInputStream();
083: int ch;
084: ByteArrayOutputStream baos = new ByteArrayOutputStream();
085: while ((ch = is.read()) != -1) {
086: baos.write((byte) ch);
087: }
088: return new String(baos.toByteArray());
089: }
090:
091: public void versionGet(HttpServletRequest request,
092: HttpServletResponse response) throws Exception {
093: Document d = XmlUtils.getNewDocument("version");
094: Element root = d.getRootElement();
095: root.addAttribute("number", jtrac.getReleaseVersion());
096: root.addAttribute("timestamp", jtrac.getReleaseTimestamp());
097: writeXml(d, response);
098: }
099:
100: public void itemGet(HttpServletRequest request,
101: HttpServletResponse response) throws Exception {
102: String refId = request.getParameter("refId");
103: Item item = null;
104: try {
105: item = jtrac.loadItemByRefId(refId);
106: } catch (InvalidRefIdException e) {
107: // TODO
108: }
109: // TODO if item == null
110: if (item == null) {
111: return;
112: }
113: Document d = ItemUtils.getAsXml(item);
114: writeXml(d, response);
115: }
116:
117: public void itemPut(HttpServletRequest request,
118: HttpServletResponse response) throws Exception {
119: logger.debug(getContent(request));
120: Document d = XmlUtils.getNewDocument("success");
121: Element root = d.getRootElement();
122: root.addElement("refId").addText("FOOBAR-123");
123: writeXml(d, response);
124: }
125:
126: public void spaceUsersGet(HttpServletRequest request,
127: HttpServletResponse response) throws Exception {
128: String prefixCode = request.getParameter("prefixCode");
129: Space space = jtrac.loadSpace(prefixCode);
130: Document d = XmlUtils.getNewDocument("users");
131: Element root = d.getRootElement();
132: root.addAttribute("prefixCode", prefixCode);
133: List<User> users = jtrac.findUsersForSpace(space.getId());
134: for (User user : users) {
135: root.addElement("user").addAttribute("loginName",
136: user.getLoginName()).addText(user.getName());
137: }
138: writeXml(d, response);
139: }
140:
141: }
|