Andrea –
Thanks for your response! It turns out this actually IS possible, just took some sleuthing to figure out. I’d been meaning to explain what I did, so I’ll take the opportunity now.
The Java endpoint and the Python endpoint both work fine via GET, but I’ve found that I’m unable to POST requests to either of the URLs. Instead, I get back HTTP/1.1 405 Method Not Allowed. Based off of the logs, it looks like Geoserver doesn’t even receive the request since Tomcat just sends back the 405 right away.
After looking through the code a little bit, the solution to fixing this was fairly simple; in the Resource class (which is the grandparent of HelloResource in the code I posted, and also the AppResource class from the scripting plugin) we have the method:
public boolean allowPost() {
return false;
}
Along with handlePost(), this method needs to be overridden in HelloResource.java/AppResource.java (depending on whether you’re doing pure Java REST or Jython REST). The hack I currently have is just:
@Override
public boolean allowPost() {
return true;
}
@Override
public void handlePost() {
handleGet();
}
That’s the short version. Anyone who’s using JSON-requests can stop here. ( Jythoners will need to recompile and then replace gs-script-core-.jar file. )
For those using the werkzeug module in Python to read multipart form-data, PyAppHook also needs modifications in its createEnviron(Request) method:
String contentInfo = ent.getMediaType().getName();
String boundary = ent.getMediaType().getParameters().getValues(“boundary”);
contentInfo += “; boundary=” + boundary;
environ.put(“CONTENT_TYPE”, contentInfo);
Making the above change means gs-script-py-.jar needs to be recompiled and replaced.
For reasons that I don’t understand, Java and Jython conflict regarding the CONTENT_LENGTH field; I think it’s either an issue with werkzeug itself or org.python.core.PyFile, but I could be wrong. Although it isn’t the ideal approach, I currently adjust the incoming request in Python before accessing request.form to get the values which were sent:
from StringIO import StringIO
from Flask import request
def tweak_request():
“”" Make the necessary adjustments to the incoming request payload, since
Java and Python don’t seem to agree about how Content-Length should be
measured.
rq – the werkzeug Request object
“”"
Below, read through the stream and then re-wrap the data so it can be
read again:
attachment = request.environ[‘wsgi.input’].read()
request.environ[‘CONTENT_LENGTH’] = str(len(attachment))
request.environ[‘wsgi.input’] = StringIO(attachment)
Having to do the above is fine for small requests but might cause performance-issues for larger payloads. Theoretically, I should just be able to set this value in createEnviron right after setting CONTENT_TYPE:
environ.put(“CONTENT_LENGTH”, Long.toString(request.getEntity().getSize()));
It would be nice to discuss some of this further. Should I just submit a pull-request right away, or create an issue in the Atlassian tracker, or… ?
Application Developer
Wyoming Natural Diversity Database
UW Berry Biodiversity Conservation Center
Department 3381, 1000 E. University Av.
Laramie, WY 82071
P: 307-766-3018
···
Hi Patrick,
the people that wrote the python scripting plugins are no more active in the project,
so I’m not sure if you’ll get an answer.
Given that it’s been a while since you asked, I guess nobody here knows the answer either.
If you figure it out and want to provide patches or documentation upgrades they
are more than welcomed!
Cheers
Andrea
On Wed, Sep 21, 2016 at 6:34 AM, P O’Toole <P.OToole@…7834…> wrote:
Hi, list.
I recently installed the Python plugin and got a simple test going, and also built a Java-style REST endpoint just to compare.
The Java endpoint and the Python endpoint both work fine via GET, but I’ve found that I’m unable to POST requests to either of the URLs. Instead, I get back HTTP/1.1 405 Method Not Allowed. Based off of the logs, it looks like Geoserver doesn’t even receive the request since Tomcat just sends back the 405 right away.
Here’s how I’m set up in Python (after having successfully swapped out jython-2.5.2.jar for jython-2.7.0.jar):
/var/lib/tomcat/webapps/geoserver/data/scripts/apps/Analysis/main.py
Adapted from exercise in Colin Henderson’s “Mastering Geoserver”
from flask import Flask, Response
app = Flask(‘Analysis’)
if name == “main”:
app.run()
Works perfectly fine:
@app.route(“//within///”)
def CategoryRadius(type):
return Response(‘{“You searched for type”:’ + ‘"’ + type + ‘"}’,mimetype=‘application/json’)
Doesn’t work:
@app.route(“/within”,methods=[“POST”])
def CategoryRadiusPost():
type = request.form[‘type’]
return Response(‘{“You searched for type”:’ + ‘"’ + type + ‘"}’,mimetype=‘application/json’)
And in Java:
package org.geoserver.rest.hello;
import java.util.List;
import org.geoserver.rest.AbstractResource;
import org.geoserver.rest.format.DataFormat;
import org.restlet.data.Request;
import org.restlet.data.Response;
import java.util.ArrayList;
import org.geoserver.rest.format.StringFormat;
import org.restlet.data.MediaType;
public class HelloResource extends AbstractResource {
@Override
protected List createSupportedFormats(Request request, Response response) {
List formats = new ArrayList();
formats.add(new StringFormat( MediaType.TEXT_PLAIN ));
return formats;
}
@Override
public void handleGet() {
//get the appropriate format
DataFormat format = getFormatGet();
//transform the string “Hello World” to the appropriate response
getResponse().setEntity(format.toRepresentation(“Hello World”));
}
@Override
public void handlePost() {
getResponse().setEntity(“Hello World”, MediaType.TEXT_PLAIN);
}
} // HelloResource class OUT
With applicationContext.xml looking like:
<?xml version="1.0"?>
/hello.{format}
hello
From shell, I’m trying to retrieve the pages like so:
curl http://localhost:8080/geoserver/script/apps/Analysis/within -u ‘admin:’ -d “type=mammals” -d “x=1” -d “y=2” -d “radius=3” -i
curl http://localhost:8080/geoserver/rest/hello.txt -u ‘admin:’ -d “greeting=howdy” -i
Note: the -d flag causes POST to be used. Including -X POST does not change the outcome shown below.
But both pages give me back the response:
HTTP/1.1 405 Method Not Allowed
Allow: GET
Date: Wed, 21 Sep 2016 00:37:51 GMT
Server: Noelios-Restlet-Engine/1.0…8
Transfer-Encoding: chunked
I looked at the gs-rest and gs-restconfig sources a little bit but from what I can tell this is a higher-level Tomcat configuration issue. Where do I make the adjustment to allow fetching pages after POST-ing at them? I already tried specifying cors.allowed.methodsGET,POST in Tomcat’s web.xml under the CORS filter, but it’s clearly being overridden by something that’s buried down deeper.
Thanks
Application Developer
Wyoming Natural Diversity Database
UW Berry Biodiversity Conservation Center
Department 3381, 1000 E. University Av.
Laramie, WY 82071
P: 307-766-3018
Geoserver-users mailing list
Geoserver-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-users
–
==
GeoServer Professional Services from the experts! Visit
http://goo.gl/it488V for more information.
==
Ing. Andrea Aime
@geowolf
Technical Lead
GeoSolutions S.A.S.
Via di Montramito 3/A
55054 Massarosa (LU)
phone: +39 0584 962313
fax: +39 0584 1660272
mob: +39 339 8844549
http://www.geo-solutions.it
http://twitter.com/geosolutions_it
AVVERTENZE AI SENSI DEL D.Lgs. 196/2003
Le informazioni contenute in questo messaggio di posta elettronica e/o nel/i file/s allegato/i sono da considerarsi strettamente riservate. Il loro utilizzo è consentito esclusivamente al destinatario del messaggio, per le finalità indicate nel messaggio stesso. Qualora riceviate questo messaggio senza esserne il destinatario, Vi preghiamo cortesemente di darcene notizia via e-mail e di procedere alla distruzione del messaggio stesso, cancellandolo dal Vostro sistema. Conservare il messaggio stesso, divulgarlo anche in parte, distribuirlo ad altri soggetti, copiarlo, od utilizzarlo per finalità diverse, costituisce comportamento contrario ai principi dettati dal D.Lgs. 196/2003.
The information in this message and/or attachments, is intended solely for the attention and use of the named addressee(s) and may be confidential or proprietary in nature or covered by the provisions of privacy act (Legislative Decree June, 30 2003, no.196 - Italy’s New Data Protection Code).Any use not in accord with its purpose, any disclosure, reproduction, copying, distribution, or either dissemination, either whole or partial, is strictly forbidden except previous formal approval of the named addressee(s). If you are not the intended recipient, please contact immediately the sender by telephone, fax or e-mail and delete the information in this message that has been received in error. The sender does not give any warranty or accept liability as the content, accuracy or completeness of sent messages and accepts no responsibility for changes made after they were sent or for other risks which arise as a result of e-mail transmission, viruses, etc.