Skip to content

Commit

Permalink
Merge pull request #9 from maartenplieger/master
Browse files Browse the repository at this point in the history
Added adagucopendap endpoint for forwarding opendap server requests t…
  • Loading branch information
maartenplieger authored Mar 8, 2018
2 parents 48ede6f + 23dc860 commit 40b26bf
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 142 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>nl.knmi.adagucservices</groupId>
<artifactId>adaguc-services</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<packaging>war</packaging>

<name>adaguc-services</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,21 @@ public void ADAGUCSERVERWCS(HttpServletResponse response, HttpServletRequest req
}

}
@ResponseBody
@RequestMapping("adagucopendap/**")
public void ADAGUCSERVEROPENDAP(HttpServletResponse response, HttpServletRequest request){

try {
ADAGUCServer.runADAGUCOpenDAP(request,response,null,null);
} catch (Exception e) {
JSONResponse jsonResponse = new JSONResponse(request);
jsonResponse.setException("ADAGUCServer OPENDAP request failed",e);
try {
jsonResponse.print(response);
} catch (Exception e1) {

}
}

}
}
297 changes: 156 additions & 141 deletions src/main/java/nl/knmi/adaguc/services/adagucserver/ADAGUCServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,149 +29,164 @@
*/
public class ADAGUCServer extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Runs the ADAGUC WMS server as executable on the system.
* Emulates the behavior of scripts in a traditional cgi-bin directory of apache http server.
* @param request, the HttpServletRequest to obtain querystring parameters from.
* @param response Can be null, when given the content-type for the response will be set.
* Results are not sent to this stream, this is done by outputStream parameter
* @param queryString The querystring for the CGI script
* @param outputStream A standard byte output stream in which the data of stdout is captured.
* When null, it will be set to response.getOutputStream().
* @throws Exception
*/
public static void runADAGUCWMS(HttpServletRequest request,HttpServletResponse response,String queryString,OutputStream outputStream) throws Exception{
runADAGUC(request,response, queryString, outputStream, ADAGUCServiceType.WMS);
}
/**
* Runs the ADAGUC WCS server as executable on the system.
* Emulates the behavior of scripts in a traditional cgi-bin directory of apache http server.
* @param request, the HttpServletRequest to obtain querystring parameters from.
* @param response Can be null, when given the content-type for the response will be set.
* Results are not sent to this stream, this is done by outputStream parameter
* @param queryString The querystring for the CGI script
* @param outputStream A standard byte output stream in which the data of stdout is captured.
* When null, it will be set to response.getOutputStream().
* @throws Exception
*/
public static void runADAGUCWCS(HttpServletRequest request,HttpServletResponse response,String queryString,OutputStream outputStream) throws Exception{
runADAGUC(request,response, queryString, outputStream, ADAGUCServiceType.WCS);
}
enum ADAGUCServiceType{
WMS, WCS
}
public static void runADAGUC(HttpServletRequest request,HttpServletResponse response,String queryString,OutputStream outputStream, ADAGUCServiceType serviceType) throws Exception{

Debug.println("runADAGUCWMS");
List<String> environmentVariables = new ArrayList<String>();
String userHomeDir="/tmp/";

AuthenticatorInterface authenticator = AuthenticatorFactory.getAuthenticator(request);
if(authenticator != null){
try {
userHomeDir = UserManager.getUser(authenticator).getHomeDir();
} catch(Exception e){
/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Runs the ADAGUC WMS server as executable on the system.
* Emulates the behavior of scripts in a traditional cgi-bin directory of apache http server.
* @param request, the HttpServletRequest to obtain querystring parameters from.
* @param response Can be null, when given the content-type for the response will be set.
* Results are not sent to this stream, this is done by outputStream parameter
* @param queryString The querystring for the CGI script
* @param outputStream A standard byte output stream in which the data of stdout is captured.
* When null, it will be set to response.getOutputStream().
* @throws Exception
*/
public static void runADAGUCWMS(HttpServletRequest request,HttpServletResponse response,String queryString,OutputStream outputStream) throws Exception{
runADAGUC(request,response, queryString, outputStream, ADAGUCServiceType.WMS);
}
/**
* Runs the ADAGUC WCS server as executable on the system.
* Emulates the behavior of scripts in a traditional cgi-bin directory of apache http server.
* @param request, the HttpServletRequest to obtain querystring parameters from.
* @param response Can be null, when given the content-type for the response will be set.
* Results are not sent to this stream, this is done by outputStream parameter
* @param queryString The querystring for the CGI script
* @param outputStream A standard byte output stream in which the data of stdout is captured.
* When null, it will be set to response.getOutputStream().
* @throws Exception
*/
public static void runADAGUCWCS(HttpServletRequest request,HttpServletResponse response,String queryString,OutputStream outputStream) throws Exception{
runADAGUC(request,response, queryString, outputStream, ADAGUCServiceType.WCS);
}
enum ADAGUCServiceType{
WMS, WCS, OPENDAP
}
public static void runADAGUC(HttpServletRequest request,HttpServletResponse response,String queryString,OutputStream outputStream, ADAGUCServiceType serviceType) throws Exception{

Debug.println("runADAGUCWMS");
List<String> environmentVariables = new ArrayList<String>();
String userHomeDir="/tmp/";

AuthenticatorInterface authenticator = AuthenticatorFactory.getAuthenticator(request);
if(authenticator != null){
try {
userHomeDir = UserManager.getUser(authenticator).getHomeDir();
} catch(Exception e){

}

}
Debug.println("Using home " + userHomeDir);
String homeURL=MainServicesConfigurator.getServerExternalURL();
String adagucExecutableLocation = ADAGUCConfigurator.getADAGUCExecutable();
Debug.println("adagucExecutableLocation: "+adagucExecutableLocation);

if(adagucExecutableLocation == null){
Debug.errprintln("Adagucserver executable not configured");
throw new Exception("Adagucserver executable not configured");
}

File f=new File(adagucExecutableLocation);
if(f.exists() == false || f.isFile() == false){
Debug.errprintln("Adagucserver executable not found");
throw new Exception("Adagucserver executable not found");
}



if(response == null && outputStream == null){
throw new Exception("Either response or outputstream needs to be set");
}

if(request == null && queryString == null){
throw new Exception("Either request or queryString needs to be set");
}

if(outputStream == null){
outputStream = response.getOutputStream();
}

if(queryString == null){
queryString = request.getQueryString();
}



environmentVariables.add("HOME="+userHomeDir);
environmentVariables.add("QUERY_STRING="+queryString);
if(serviceType == ADAGUCServiceType.WMS){
environmentVariables.add("ADAGUC_ONLINERESOURCE="+homeURL+"/wms?");
}
if(serviceType == ADAGUCServiceType.WCS){
environmentVariables.add("ADAGUC_ONLINERESOURCE="+homeURL+"/wcs?");
}

if(serviceType == ADAGUCServiceType.OPENDAP){

/* localenv['SCRIPT_NAME']="/myscriptname";
SCRIPT_NAME [/cgi-bin/autoresource.cgi],
REQUEST_URI [/cgi-bin/autoresource.cgi/opendap/clipc/combinetest/wcs_nc2.nc.das]
localenv['REQUEST_URI']="/myscriptname/" + path*/
environmentVariables.add("ADAGUC_ONLINERESOURCE="+homeURL+"/adagucopendap?");
environmentVariables.add("REQUEST_URI="+request.getRequestURI());
environmentVariables.add("SCRIPT_NAME=");
Debug.println(request.getRequestURI());
}

Tools.mksubdirs(userHomeDir+"/adaguctmp/");
environmentVariables.add("ADAGUC_TMP="+userHomeDir+"/adaguctmp/");

String[] configEnv = ADAGUCConfigurator.getADAGUCEnvironment();
if(configEnv == null){
Debug.println("ADAGUC environment is not configured");
}else{
for(int j=0;j<configEnv.length;j++)environmentVariables.add(configEnv[j]);
}

}
Debug.println("Using home " + userHomeDir);
String homeURL=MainServicesConfigurator.getServerExternalURL();
String adagucExecutableLocation = ADAGUCConfigurator.getADAGUCExecutable();
Debug.println("adagucExecutableLocation: "+adagucExecutableLocation);

if(adagucExecutableLocation == null){
Debug.errprintln("Adagucserver executable not configured");
throw new Exception("Adagucserver executable not configured");
}

File f=new File(adagucExecutableLocation);
if(f.exists() == false || f.isFile() == false){
Debug.errprintln("Adagucserver executable not found");
throw new Exception("Adagucserver executable not found");
}



if(response == null && outputStream == null){
throw new Exception("Either response or outputstream needs to be set");
}

if(request == null && queryString == null){
throw new Exception("Either request or queryString needs to be set");
}

if(outputStream == null){
outputStream = response.getOutputStream();
}

if(queryString == null){
queryString = request.getQueryString();
}



environmentVariables.add("HOME="+userHomeDir);
environmentVariables.add("QUERY_STRING="+queryString);
if(serviceType == ADAGUCServiceType.WMS){
environmentVariables.add("ADAGUC_ONLINERESOURCE="+homeURL+"/wms?");
}
if(serviceType == ADAGUCServiceType.WCS){
environmentVariables.add("ADAGUC_ONLINERESOURCE="+homeURL+"/wcs?");
}

Tools.mksubdirs(userHomeDir+"/adaguctmp/");
environmentVariables.add("ADAGUC_TMP="+userHomeDir+"/adaguctmp/");

String[] configEnv = ADAGUCConfigurator.getADAGUCEnvironment();
if(configEnv == null){
Debug.println("ADAGUC environment is not configured");
}else{
for(int j=0;j<configEnv.length;j++)environmentVariables.add(configEnv[j]);
}
String commands[] = {adagucExecutableLocation};

String[] environmentVariablesAsArray = new String[ environmentVariables.size() ];
environmentVariables.toArray( environmentVariablesAsArray );
CGIRunner.runCGIProgram(commands,environmentVariablesAsArray,userHomeDir,response,outputStream,null);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
Debug.println("Handle ADAGUC WMS requests");
OutputStream out1 = null;
//response.setContentType("application/json");
try {
out1 = response.getOutputStream();
} catch (IOException e) {
Debug.errprint(e.getMessage());
return;
}

try {
ADAGUCServer.runADAGUCWMS(request,response,request.getQueryString(),out1);

} catch (Exception e) {
response.setStatus(401);
try {
out1.write(e.getMessage().getBytes());
} catch (Exception e1) {
Debug.errprintln("Unable to write to stream");
Debug.printStackTrace(e);
}
}
}
String commands[] = {adagucExecutableLocation};

String[] environmentVariablesAsArray = new String[ environmentVariables.size() ];
environmentVariables.toArray( environmentVariablesAsArray );
CGIRunner.runCGIProgram(commands,environmentVariablesAsArray,userHomeDir,response,outputStream,null);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
Debug.println("Handle ADAGUC WMS requests");
OutputStream out1 = null;
//response.setContentType("application/json");
try {
out1 = response.getOutputStream();
} catch (IOException e) {
Debug.errprint(e.getMessage());
return;
}

try {
ADAGUCServer.runADAGUCWMS(request,response,request.getQueryString(),out1);

} catch (Exception e) {
response.setStatus(401);
try {
out1.write(e.getMessage().getBytes());
} catch (Exception e1) {
Debug.errprintln("Unable to write to stream");
Debug.printStackTrace(e);
}
}
}
public static void runADAGUCOpenDAP(HttpServletRequest request,HttpServletResponse response,String queryString,OutputStream outputStream) throws Exception {
runADAGUC(request,response, queryString, outputStream, ADAGUCServiceType.OPENDAP);
}


}

0 comments on commit 40b26bf

Please sign in to comment.