10 |
import java.util.ArrayList; |
import java.util.ArrayList; |
11 |
import java.util.Date; |
import java.util.Date; |
12 |
import java.util.List; |
import java.util.List; |
13 |
|
import java.util.Map; |
14 |
import java.util.logging.Level; |
import java.util.logging.Level; |
15 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
16 |
import java.util.zip.ZipEntry; |
import java.util.zip.ZipEntry; |
21 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
22 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
23 |
|
|
24 |
|
import dk.thoerup.traininfoservice.banedk.TimeoutMap; |
25 |
|
|
26 |
public class RequestPlotter extends HttpServlet { |
public class RequestPlotter extends HttpServlet { |
27 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
28 |
|
|
30 |
|
|
31 |
static final String KML = "application/vnd.google-earth.kml"; |
static final String KML = "application/vnd.google-earth.kml"; |
32 |
static final String KMZ = "application/vnd.google-earth.kmz"; |
static final String KMZ = "application/vnd.google-earth.kmz"; |
33 |
|
|
34 |
|
Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000); |
35 |
|
|
36 |
class RequestPosition { |
class RequestPosition { |
37 |
public String ip; |
public String ip; |
126 |
*/ |
*/ |
127 |
|
|
128 |
|
|
129 |
|
final String STYLE_GREEN = "green"; |
130 |
|
final String STYLE_YELLOW = "yellow"; |
131 |
|
final String STYLE_RED = "red"; |
132 |
|
|
133 |
|
String oldstyle = null; |
|
|
|
|
|
|
134 |
Date now = new Date(); |
Date now = new Date(); |
135 |
for(RequestPosition current : list) { |
for(RequestPosition current : list) { |
136 |
|
|
138 |
long timediff = now.getTime() - current.time.getTime(); |
long timediff = now.getTime() - current.time.getTime(); |
139 |
|
|
140 |
if ( timediff < (3*60*60*1000) ) { |
if ( timediff < (3*60*60*1000) ) { |
141 |
style = "#red"; |
style = STYLE_RED; |
142 |
} else if ( timediff < (24*60*60*1000)) { |
} else if ( timediff < (24*60*60*1000)) { |
143 |
style = "#yellow"; |
style = STYLE_YELLOW; |
144 |
} else { |
} else { |
145 |
style = "#green"; |
style = STYLE_GREEN; |
146 |
|
} |
147 |
|
|
148 |
|
if ( !style.equals(oldstyle) ) { |
149 |
|
if (oldstyle != null) |
150 |
|
sb.append( "</Folder>\n"); |
151 |
|
sb.append( "<Folder>\n"); |
152 |
|
sb.append( " <name>" ).append(style).append("</name>\n"); |
153 |
|
sb.append( " <open>0</open>\n" ); |
154 |
|
|
155 |
|
oldstyle = style; |
156 |
} |
} |
157 |
|
|
158 |
sb.append( " <Placemark>\n" ); |
sb.append( " <Placemark>\n" ); |
159 |
sb.append( " <styleUrl>" + style + "</styleUrl>\n" ); |
sb.append( " <styleUrl>#" + style + "</styleUrl>\n" ); |
160 |
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" ); |
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" ); |
161 |
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" ); |
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" ); |
162 |
sb.append( " </Placemark>\n" ); |
sb.append( " </Placemark>\n" ); |
163 |
} |
} |
164 |
|
sb.append( "</Folder>\n" ); |
165 |
sb.append( "</Document>\n" ); |
sb.append( "</Document>\n" ); |
166 |
sb.append( "</kml>\n" ); |
sb.append( "</kml>\n" ); |
167 |
|
|
168 |
return sb.toString(); |
return sb.toString(); |
170 |
|
|
171 |
@Override |
@Override |
172 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
173 |
|
final String KEY = "kmldata"; |
174 |
|
|
175 |
|
String kmlData = cache.get(KEY); |
176 |
|
|
177 |
|
if (kmlData == null) { |
178 |
|
kmlData = formatXml( getRequestsFromFile() ); |
179 |
|
cache.put(KEY, kmlData); |
180 |
|
kmlData += "<!-- from source -->"; |
181 |
|
} else { |
182 |
|
kmlData += "<!-- cached -->"; |
183 |
|
} |
184 |
|
|
185 |
String data = formatXml( getRequestsFromFile() ); |
|
186 |
|
|
187 |
if (req.getParameter("zip") != null) { |
if (req.getParameter("zip") != null) { |
188 |
|
|
190 |
|
|
191 |
ZipOutputStream zip = new ZipOutputStream(baos); |
ZipOutputStream zip = new ZipOutputStream(baos); |
192 |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
193 |
zip.write( data.getBytes() ); |
zip.write( kmlData.getBytes() ); |
194 |
zip.closeEntry(); |
zip.closeEntry(); |
195 |
zip.close(); |
zip.close(); |
196 |
|
|
202 |
|
|
203 |
} else { |
} else { |
204 |
resp.setContentType(KML); |
resp.setContentType(KML); |
205 |
resp.getWriter().print( data ); |
resp.getWriter().print( kmlData ); |
206 |
} |
} |
207 |
} |
} |
208 |
} |
} |