Hello.
I was just doing some tests. Basically, client will never send json data during
http post sessions. Will send only text.
So forget about json in the request process. Now, these couple days I made some
tests and I used both methods of rowSetRepresentation.
Namely webRowSet and JdbcResult to process the response. Still none method
returned any response entity.
Finally, I used directly jdbc api methods
(resultset, statement etc) but the server continues to complain producing the
same message.
I am attaching a modified version of jdbcClient class (draft version)
Also, I use json to store temporarily each querying resulting column to json
objects and all wrapped in a json array. The array is converting to string so
the response media type will be text_xml or text_plain.
Also, I tried send a response using directly json (jsonrepresentation & media
type application_json) but without effect.
I modified a lot of times the 'plpgsql' stored procedure which is called from
an xml configuration file but with no luck.
Searching around I noticed that a stored procedure (as the procedure I am
using) that returns a setof result is called using jdbc statement or
preparestatement methods and not with callablestatement (jdbc) methods.
I dont know if I have to use some filter as u suggesting to get eventually the
entity.
That's for know. I look forward for your answer!!!
Thank u.
package restletdatasourcespackage;
import org.antlr.stringtemplate.StringTemplate;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.Client;
import org.restlet.data.Preference;
import org.restlet.data.Request;
import org.restlet.data.Response;
import java.io.File;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.representation.ObjectRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import restletconfigurationpackage.Configuration;
import restletutilitiespackage.IOClass;
import restletutilitiespackage.Service;
import org.restlet.ext.jdbc.JdbcClientHelper;
import org.restlet.ext.jdbc.JdbcResult;
import org.restlet.ext.jdbc.RowSetRepresentation;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.WebRowSet;
/**
* <b>To access to a database</b>
* <p>
* Provides methods to open, request and close a database connection <br />
* DatabaseConnection object is characterized by the following information:
* <ul>
* <li>A Connection object</li>
* <li>A Statement object</li>
* <li>A boolean to know if the connection is started or not</li>
* <li>An JdbcClientConfiguration object to provide information about "how to
* connect"</li>
* </ul>
* </p>
*/
public class JdbcClient {
private JdbcClientConfiguration jdbcConfiguration;
private Configuration configuration;
private IOClass ioClass;
// private Configuration configuration;
/**
* Constructor JdbcClient
* <p>
* Creates a new DatabaseConnection object with the specified Configuration
* object
* </p>
*
*/
public JdbcClient(Configuration configuration) {
this.configuration = configuration;
ioClass = new IOClass(configuration);
}
/**
* Creates and sends the query using pool connection
*
* @param query
* the SQL query
* @return the result of the request
* @throws JSONException
*/
public ResultSet performRequest(Service service, String query) {
StringTemplate queryTemplate = new StringTemplate(ioClass
.loadFile(new File(configuration.getUrlXMLTemplateQuery())));
jdbcConfiguration = service.getJdbcClientConfiguration();
// Makes the SQL query
queryTemplate.setAttribute("user", jdbcConfiguration.getUser());
queryTemplate.setAttribute("password", jdbcConfiguration.getPassword());
queryTemplate.setAttribute("query", query);
ResultSet result=null;
Client client = new Client(Protocol.JDBC);
JdbcClientHelper helper = new JdbcClientHelper(client);
try {
Class.forName(jdbcConfiguration.getDriver());
} catch (ClassNotFoundException e) {
Logger.getLogger(JdbcClient.class.getName()).log(Level.SEVERE,
"ERROR: Can not find the driver", e);
}
Representation req = new StringRepresentation(queryTemplate.toString()
, MediaType.TEXT_XML);
Request request=JdbcClientHelper.create(jdbcConfiguration.getURL(),
req);
//request.getClientInfo().getAcceptedMediaTypes()
//.add(new Preference(MediaType.TEXT_PLAIN));
Response response = new Response(request);
helper.handle(request, response);
/*RowSetRepresentation output =
(RowSetRepresentation)response.getEntity();
try {
JdbcResult jdbcResult =
(JdbcResult)output.getJdbcResult();
result = jdbcResult.getResultSet();
} catch (SQLException e) {
e.printStackTrace();
}
*/
/*List<Variant> variants = new ArrayList<Variant>();
variants.add(new Variant(MediaType.APPLICATION_JSON));
variants.add(new Variant(MediaType.TEXT_PLAIN));*/
Connection con=null;
try {
con = DriverManager.getConnection(
jdbcConfiguration.getURL(), jdbcConfiguration.getUser(),
jdbcConfiguration.getPassword());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Statement stmt = null;
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
stmt.execute(query);
result=stmt.getResultSet();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*Response response = new Response(request);
helper.handle(request, response);
WebRowSet rowSet= ((RowSetRepresentation) response.getEntity())
.getWebRowSet();*/
//if(MediaType.MULTIPART_FORM_DATA.equals(request.getEntity().getMediaType()))
/*RowSetRepresentation rset = new RowSetRepresentation(rowSet);
if (rset.getMediaType()==MediaType.TEXT_JAVASCRIPT){
JSONObject json = null;
try {
//json = new JsonRepresentation(response.getEntity()).toJsonObject();
json = new JsonRepresentation(response.getEntity()).getJsonObject();
} catch (JSONException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
JSONObject jsonResultSet = json.getJSONObject("ResultSet");
result=(ResultSet)jsonResultSet;
//result=(ResultSet)json;
}
else {*/
/*try {
//client.put(jdbcConfiguration.getURL(), (JsonRepresentation) rowSet);*/
//rowSet.setType(WebRowSet.TYPE_SCROLL_SENSITIVE);
/*if (response.getEntity()!=null){*/
//result = rowSet;
//} catch(Exception e) {
/* Logger
.getLogger(JdbcClient.class.getName())
.log(
Level.SEVERE,
"ERROR: Can not cast the result from WebRowSet to ResultSet",e);
}*/
//}
return result;
}
}