I forgot about one other important aspect of this. Query only defines a single serializer property. Currently, that serializer is used to read GETs and write POSTs and PUTs. Will you be able to use a single serializer to address your use case? I imagine so, since Serializer defines both readObject() and writeObject(), but I just wanted to make sure.
If so, it might be possible to lift the restriction on line 469 that requires a GET request in order to return data. Does anyone know if it is possible to determine if a request contains a response body? Perhaps that check could be used to gate the if block instead of method == Method.GET. G On Aug 6, 2011, at 9:49 AM, Thomas G. Schuessler wrote: > > [email protected] wrote: >> >> Yes, but this shouldn't be too difficult. You can probably use the >> existing GetQuery and PostQuery classes as a guide. Let us know if you >> have any questions. >> > > Below please see my first non-working humble attempt. The problem is here: > > inputStream = connection.getInputStream(); > > I cannot access the connection since it is local to Query's execute method. > The only ways I can think of to fix this, are: > 1. Copy the complete Query.execute, modify it in MyPostQuery, and not call > the inherited method at all. > 2. Create my own abstract Query class and subclass that. > > Both choices seem to come with maintenance issues for future releases of > Pivot... > > Any alternatives I have overlooked? If not, would you choose 1 or 2? > > -------------------------------------------- > package pivot.test; > > import java.io.InputStream; > import java.util.concurrent.ExecutorService; > > import org.apache.pivot.json.JSONSerializer; > import org.apache.pivot.serialization.Serializer; > import org.apache.pivot.web.Query; > import org.apache.pivot.web.QueryException; > > public class MyPostQuery extends Query { > public static final Method METHOD = Method.POST; > > private Object value = null; > private Serializer<?> serializer = new JSONSerializer(); > > public MyPostQuery(String hostname, String path) { > this(hostname, DEFAULT_PORT, path, false); > } > > public MyPostQuery(String hostname, int port, String path, boolean > secure) > { > this(hostname, port, path, secure, DEFAULT_EXECUTOR_SERVICE); > } > > public MyPostQuery(String hostname, int port, String path, boolean > secure, > ExecutorService executorService) { > super(hostname, port, path, secure, executorService); > } > > @Override > public Method getMethod() { > return METHOD; > } > > public Object getValue() { > return this.value; > } > > public void setValue(Object value) { > this.value = value; > } > > @Override > public Object execute() throws QueryException { > execute(METHOD, this.value); > if (getStatus() == Status.CREATED) { > InputStream inputStream = null; > try { > inputStream = connection.getInputStream(); > this.value = this.serializer.readObject(new > MonitoredInputStream( > inputStream)); > } finally { > if (inputStream != null) { > inputStream.close(); > } > } > > } > return this.value; > } > > } > > > -- > View this message in context: > http://apache-pivot-users.399431.n3.nabble.com/Invoking-an-HTTP-POST-service-that-returns-a-value-object-not-a-URL-tp3230971p3231076.html > Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
