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

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