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

リビジョン 228, 5.9 KB (コミッタ: atsuko, 10 年 前)

データ増加に伴い効率性を多少向上

行番号 
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.util.*;
12import javax.json.*;
13import javax.servlet.ServletException;
14import javax.servlet.annotation.WebServlet;
15import javax.servlet.http.HttpServlet;
16import javax.servlet.http.HttpServletRequest;
17import javax.servlet.http.HttpServletResponse;
18import javax.servlet.http.HttpSession;
19import org.biohackathon.SPARQLBuilder.OWL.*;
20
21/**
22 *
23 * @author atsuko
24 */
25@WebServlet(name = "CLServlet", urlPatterns = {"/clist"})
26public class CLServlet extends HttpServlet {
27
28    private static final String FILENAME = "cdata/";
29   
30    /**
31     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
32     * methods.
33     *
34     * @param request servlet request
35     * @param response servlet response
36     * @throws ServletException if a servlet-specific error occurs
37     * @throws IOException if an I/O error occurs
38     */
39    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
40            throws ServletException, IOException {
41        response.setContentType("text/html;charset=UTF-8");
42        try (PrintWriter out = response.getWriter()) {
43            /* TODO output your page here. You may use following sample code. */
44            out.println("<!DOCTYPE html>");
45            out.println("<html>");
46            out.println("<head>");
47            out.println("<title>Servlet CLServlet</title>");           
48            out.println("</head>");
49            out.println("<body>");
50            out.println("<h1>Servlet CLServlet at " + request.getContextPath() + "</h1>");
51            out.println("</body>");
52            out.println("</html>");
53        }
54    }
55
56    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
57    /**
58     * Handles the HTTP <code>GET</code> method.
59     *
60     * @param request servlet request
61     * @param response servlet response
62     * @throws ServletException if a servlet-specific error occurs
63     * @throws IOException if an I/O error occurs
64     */
65    @Override
66    protected void doGet(HttpServletRequest request, HttpServletResponse response)
67            throws ServletException, IOException {
68        //processRequest(request, response);
69        response.setContentType("application/json;charset=UTF-8");
70        response.setHeader("Access-Control-Allow-Origin", "*");
71        response.setHeader("Access-Control-Allow-Methods", "GET");
72        response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
73        response.setHeader("Access-Control-Max-Age", "-1");     
74        PrintWriter out = response.getWriter();
75        String ep = request.getParameter("ep");
76        HttpSession session = request.getSession();
77        QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg");
78        if ( qpg == null ){
79            qpg = new QueryPathGenerator();
80        }
81        SortedSet<String> sortedClasses = new TreeSet<String>(); // display + url
82        qpg.setSPARQLendpoint(ep);
83        //OWLClassGraph ocg = qpg.getOWLClassGraph();
84        SClass[] classes = qpg.getClasses(null);
85        for (int i = 0 ; i < classes.length; i++ ){
86            String uri = classes[i].getClassURI();
87            //if ( ocg.getNumberOfEdge(uri) == 0 ){
88            //    continue;
89            //}
90            Label[] labels = classes[i].getLabels();
91            String label = null;
92            for ( int j = 0 ; j < labels.length; j++ ){
93                if ( labels[j].getLanguage() == null ){
94                    label = labels[j].getLabel(); break;
95                }else if ( labels[j].getLanguage().equals("en") ){
96                    label = labels[j].getLabel(); break;
97                }
98            }
99            if ( label == null ){
100                String[] uris = uri.split("/");
101                String tmplabel = uris[uris.length-1];
102                String[] tmplabel2 = tmplabel.split("#");
103                label = tmplabel2[tmplabel2.length-1];
104            }
105            StringBuilder classbuilder = new StringBuilder(label);
106            classbuilder.append("  ");
107            classbuilder.append(classes[i].getNumOfInstances());
108            classbuilder.append("  ");
109            classbuilder.append(uri);
110            sortedClasses.add(classbuilder.toString());
111        }
112        JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
113        JsonArrayBuilder jab = jbfactory.createArrayBuilder();
114        Iterator<String> cit = sortedClasses.iterator();
115        while( cit.hasNext() ){
116            JsonObjectBuilder job = jbfactory.createObjectBuilder();
117            String classinfo = cit.next();
118            String[] data = classinfo.split("  ");
119            if (data.length != 3 ){
120                System.out.println("data is wrong?");
121            }
122            job.add("uri", data[2]);
123            job.add("label", data[0]);
124            job.add("number", data[1]);
125            jab.add(job);
126        }
127        JsonArray ja = jab.build();
128        out.print(ja);
129    }
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 を参照してください。