/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/SearchServlet.java
ViewVC logotype

Annotation of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/SearchServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2540 - (hide annotations) (download)
Mon May 11 08:13:04 2015 UTC (9 years ago) by torben
File size: 5600 byte(s)
FindBugs
1 torben 2274 package dk.daoas.daoadresseservice;
2    
3     import java.io.IOException;
4    
5     import javax.servlet.ServletException;
6     import javax.servlet.annotation.WebServlet;
7     import javax.servlet.http.HttpServlet;
8     import javax.servlet.http.HttpServletRequest;
9     import javax.servlet.http.HttpServletResponse;
10    
11 torben 2506 import org.apache.commons.lang3.StringUtils;
12    
13 torben 2290 import dk.daoas.daoadresseservice.beans.Address;
14 torben 2282 import dk.daoas.daoadresseservice.beans.SearchResult;
15     import dk.daoas.daoadresseservice.beans.SearchResult.Status;
16 torben 2372 import dk.daoas.daoadresseservice.db.LogPersistance;
17 torben 2274
18     /**
19 torben 2311 * Denne servlet forventer at Tomcat connector'en er konfigureret med
20     * URIEncoding="UTF-8"
21 torben 2274 */
22 torben 2277 @WebServlet(urlPatterns = { "/SearchServlet", "/search" }, loadOnStartup = 1)
23 torben 2274 public class SearchServlet extends HttpServlet {
24 torben 2278
25 torben 2372 private static final long serialVersionUID = 1L;
26 torben 2346
27 torben 2540
28     private transient LogPersistance logPersistance;
29     //transient ServiceConfig config;
30    
31 torben 2278 @Override
32     public void init() throws ServletException {
33     super.init();
34 torben 2346
35 torben 2540 //config = (ServiceConfig) this.getServletContext().getAttribute("config");
36 torben 2372 logPersistance = new LogPersistance();
37 torben 2346
38 torben 2278 System.out.println("SearchServlet loaded");
39     }
40 torben 2274
41 torben 2375 @Override
42     public void destroy() {
43     super.destroy();
44    
45     if (logPersistance != null) {
46     logPersistance.shutdown();
47     }
48     System.out.println("SearchServlet destroyed");
49     }
50    
51    
52    
53    
54    
55    
56 torben 2274 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57 torben 2299
58 torben 2410 AddressSearch search = (AddressSearch) this.getServletContext().getAttribute("search");
59 torben 2299 if (search == null) {
60     response.setContentType("text/xml; charset=UTF-8");
61 torben 2374 response.getWriter().print( generateErrorXML("Service not ready") );
62     return;
63 torben 2299 }
64 torben 2274
65     String brugerid = request.getParameter("brugerid");
66     String postnr = request.getParameter("postnr");
67 torben 2287 String adresseStr = request.getParameter("adresse");
68 torben 2374
69     if (postnr == null) {
70     response.setContentType("text/xml; charset=UTF-8");
71     response.getWriter().print( generateErrorXML("Mangler postnr parameter") );
72     return;
73     }
74    
75     if (adresseStr == null) {
76     response.setContentType("text/xml; charset=UTF-8");
77     response.getWriter().print( generateErrorXML("Mangler adresse parameter") );
78     return;
79     }
80     if (brugerid == null) {
81     brugerid = request.getRemoteHost();
82     }
83 torben 2274
84 torben 2350 SearchResult result = search.search(postnr, adresseStr);
85 torben 2274
86 torben 2288
87 torben 2274 StringBuilder sb = new StringBuilder();
88 torben 2294 sb.append("<result>\n");
89 torben 2274
90     if (result.status == Status.STATUS_OK) {
91     sb.append("<status>ok</status>");
92     } else {
93     sb.append("<status>error</status>");
94     }
95 torben 2506
96     String rute = StringUtils.defaultString(result.address.rute); //Sikre at der ikke sendes "null" tilbage
97     String koreliste = StringUtils.defaultString(result.address.koreliste); //Sikre at der ikke sendes "null" tilbage
98 torben 2397
99     sb.append("<error>").append( result.status ).append("</error>");
100 torben 2506 sb.append("<distrikt>").append( rute ).append("</distrikt>");
101     sb.append("<koreliste>").append( koreliste ).append("</koreliste>");
102 torben 2397 sb.append("<kommunekode>").append(result.address.kommunekode).append("</kommunekode>");
103     sb.append("<vejkode>").append(result.address.vejkode).append("</vejkode>");
104     sb.append("<dbkbane>").append(result.address.dbkBane).append("</dbkbane>");
105     sb.append("\n");
106    
107     //Extra information
108 torben 2444 sb.append("<split>");
109     sb.append("<vej>").append( result.splitResult.vej).append("</vej>");
110     sb.append("<husnr>").append( result.splitResult.husnr).append("</husnr>");
111     sb.append("<litra>").append( result.splitResult.litra).append("</litra>");
112     sb.append("<etage>").append( result.splitResult.etage).append("</etage>"); //sal
113     sb.append("<lejlighed>").append( result.splitResult.lejlighed).append("</lejlighed>"); //side
114     sb.append("</split>");
115    
116    
117 torben 2397 sb.append("<gadeident>").append( result.gadeident ).append("</gadeident>");
118 torben 2405 sb.append("<vejnavn>").append(result.vej).append("</vejnavn>");
119 torben 2442
120     sb.append("<latitude>").append(result.address.latitude).append("</latitude>");
121     sb.append("<longitude>").append(result.address.longitude).append("</longitude>");
122    
123 torben 2397 sb.append("<daekning>").append(result.address.daekningsType).append("</daekning>");
124     sb.append("<distributor>").append(result.address.distributor).append("</distributor>");
125     if (result.address.daekningsType == DaekningsType.DAEKNING_UDVIDET) {
126     sb.append("<extafstand>").append(result.address.extAfstand).append("</extafstand>");
127    
128     Address t = result.address.extTarget;
129     sb.append("<extaddr>").append(t.vejnavn + " " + t.husnr + t.husnrbogstav + ", " + t.postnr).append("</extaddr>");
130 torben 2444 }
131    
132 torben 2397
133     sb.append("<levenshtein>").append(result.levenshtein).append("</levenshtein>");
134    
135     sb.append("<osm>").append(result.osm).append("</osm>");
136     sb.append("<osmvej>").append(result.osmVej).append("</osmvej>");
137    
138     sb.append("<google>").append(result.google).append("</google>");
139     sb.append("<googlevej>").append(result.googleVej).append("</googlevej>");
140    
141    
142 torben 2274 sb.append("</result>");
143    
144     response.setContentType("text/xml; charset=UTF-8");
145     response.getWriter().print( sb );
146    
147 torben 2372 logPersistance.saveRequestLog(brugerid, postnr, adresseStr, result);
148    
149 torben 2274 }
150 torben 2374
151     private String generateErrorXML(String msg) {
152     StringBuilder sb = new StringBuilder();
153     sb.append("<result>");
154     sb.append("<status>error</status>");
155     sb.append("<error>").append(msg).append("</error>");
156     sb.append("</result>");
157    
158     return sb.toString();
159    
160     }
161 torben 2274
162     }

  ViewVC Help
Powered by ViewVC 1.1.20