root/SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/www/CLServlet.java @ 273

リビジョン 260, 6.6 KB (コミッタ: atsuko, 9 年 前)

エンドポイントを指定しないと全部のクラスを出すように変更

行番号 
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.IOException;
10import java.io.PrintWriter;
11import java.math.BigDecimal;
12import java.util.*;
13import javax.json.*;
14import javax.servlet.ServletException;
15import javax.servlet.annotation.WebServlet;
16import javax.servlet.http.HttpServlet;
17import javax.servlet.http.HttpServletRequest;
18import javax.servlet.http.HttpServletResponse;
19import javax.servlet.http.HttpSession;
20import org.biohackathon.SPARQLBuilder.OWL.*;
21
22/**
23 *
24 * @author atsuko
25 */
26@WebServlet(name = "CLServlet", urlPatterns = {"/clist"})
27public class CLServlet extends HttpServlet {
28
29    private static final String FILENAME = "cdata/";
30   
31    /**
32     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
33     * methods.
34     *
35     * @param request servlet request
36     * @param response servlet response
37     * @throws ServletException if a servlet-specific error occurs
38     * @throws IOException if an I/O error occurs
39     */
40    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
41            throws ServletException, IOException {
42        response.setContentType("text/html;charset=UTF-8");
43        try (PrintWriter out = response.getWriter()) {
44            /* TODO output your page here. You may use following sample code. */
45            out.println("<!DOCTYPE html>");
46            out.println("<html>");
47            out.println("<head>");
48            out.println("<title>Servlet CLServlet</title>");           
49            out.println("</head>");
50            out.println("<body>");
51            out.println("<h1>Servlet CLServlet at " + request.getContextPath() + "</h1>");
52            out.println("</body>");
53            out.println("</html>");
54        }
55    }
56
57    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
58    /**
59     * Handles the HTTP <code>GET</code> method.
60     *
61     * @param request servlet request
62     * @param response servlet response
63     * @throws ServletException if a servlet-specific error occurs
64     * @throws IOException if an I/O error occurs
65     */
66    @Override
67    protected void doGet(HttpServletRequest request, HttpServletResponse response)
68            throws ServletException, IOException {
69        //processRequest(request, response);
70        response.setContentType("application/json;charset=UTF-8");
71        response.setHeader("Access-Control-Allow-Origin", "*");
72        response.setHeader("Access-Control-Allow-Methods", "GET");
73        response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
74        response.setHeader("Access-Control-Max-Age", "-1");     
75        PrintWriter out = response.getWriter();
76        String ep = request.getParameter("ep");
77       
78        HttpSession session = request.getSession();
79        QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg");
80        SortedSet<String> sortedClasses = null;
81               
82        if ( qpg == null ){
83            qpg = new QueryPathGenerator();
84        }
85        if ( ep == null ){
86            JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
87            JsonObjectBuilder job = jbfactory.createObjectBuilder();
88
89            String[] eplist = qpg.getFactory().getEndpointURIList();
90            for ( int i = 0; i < eplist.length; i++ ){
91                try{
92                    qpg.setSPARQLendpoint(eplist[i]);
93                    SClass[] classes = qpg.getClasses(null);
94                    sortedClasses = qpg.getSortedClasses(classes);
95                    JsonArray ja = getJsonArrayFromSortedClasses(jbfactory, sortedClasses);
96                    job.add(eplist[i], ja);                   
97                }catch(Exception e){
98                    System.err.println(e);
99                }
100            }
101            JsonObject jo = job.build();
102            out.print(jo);
103            return;
104        }
105        String classURI = request.getParameter("class");
106        qpg.setSPARQLendpoint(ep);
107        SClass[] classes = null;
108        if ( classURI != null ){
109            qpg.setOWLClassGraph(classURI);
110            classes = qpg.getReachableClasses();
111        }else{
112            classes = qpg.getClasses(null);
113        }
114        sortedClasses = qpg.getSortedClasses(classes);
115       
116        JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
117        JsonArray ja = getJsonArrayFromSortedClasses(jbfactory, sortedClasses);
118        out.print(ja);
119    }
120
121    /**
122     * Handles the HTTP <code>POST</code> method.
123     *
124     * @param request servlet request
125     * @param response servlet response
126     * @throws ServletException if a servlet-specific error occurs
127     * @throws IOException if an I/O error occurs
128     */
129    @Override
130    protected void doPost(HttpServletRequest request, HttpServletResponse response)
131            throws ServletException, IOException {
132        processRequest(request, response);
133    }
134
135    /**
136     * Returns a short description of the servlet.
137     *
138     * @return a String containing servlet description
139     */
140    @Override
141    public String getServletInfo() {
142        return "Short description";
143    }// </editor-fold>
144
145    private JsonArray getJsonArrayFromSortedClasses(JsonBuilderFactory jbfactory, SortedSet<String> sortedClasses){
146        JsonArrayBuilder jab = jbfactory.createArrayBuilder();
147        Iterator<String> cit = sortedClasses.iterator();
148        List<String> tmpclasses = new LinkedList<String>();
149        JsonObjectBuilder job = jbfactory.createObjectBuilder();
150        while( cit.hasNext() ){
151            String classinfo = cit.next();
152            String[] data = classinfo.split("  ");
153            if (data.length != 3 ){
154                System.out.println("data is wrong?");
155            }
156            if (data[0].matches("^[0-9]*$")){
157                tmpclasses.add(classinfo);
158            }else{
159                job.add("uri", data[2]);
160                job.add("label", data[0]);
161                job.add("number", data[1]);
162                jab.add(job);
163            }
164        }
165        cit = tmpclasses.iterator();
166        while( cit.hasNext() ){
167            String classinfo = cit.next();
168            String[] data = classinfo.split("  ");
169            if (data.length != 3 ){
170                System.out.println("data is wrong?");
171            }
172            job.add("uri", data[2]);
173            job.add("label", data[0]);
174            job.add("number", data[1]);
175            jab.add(job);
176        }       
177        JsonArray ja = jab.build();
178        return ja;
179    }
180}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。