1 package xuml.tools.diagram;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.apache.commons.io.IOUtils;
11
12 import xuml.tools.miuml.metamodel.jaxb.Marshaller;
13
14 public class ModelServlet extends HttpServlet {
15
16 private static final long serialVersionUID = 1L;
17
18 @Override
19 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
20 throws ServletException, IOException {
21 try {
22 String id = req.getParameter("id");
23 String result = ModelPersistence.instance().getXml(id);
24 resp.setContentType("text/plain");
25 if (result != null)
26 resp.getOutputStream().write(result.getBytes());
27 } catch (RuntimeException e) {
28 e.printStackTrace();
29 throw e;
30 }
31 }
32
33 @Override
34 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
35 throws ServletException, IOException {
36 String id = req.getParameter("id");
37 String xml = req.getParameter("xml");
38
39 try {
40 new Marshaller().unmarshal(IOUtils.toInputStream(xml));
41
42 ModelPersistence.instance().save(id, xml);
43 } catch (RuntimeException e) {
44 resp.setStatus(500);
45 resp.getWriter().println(e.getMessage());
46 }
47 }
48
49 }