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

リビジョン 206, 5.7 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.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        HttpSession session = request.getSession();
78        QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg");
79        if ( qpg == null ){
80                   qpg = new QueryPathGenerator();
81        }
82        SortedSet<String> sortedClasses = new TreeSet<String>(); // display + url
83        qpg.setSPARQLendpoint(ep);
84        SClass[] classes = qpg.getClasses(null);
85        for (int i = 0 ; i < classes.length; i++ ){
86            String uri = classes[i].getClassURI();
87            Label[] labels = classes[i].getLabels();
88            String label = null;
89            for ( int j = 0 ; j < labels.length; j++ ){
90                if ( labels[j].getLanguage() == null ){
91                    label = labels[j].getLabel(); break;
92                }else if ( labels[j].getLanguage().equals("en") ){
93                    label = labels[j].getLabel(); break;
94                }
95            }
96            if ( label == null ){
97                String[] uris = uri.split("/");
98                String tmplabel = uris[uris.length-1];
99                String[] tmplabel2 = tmplabel.split("#");
100                label = tmplabel2[tmplabel2.length-1];
101            }
102            StringBuilder classbuilder = new StringBuilder(label);
103            classbuilder.append(" (");
104            classbuilder.append(classes[i].getNumOfInstances());
105            classbuilder.append(")  ");
106            classbuilder.append(uri);
107            sortedClasses.add(classbuilder.toString());
108        }
109        JsonBuilderFactory jbfactory = Json.createBuilderFactory(null);
110        JsonArrayBuilder jab = jbfactory.createArrayBuilder();
111        Iterator<String> cit = sortedClasses.iterator();
112        while( cit.hasNext() ){
113            JsonObjectBuilder job = jbfactory.createObjectBuilder();
114            String classinfo = cit.next();
115            String[] data = classinfo.split("  ");
116            if (data.length != 2 ){
117                System.out.println("data is wrong?");
118            }
119            job.add("uri", data[1]);
120            job.add("display", data[0]);
121            jab.add(job);
122        }
123        JsonArray ja = jab.build();
124        out.print(ja);
125        //
126       
127    }
128
129    /**
130     * Handles the HTTP <code>POST</code> method.
131     *
132     * @param request servlet request
133     * @param response servlet response
134     * @throws ServletException if a servlet-specific error occurs
135     * @throws IOException if an I/O error occurs
136     */
137    @Override
138    protected void doPost(HttpServletRequest request, HttpServletResponse response)
139            throws ServletException, IOException {
140        processRequest(request, response);
141    }
142
143    /**
144     * Returns a short description of the servlet.
145     *
146     * @return a String containing servlet description
147     */
148    @Override
149    public String getServletInfo() {
150        return "Short description";
151    }// </editor-fold>
152
153}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。