root/SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/www/EPServlet.java

リビジョン 285, 5.6 KB (コミッタ: atsuko, 8 年 前)

ep/ds対応

行番号 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7package org.biohackathon.SPARQLBuilder.www;
8
9import java.io.*;
10import java.math.BigDecimal;
11import javax.servlet.ServletException;
12import javax.servlet.annotation.WebServlet;
13import javax.servlet.http.HttpServlet;
14import javax.servlet.http.HttpServletRequest;
15import javax.servlet.http.HttpServletResponse;
16import javax.servlet.http.HttpSession;
17import org.biohackathon.SPARQLBuilder.OWL.*;
18import javax.json.*;
19import java.util.*;
20
21//import java.io.*;
22
23/**
24 *
25 * @author atsuko
26 */
27@WebServlet(name = "EPServlet", urlPatterns = {"/eplist"})
28public class EPServlet extends HttpServlet {
29
30    private static final String FILENAME = "cdata/";
31   
32    /**
33     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
34     * methods.
35     *
36     * @param request servlet request
37     * @param response servlet response
38     * @throws ServletException if a servlet-specific error occurs
39     * @throws IOException if an I/O error occurs
40     */
41    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
42            throws ServletException, IOException {
43        response.setContentType("text/html;charset=UTF-8");
44        try (PrintWriter out = response.getWriter()) {
45            /* TODO output your page here. You may use following sample code. */
46            out.println("<!DOCTYPE html>");
47            out.println("<html>");
48            out.println("<head>");
49            out.println("<title>Servlet EPServlet</title>");           
50            out.println("</head>");
51            out.println("<body>");
52            out.println("<h1>Servlet EPServlet at " + request.getContextPath() + "</h1>");
53            out.println("</body>");
54            out.println("</html>");
55        }
56    }
57
58    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
59    /**
60     * Handles the HTTP <code>GET</code> method.
61     *
62     * @param request servlet request
63     * @param response servlet response
64     * @throws ServletException if a servlet-specific error occurs
65     * @throws IOException if an I/O error occurs
66     */
67    @Override
68    protected void doGet(HttpServletRequest request, HttpServletResponse response)
69            throws ServletException, IOException {
70        HttpSession session = request.getSession();
71        // Initialize the session
72        session.removeAttribute("qpg");
73        //session.removeAttribute("graph");
74        QueryPathGenerator qpg = new QueryPathGenerator();
75
76        response.setContentType("application/json;charset=UTF-8");
77        response.setHeader("Access-Control-Allow-Origin", "*");
78        response.setHeader("Access-Control-Allow-Methods", "GET");
79        response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
80        response.setHeader("Access-Control-Max-Age", "-1");       
81        PrintWriter out = response.getWriter();
82     
83        String ds = request.getParameter("ds");
84   
85        String[] elist = qpg.getFactory().getEndpointURIList();
86        SortedSet<String> sortedelist = new TreeSet<String>();
87        for (int i = 0; i < elist.length; i++ ){
88            sortedelist.add(elist[i]);
89        }
90        JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
91        JsonArrayBuilder jab = jbfactory.createArrayBuilder();
92        if ( ds == null ){
93            Iterator<String> eit = sortedelist.iterator();
94            while( eit.hasNext() ){
95                jab.add(eit.next());           
96            }
97            JsonArray ja = jab.build();
98            out.print(ja);
99        }else{
100            File dsf = new File("dstable.txt");
101            HashMap<String, String> demap = new HashMap<String, String>();
102            try{
103                BufferedReader br = new BufferedReader(new FileReader(dsf));
104                String buf;
105                while ( (buf = br.readLine()) != null ){
106                    String[] data = buf.split(",");
107                    if (data.length != 2 ){ continue; }
108                    demap.put(data[0],data[1]);
109                }
110            }catch(IOException e){
111                e.printStackTrace();
112            }
113            Iterator<String> eit = sortedelist.iterator();
114            while( eit.hasNext() ){
115                JsonObjectBuilder job = jbfactory.createObjectBuilder();
116                String duri = eit.next();
117                String euri = duri;
118                String ep = demap.get(duri);
119                if ( ep != null ){
120                    euri = ep;
121                }
122                job.add("label", duri);
123                job.add("uri", euri);
124                jab.add(job);           
125            }
126            JsonArray ja = jab.build();
127            out.print(ja);           
128        }
129        session.setAttribute("qpg", qpg);
130    }
131    /**
132     * Handles the HTTP <code>POST</code> method.
133     *
134     * @param request servlet request
135     * @param response servlet response
136     * @throws ServletException if a servlet-specific error occurs
137     * @throws IOException if an I/O error occurs
138     */
139    @Override
140    protected void doPost(HttpServletRequest request, HttpServletResponse response)
141            throws ServletException, IOException {
142        processRequest(request, response);
143    }
144
145    /**
146     * Returns a short description of the servlet.
147     *
148     * @return a String containing servlet description
149     */
150    @Override
151    public String getServletInfo() {
152        return "Short description";
153    }// </editor-fold>
154
155}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。