root/SPARQLBuilderWWW2016/src/java/org/biohackathon/SPARQLBuilder/www/CLServlet.java @ 272

リビジョン 268, 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                    SClass[] classes = qpg.getClassesByEndpoint(eplist[i]);
93                    sortedClasses = qpg.getSortedClasses(classes);
94                    JsonArray ja = getJsonArrayFromSortedClasses(jbfactory, sortedClasses);
95                    job.add(eplist[i], ja);                   
96                }catch(Exception e){
97                    System.err.println(e);
98                }
99            }
100            JsonObject jo = job.build();
101            out.print(jo);
102            return;
103        }
104        String classURI = request.getParameter("class");
105        SClass[] classes = null;
106        if ( classURI != null ){
107            //qpg.setOWLClassGraph(classURI); // KOKO
108            //classes = qpg.getReachableClasses();
109        }else{
110            classes = qpg.getClasses(null);
111        }
112        sortedClasses = qpg.getSortedClasses(classes);
113       
114        JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
115        JsonArray ja = getJsonArrayFromSortedClasses(jbfactory, sortedClasses);
116        out.print(ja);
117    }
118
119    /**
120     * Handles the HTTP <code>POST</code> method.
121     *
122     * @param request servlet request
123     * @param response servlet response
124     * @throws ServletException if a servlet-specific error occurs
125     * @throws IOException if an I/O error occurs
126     */
127    @Override
128    protected void doPost(HttpServletRequest request, HttpServletResponse response)
129            throws ServletException, IOException {
130        processRequest(request, response);
131    }
132
133    /**
134     * Returns a short description of the servlet.
135     *
136     * @return a String containing servlet description
137     */
138    @Override
139    public String getServletInfo() {
140        return "Short description";
141    }// </editor-fold>
142
143    private JsonArray getJsonArrayFromSortedClasses(JsonBuilderFactory jbfactory, SortedSet<String> sortedClasses){
144        JsonArrayBuilder jab = jbfactory.createArrayBuilder();
145        Iterator<String> cit = sortedClasses.iterator();
146        List<String> tmpclasses = new LinkedList<String>();
147        JsonObjectBuilder job = jbfactory.createObjectBuilder();
148        while( cit.hasNext() ){
149            String classinfo = cit.next();
150            String[] data = classinfo.split("  ");
151            if (data.length != 3 ){
152                System.out.println("data is wrong?");
153            }
154            if (data[0].matches("^[0-9]*$")){
155                tmpclasses.add(classinfo);
156            }else{
157                job.add("uri", data[2]);
158                job.add("label", data[0]);
159                job.add("number", data[1]);
160                jab.add(job);
161            }
162        }
163        cit = tmpclasses.iterator();
164        while( cit.hasNext() ){
165            String classinfo = cit.next();
166            String[] data = classinfo.split("  ");
167            if (data.length != 3 ){
168                System.out.println("data is wrong?");
169            }
170            job.add("uri", data[2]);
171            job.add("label", data[0]);
172            job.add("number", data[1]);
173            jab.add(job);
174        }       
175        JsonArray ja = jab.build();
176        return ja;
177    }
178}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。