Getting Exception while calling WebDAV propFindMethod in java - httpclient

Getting below Exception::
WARN main [DavDocumentBuilderFactory.createFactory] - Secure XML processing is not supported
java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
at org.apache.jackrabbit.webdav.xml.DavDocumentBuilderFactory.createFactory(DavDocumentBuilderFactory.java:50)
at org.apache.jackrabbit.webdav.xml.DavDocumentBuilderFactory.<init>(DavDocumentBuilderFactory.java:39)
at org.apache.jackrabbit.webdav.xml.DomUtil.<clinit>(DomUtil.java:60)
at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:203)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:72)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:61)
at com.spero.poc.WebDavClient.listOfFilesFromWebDav(WebDavClient.java:108)
at com.spero.poc.WebDavClient.main(WebDavClient.java:92)
Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:377)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:131)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:684)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:728)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:343)
at org.apache.jackrabbit.webdav.xml.DomUtil.transformDocument(DomUtil.java:805)
at org.apache.jackrabbit.webdav.client.methods.XmlRequestEntity.<init>(XmlRequestEntity.java:43)
at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:193)
at org.apache.jackrabbit.webdav.client.methods.DavMethodBase.setRequestBody(DavMethodBase.java:205)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:72)
at org.apache.jackrabbit.webdav.client.methods.PropFindMethod.<init>(PropFindMethod.java:61)
at com.spero.poc.WebDavClient.listOfFilesFromWebDav(WebDavClient.java:109)
at com.spero.poc.WebDavClient.main(WebDavClient.java:92)
Here is my code::
package com.spero.poc;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
public class WebDavClient {
static HttpClient client = null;
public static void main(String[] args) {
// String uri = "https://www.webdavserver.com/Userab71566/Library/Content.txt";
String uri = "https://www.webdavserver.com/Userab71566/Library/";
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost("https://www.webdavserver.com");
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
int maxHostConnections = 20;
params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
params.setConnectionTimeout(60000);
connectionManager.setParams(params);
HttpClientParams cParams = new HttpClientParams();
cParams.setSoTimeout(60000);
// Create the HttpClient object and eventually pass the Credentials if required:
client = new HttpClient(cParams,connectionManager);
client.setHostConfiguration(hostConfig);
// Credentials creds = new UsernamePasswordCredentials("userId", "pw");
// client.getState().setCredentials(AuthScope.ANY, creds);
try {
WebDavClient test = new WebDavClient();
test.listOfFilesFromWebDav(uri);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
client = null;
}
if(client == null)
System.out.println("Client is in null state");
}
private List<String> listOfFilesFromWebDav(String folderUrl) {
try {
PropFindMethod pFind = new PropFindMethod(folderUrl, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
pFind.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
client.executeMethod(pFind);
List<String> files = new ArrayList<>();
MultiStatus multiStatus = pFind.getResponseBodyAsMultiStatus();
MultiStatusResponse[] responses = multiStatus.getResponses();
for (MultiStatusResponse response : responses) {
if (response.getHref().endsWith(".txt")) {
System.out.println("Data::"+response.getHref());
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DavException e) {
e.printStackTrace();
}
return null;
}
}

What version of Jackrabbit?
Looking at the source I see that the exception is actually caught and logged, and the system should proceed.
See https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DavDocumentBuilderFactory.java#L43
Are you really getting an exception in the code?

Thanks for the suggestion, I have gone through the logs and find out the mismatched class and jars. I have added the below system settings to my code and the Jar.
We have to resort to System.setProperty(...) calls from your code, eg:
//this would enforce the DOM implementation to be Xerces
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
//this would enforce the SAX implementation to be Xerces
System.setProperty("javax.xml.parsers.SAXParserFactory","org.apache.xerces.jaxp.SAXParserFactoryImpl");
//this would enforce the XSLT implementation to be Xalan
System.setProperty("javax.xml.transform.TransformerFactory","org.apache.xalan.processor.TransformerFactoryImpl");
The respective JRE implementation classes are:
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

Related

Californium Framework CoAP and PUT request

I am trying to do a request to coap server (er-rest-example) using Californium.
I succesfully do a POST request.
But with PUT I am getting a BAD REQUEST, I try using this URLs in url:
coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds
coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds?
coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds?color=r
But with no one get success.
What I am doing wrong?.
This is my simple script:
package coap_client;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
public class cliente {
public static void main(String[] args) throws Exception {
Timer timer;
timer = new Timer();
TimerTask task = new TimerTask(){
#Override
public void run(){
String url="coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds";
URI uri= null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
}
CoapClient client = new CoapClient(uri);
CoapResponse response = client.put("color=r",MediaTypeRegistry.TEXT_PLAIN);
System.out.println(response.isSuccess());
if (response!=null) {
byte[] myreponse=response.getPayload();
String respuesta2 = new String(myreponse);
System.out.println(respuesta2);
}
}
};
timer.schedule(task, 10,10*1000);
}
}
In Contiki er-rest-example, see the POST/PUT handler(1) for the LED CoAP resource. It expects a mode param without which you will get a BAD_REQUEST as response. I assume that has to go in the request body.

Eclipse Luna F6 doesn't step over

Strange, I have a Java application in Eclipse Luna and when debugging it and I'm trying to use F6 it doesn't "step over", but instead it "steps into".
I was trying to build a Java applicaton in Eclipse Luna. It was a database application from
here.
With two source files (DB.java):
`import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
public Connection conn = null;
public DB() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/Crawler";
conn = DriverManager.getConnection(url, "root", "admin213");
System.out.println("conn built");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public ResultSet runSql(String sql) throws SQLException {
Statement sta = conn.createStatement();
return sta.executeQuery(sql);
}
public boolean runSql2(String sql) throws SQLException {
Statement sta = conn.createStatement();
return sta.execute(sql);
}
#Override
protected void finalize() throws Throwable {
if (conn != null || !conn.isClosed()) {
conn.close();
}
}
}`
and Main.java:
`import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Main {
public static DB db = new DB();
public static void main(String[] args) throws SQLException, IOException {
db.runSql2("TRUNCATE Record;");
processPage("http://www.mit.edu");
}
public static void processPage(String URL) throws SQLException, IOException{
//check if the given URL is already in database
String sql = "select * from Record where URL = '"+URL+"'";
ResultSet rs = db.runSql(sql);
if(rs.next()){
}else{
//store the URL to database to avoid parsing again
sql = "INSERT INTO `Crawler`.`Record` " + "(`URL`) VALUES " + "(?);";
PreparedStatement stmt = db.conn.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, URL);
stmt.execute();
//get useful information
Document doc = Jsoup.connect("http://www.mit.edu/").get();
if(doc.text().contains("research")){
System.out.println(URL);
}
//get all links and recursively call the processPage method
Elements questions = doc.select("a[href]");
for(Element link: questions){
if(link.attr("href").contains("mit.edu"))
processPage(link.attr("abs:href"));
}
}
}
}`
I forgot to include the mysql-connector-java-5.1.16-bin.jar into the buildpath.
This resulted in an exception occuring in the initialization that occurs in the DB()
initializer code. At least this is my assumption that the behaviour of Eclipse was
irritated (maybe it's a feature?) and the step over didn't work right.

Map Reduce Distributed Cache

I am not able to compile my DriverClass at the job.waitforcompletion(boolean) clause.It gives me a NoClassFoundException.If I catch the exception ,the run method throws the error that its expecting a int value.I am using MapReduce New API.Could anyone suggest what is the issue :
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class Dist_Driver extends Configured implements Tool {
public int run(String args[]) throws IOException, InterruptedException {
// Configuration phase
// Configuration conf=new Configuration();
Job job = new Job(new Configuration());
job.setJarByClass(Dist_Driver.class);
// Mapper Reducer InputFormat
job.setInputFormatClass(FileInputFormat.class);
// Mapper and Reducer Class
job.setMapperClass(Dist_Mapper.class);
job.setReducerClass(DistCache_Reducer.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setInputFormatClass(KeyValueTextInputFormat.class);
// set FileInputOutput
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// setting number of reduce tasks and submit it
job.setNumReduceTasks(2);
// Lets check if the file exist
File f1 = new File("/home/hdfs/trials_mapreduce_progams/emp_id");
if (f1.exists())
System.out.println("The Files Exists");
else
System.out.println("The File doesnot exist");
URI path1;
try {
path1 = new URI(
"/home/hdfs/trials_mapreduce_progams/emp_lookup.txt");
DistributedCache.addCacheFile(path1, job.getConfiguration());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (job.waitForCompletion(true))
return 0;
else
return 1;
}
public static void main(String[] args) throws Exception {
int exitcode = ToolRunner.run(new Dist_Driver(), args);
System.exit(exitcode);
}
}
Just add the ClassNotFoundException to the run method signature
public int run(String args[]) throws IOException,
InterruptedException,
ClassNotFoundException {
The reason you get an error when you try and try/catch it is because if there is a ClassNotFoundException thrown during execution, there will be no return value, and the method has to return something.
If you really want to catch it, just return 1 in the catch clause, which is the error exit code

Client IP address using emulator

I am currently writing this code for my client and server,
and I want to test it out using my emulator, but I'm stuck.
is this the correct IP address that I should be using?
socket = new Socket("10.0.2.2", 6000);
If i want to use my phone to test this out, what ip address should i be using?
thanks.
if you want to send messages between server/client, here is a sample code that i have made before.
please refer to the code below and feel free to comment!
also, that is the correct ip address to use when using emulator for simulation.
in addition, don't forget to change your permission to "android.permission.INTERNET" in your manifesto.
=================================myClient==================================
package com.example.myclient;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Manifest --> uses permission --> "android.permission.INTERNET" */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
class MyThread extends Thread {
#Override
public void run() {
super.run();
Log.d("client", "thread is running...");
String str = "Do you want to eat hamburger?";
Socket socket;
try {
socket = new Socket("10.0.2.2", 6000);
ObjectOutputStream out = new ObjectOutputStream(socket
.getOutputStream());
ObjectInputStream in = new ObjectInputStream(
socket.getInputStream());
out.writeObject(str);
String rcv = (String) in.readObject();
Log.d("client", "Server :" + rcv);
out.close();
in.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MyThread t = new MyThread();
t.start();
}
});
}
}
============================MyServer========================================
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket server = new ServerSocket(6000);
System.out.println("waiting.....");
while (true) {
Socket socket = server.accept();
System.out.println("a client has connected...");
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
ObjectInputStream objIn = new ObjectInputStream(in);
ObjectOutputStream objOut = new ObjectOutputStream(out);
String str = (String) objIn.readObject();
System.out.println("client : " + str);
objOut.writeObject("No, I'm on a diet!!!");
objIn.close();
objOut.close();
socket.close();
}
}
}
10.0.2.2 will be the correct IP you are using emulator. 127.0.0.1 will be the IP if you are developing on the machine(client and server on same machine). As you said you want to test it in your mobile run the following code and you will get your IP(it will also work if you are on computer):
public class net
{
net() throws UnknownHostException
{
InetAddress ia=InetAddress.getLocalHost();
System.out.println(ia);
ia=InetAddress.getByName("local host");
System.out.println(ia);
}
public static void main(String args[])throws UnknownHostException
{
net a=new net();
}
}

Cassandra / Eclipse - Can't make connection

So I'm using Cassandra in my project and I have to make a connection between Eclipse and the data base. I tried to use a JDBC-compliant driver that I found on code.google.com but I'm getting this exception:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
This is my code:
package cassandrasampledriver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.cassandra.cql.jdbc.DriverResolverException;
import org.apache.cassandra.cql.jdbc.InvalidUrlException;
public class CassandraDriver
{
public static void main(String[] args) {
Connection con = null;
String KS = "cassandrademocql";
try
{
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/" + KS);
Statement stmt = con.createStatement();
String query = "DROP KEYSPACE cassandrademocql;";
ResultSet result = stmt.executeQuery(query);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
Thanks in advance :)
I ran your code using this cql driver (Version 1.1.2 compatible with cassandra 1.2.
on JDK6 without an Error. Maybe you are missing references in your classpath? The only problem with the code is that you are assigning a variable to a method call that wont return anything ResultSet result = stmt.executeQuery(query); and so a java.sql.SQLNonTransientException exception will be thrown.
You said you are new to Cassandra, just some friendly advice, I would do some research on the available APIs for Cassandra before making a choice.
These are the JARs I used
apache-cassandra-1.2.0-rc1-SNAPSHOT.jar
apache-cassandra-clientutil-1.2.0-rc1-SNAPSHOT.jar
apache-cassandra-thrift-1.2.0-rc1-SNAPSHOT.jar
cassandra-jdbc-1.1.2.jar libthrift-0.9.0.jar log4j-1.2.16.jar
log4j-over-slf4j-1.7.2.jar selenium-server-standalone-2.21.0.jar
slf4j-api-1.7.2.jar slf4j-simple-1.7.2.jar
Full code that I ran:
package cassandrasampledriver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CassandraDriver {
public static void main(String[] args) {
Connection con = null;
String KS = "cassandrademocql";
try
{
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/" + KS);
Statement stmt = con.createStatement();
String query = "DROP KEYSPACE cassandrademocql;";
// Because you are droping the KS this will not return anything
// So the result set will be null and a java.sql.SQLNonTransientException exception will be thrown every time.
ResultSet result = stmt.executeQuery(query);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}