In this demo, we will demonstrate how to use the INTGeo API to create a web service for INTGeoServer. This walkthrough assumes that you have configured your IDE.
The web service that this we will create will display the file size of a file located on the application server.
The FileSizeRequestHandler class defines the simple web service request for this demo. The method canHandleRequest() is used to determine if the FileSizeRequestHandler matches the requested URL.
The method handleRequest() is responsible for handling the JSON web service request.
FILESIZEREQUESTHANDLER
package com.interactive.intgeo.demoprojects.simplejsonservice;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.interactive.intgeoapi.server.errorhandlers.AbstractIntGeoServerErrorHandler;
import com.interactive.intgeoapi.server.requesthandlers.AbstractJSONServiceRequestHandler;
import com.interactive.intgeoapi.server.requesthandlers.AbstractServiceRequestHandler;
import com.interactive.intgeo.server.util.JSONUtil;
import com.interactive.intgeoapi.server.IntGeoServiceContextGlobal;
import java.io.File;
public class FileSizeRequestHandler extends AbstractJSONServiceRequestHandler {
private static final String FILE_SIZE_PATH = "/filesize";
@Override
public boolean canHandleRequest() {
String pathInfo = IntGeoServiceContextGlobal.get().getRequestPathInfo();
return pathInfo != null && pathInfo.equals(FILE_SIZE_PATH);
}
@Override
public JsonNode handleJsonObjectRequest(JsonNode jsonObject) throws Exception {
// verify that a path is specified
String filePath = JSONUtil.getStringFromJSON(jsonObject, "file");
if (filePath == null) {
AbstractIntGeoServerErrorHandler.Factory.getInstance().handleMissingRequestParameter(this, "file");
return null;
}
// verify that the path matches a file on disk
File file = new File(filePath);
if (!(file.exists())) {
AbstractIntGeoServerErrorHandler.Factory.getInstance().handleNoResourceFound(this, filePath);
return null;
}
// print the size of that file
ObjectNode responseJsonObject = JsonNodeFactory.instance.objectNode();
responseJsonObject.put("size", file.length());
return responseJsonObject;
}
@Override
public AbstractServiceRequestHandler getInstance() {
return new FileSizeRequestHandler();
}
}
This layer.xml file is needed to register the FileRequestHandler class. The layer.xml file will need to be added to the META-INF/services directory of your source packages. See the project snapshot below for an example:
Here is the same project as seen in the "Files" window:
Make sure the fully qualified name of the FileRequestHandler in the layer.xml matches the FileRequestHandler's fully qualified name in your project.
LAYER.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
<filesystem>
<folder name="com.interactive.intgeoapi.server.requesthandlers.AbstractJSONServiceRequestHandler">
<file name="com.interactive.intgeo.simplewebservice.FileSizeRequestHandler.instance">
<attr name="position" intvalue="7000"/>
</file>
</folder>
</filesystem>
The FileSizeRequestHandlerTest class is a junit test file that can be used to test FileRequestHandler class.
A JUnit test file allows for a web service request class to be testing without having to specify the full JSON string and without having to relying on the INTGeo server running.
FILESIZEREQUESTHANDLERTEST
package com.interactive.intgeo.demoprojects.simplejsonservice;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.interactive.intgeoapi.server.IntGeoServiceContextGlobal;
import com.interactive.intgeoapi.server.TestServiceContext;
import java.io.PrintWriter;
import java.io.StringWriter;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class FileSizeRequestHandlerTest {
public FileSizeRequestHandlerTest() {
}
@Test
public void testHandleRequest() throws Exception {
ObjectNode params = JsonNodeFactory.instance.objectNode();
params.put("file", "C:\\Windows\\regedit.exe");
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
TestServiceContext context = new TestServiceContext(params, writer, "/filesize", "/json");
IntGeoServiceContextGlobal.set(context);
FileSizeRequestHandler instance = new FileSizeRequestHandler();
JsonNode result = instance.handleRequest();
double sizeFound = result.get("size").doubleValue();
assertTrue(sizeFound == 320512);
}
}