Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,241 additions
and
1,156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...onitor/core/src/main/java/org/geoserver/monitor/rest/AbstractMonitorRequestConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.monitor.rest; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.geoserver.monitor.Monitor; | ||
import org.geoserver.monitor.Query; | ||
import org.geoserver.monitor.RequestData; | ||
import org.geoserver.monitor.RequestDataVisitor; | ||
import org.geoserver.rest.converters.BaseMessageConverter; | ||
import org.springframework.http.HttpInputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
|
||
/** | ||
* Base class for monitor requests converters | ||
*/ | ||
public abstract class AbstractMonitorRequestConverter extends BaseMessageConverter { | ||
|
||
@Override | ||
public boolean canRead(Class clazz, MediaType mediaType) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean canWrite(Class clazz, MediaType mediaType) { | ||
return MonitorQueryResults.class.isAssignableFrom(clazz) | ||
&& getSupportedMediaTypes().contains(mediaType); | ||
} | ||
|
||
@Override | ||
public Object read(Class clazz, HttpInputMessage inputMessage) | ||
throws IOException, HttpMessageNotReadableException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
static void handleRequests(Object object, RequestDataVisitor visitor, Monitor monitor) { | ||
if (object instanceof Query) { | ||
monitor.query((Query) object, visitor); | ||
} else { | ||
List<RequestData> requests; | ||
if (object instanceof List) { | ||
requests = (List) object; | ||
} else { | ||
requests = Collections.singletonList((RequestData) object); | ||
} | ||
for (RequestData data : requests) { | ||
visitor.visit(data, null); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
101 changes: 101 additions & 0 deletions
101
src/extension/monitor/core/src/main/java/org/geoserver/monitor/rest/CSVMonitorConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.monitor.rest; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.geoserver.monitor.Monitor; | ||
import org.geoserver.monitor.RequestData; | ||
import org.geoserver.monitor.RequestDataVisitor; | ||
import org.geoserver.ows.util.OwsUtils; | ||
import org.geotools.feature.type.DateUtil; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CSVMonitorConverter extends AbstractMonitorRequestConverter { | ||
|
||
static Pattern ESCAPE_REQUIRED = Pattern.compile("[\\,\\s\"]"); | ||
|
||
@Override | ||
public List getSupportedMediaTypes() { | ||
return Arrays.asList(MonitorRequestController.CSV_MEDIATYPE); | ||
} | ||
|
||
@Override | ||
public void write(Object t, MediaType contentType, HttpOutputMessage outputMessage) | ||
throws IOException, HttpMessageNotWritableException { | ||
MonitorQueryResults results = (MonitorQueryResults) t; | ||
Object result = results.getResult(); | ||
String[] fields = results.getFields(); | ||
Monitor monitor = results.getMonitor(); | ||
|
||
OutputStream os = outputMessage.getBody(); | ||
write(result, fields, monitor, os); | ||
} | ||
|
||
void write(Object result, String[] fields, Monitor monitor, OutputStream os) | ||
throws IOException { | ||
final BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os)); | ||
|
||
// write out the header | ||
StringBuffer sb = new StringBuffer(); | ||
for (String fld : fields) { | ||
sb.append(fld).append(","); | ||
} | ||
sb.setLength(sb.length() - 1); | ||
w.write(sb.append("\n").toString()); | ||
|
||
handleRequests(result, new RequestDataVisitor() { | ||
public void visit(RequestData data, Object... aggregates) { | ||
try { | ||
writeRequest(data, w, fields); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}, monitor); | ||
|
||
w.flush(); | ||
} | ||
|
||
void writeRequest(RequestData data, BufferedWriter w, String[] fields) throws IOException { | ||
StringBuffer sb = new StringBuffer(); | ||
|
||
for (String fld : fields) { | ||
Object val = OwsUtils.get(data, fld); | ||
if (val instanceof Date) { | ||
val = DateUtil.serializeDateTime((Date) val); | ||
} | ||
if (val != null) { | ||
String string = val.toString(); | ||
Matcher match = ESCAPE_REQUIRED.matcher(string); | ||
if (match.find()) { // may need escaping, so escape | ||
string = string.replaceAll("\"", "\"\"");// Double all double quotes to escape | ||
sb.append("\""); | ||
sb.append(string); | ||
sb.append("\""); | ||
} else { // No need for escaping | ||
sb.append(string); | ||
} | ||
} | ||
sb.append(","); | ||
} | ||
sb.setLength(sb.length() - 1); // Remove trailing comma | ||
sb.append("\n"); | ||
w.write(sb.toString()); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...xtension/monitor/core/src/main/java/org/geoserver/monitor/rest/ExcelMonitorConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.monitor.rest; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.apache.poi.hssf.usermodel.HSSFCell; | ||
import org.apache.poi.hssf.usermodel.HSSFRichTextString; | ||
import org.apache.poi.hssf.usermodel.HSSFRow; | ||
import org.apache.poi.hssf.usermodel.HSSFSheet; | ||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
import org.geoserver.monitor.Monitor; | ||
import org.geoserver.monitor.RequestData; | ||
import org.geoserver.monitor.RequestDataVisitor; | ||
import org.geoserver.ows.util.OwsUtils; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ExcelMonitorConverter extends AbstractMonitorRequestConverter { | ||
|
||
@Override | ||
public List getSupportedMediaTypes() { | ||
return Arrays.asList(MonitorRequestController.EXCEL_MEDIATYPE); | ||
} | ||
|
||
@Override | ||
public void write(Object t, MediaType contentType, HttpOutputMessage outputMessage) | ||
throws IOException, HttpMessageNotWritableException { | ||
MonitorQueryResults results = (MonitorQueryResults) t; | ||
Object object = results.getResult(); | ||
Monitor monitor = results.getMonitor(); | ||
|
||
// Create the workbook+sheet | ||
HSSFWorkbook wb = new HSSFWorkbook(); | ||
final HSSFSheet sheet = wb.createSheet("requests"); | ||
|
||
// create the header | ||
HSSFRow header = sheet.createRow(0); | ||
String[] fields = results.getFields(); | ||
for (int i = 0; i < fields.length; i++) { | ||
HSSFCell cell = header.createCell(i); | ||
cell.setCellValue(new HSSFRichTextString(fields[i])); | ||
} | ||
|
||
// write out the request | ||
handleRequests(object, new RequestDataVisitor() { | ||
int i = 1; | ||
|
||
public void visit(RequestData data, Object... aggregates) { | ||
HSSFRow row = sheet.createRow(i++); | ||
for (int j = 0; j < fields.length; j++) { | ||
HSSFCell cell = row.createCell(j); | ||
Object obj = OwsUtils.get(data, fields[j]); | ||
if (obj == null) { | ||
continue; | ||
} | ||
|
||
if (obj instanceof Date) { | ||
cell.setCellValue((Date) obj); | ||
} else if (obj instanceof Number) { | ||
cell.setCellValue(((Number) obj).doubleValue()); | ||
} else { | ||
cell.setCellValue(new HSSFRichTextString(obj.toString())); | ||
} | ||
} | ||
} | ||
}, monitor); | ||
|
||
// write to output | ||
wb.write(outputMessage.getBody()); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...nsion/monitor/core/src/main/java/org/geoserver/monitor/rest/MonitorMediaTypeCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.monitor.rest; | ||
|
||
import org.geoserver.rest.MediaTypeCallback; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; | ||
|
||
@Component | ||
public class MonitorMediaTypeCallback implements MediaTypeCallback { | ||
|
||
@Override | ||
public void configure(ContentNegotiationConfigurer configurer) { | ||
configurer.mediaType("csv", MonitorRequestController.CSV_MEDIATYPE); | ||
configurer.mediaType("zip", MonitorRequestController.ZIP_MEDIATYPE); | ||
configurer.mediaType("xls", MonitorRequestController.EXCEL_MEDIATYPE); | ||
} | ||
|
||
} |
Oops, something went wrong.