Apache-cxf client gets SOAPFaultException instead of custom exception - exception

I'm having troubles with handling Exceptions in my apache-cxf client.
I'm expecting an exception that is thrown by the soap call 'Fault_Exception' but I'm always receiving the common SOAPFaultException
Generated classes:
#WebFault(name = "FaultMessage", targetNamespace = "http://a.b.c/ws/")
public class Fault_Exception extends Exception {
private a.b.c.Fault faultMessage;
....
}
and Fault is
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "fault", propOrder = {
"code",
"description",
"stack"
})
public class Fault {
...
}
soap:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>EXCEPTION</faultstring>
<detail>
<Fault xmlns:n1="http://a.b.c/ws/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://a.b.c/ws/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="n1:fault">
<code xmlns="http://a.b.c/ws/">error.code.1</code>
<description xmlns="http://a.b.c/ws/">some description of the error.</description>
<stack xmlns="http://a.b.c/ws/">....</stack>
</Fault>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Is there a mismatch between the generated classes and the response ?
Or what else can be the problem of this?
Or is it totally logic that I get a SOAPFaultException? I have no clue how to get the information (code & description) then ...
Kind regards!

Related

exception handling in apache servicemix

I am new to ServiceMix am trying on exception handling in ServiceMix.
when I try to print the exception, it also contains the body of the request in it. Is there any way that I can extract only the errors from exception?
<from uri="activemq:topic://topic1"/>
<!-- Schema validationm for the request received from Biblio -->
<doTry>
<to uri="validator:http://localhost/employee.xsd"/>
<doCatch>
<exception>java.lang.Exception</exception>
<log message="${exception.message}"/>
</doCatch>
</doTry>
below is the logged exception:
org.apache.xerces.jaxp.validation.SimpleXMLSchema#a0059b5
errors: [
org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'asd' is not a valid value for 'integer'., Line : -1, Column : -1
org.xml.sax.SAXParseException: cvc-type.3.1.3: The value 'asd' of element 'empnumber' is not valid., Line : -1, Column : -1
]. Exchange[ID-GBSMIXDEV01-uk-oup-com-46713-1511957485149-83-2][Message: <empRecord>
<employee>
<empnumber>asd</empnumber>
<surname>PM</surname>
<firstname>Abhinay</firstname>
</employee>
</empRecord>]
I am getting the correct exception but I dont want the below part in the exception.
Is there any way I can remove these from the exception message?
Exchange[ID-GBSMIXDEV01-uk-oup-com-46713-1511957485149-83-2][Message: <empRecord>
<employee>
<empnumber>asd</empnumber>
<surname>PM</surname>
<firstname>Abhinay</firstname>
</employee>
</empRecord>]
That message is coming from the exception, so you can manipulate the string inside a Processor.
Write a simple class to do this:
private static final Logger logger_ = LoggerFactory
.getLogger(ExceptionMsgProcessor.class);
public void process(Exchange exchange) {
Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
String msg = e.getMessage();
// manipulate the string here
log.info("Exception message: {}", msg);
}
And then send the Exception to this bean
<doTry>
<to uri="validator:http://localhost/employee.xsd"/>
<doCatch>
<exception>java.lang.Exception</exception>
<to uri="bean:exceptionMsgProcessor" />
</doCatch>
</doTry>

primefaces graphicimage CDI bean doesn't work

i had some problem showing images retrieved by my db.
View caller:
<p:graphicImage value="#{appController.image}" height="200 px" >
<f:param name="oid" value="#{item.oid}" />
</p:graphicImage>
Controller:
#Named("appController")
#ApplicationScoped
public class AppController {
#Inject
private MultimediaFacade multimediaFacade;
public StreamedContent getImage() throws IOException {
System.out.println("getting image")
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
} else {
// So, browser is requesting the image. Return a real StreamedContent with the image bytes.
String imageId = context.getExternalContext().getRequestParameterMap().get("oid");
int oid=Integer.parseInt(imageId);
System.out.println(oid);
Multimedia image = multimediaFacade.find(oid);
System.out.println(Arrays.toString(image.getFileBlob()));
return new DefaultStreamedContent(new ByteArrayInputStream(image.getFileBlob()));
}
}
}
this code shows nothing and it looks like the method is never called (never print in console)!
after days of trial changing the scope, i tried to use #ManagedBean instead of #Named, and it works!!!
can someone explain me why this work only with #ManagedBean and not with #Named?
Check that you have javax.enterprise.context.ApplicationScoped in imports.
If you have a different import for #ApplicationScoped (e.g. javax.faces.bean.ApplicationScoped), then you need to configure CDI to discover all beans instead of only those with CDI annotations (which is the default)
To tun discovery for all beans, either add empty beans.xml into WEB-INF directory, or if you already have beans.xml there, add bean-discovery-mode="all" into the <beans> element, like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>

Spring 4 RestController & Returning JSON

I have a simple controller, which should return JSON, but is failing to do so. The JSON library is Jackson, configured as a maven dependency. When I make a request using postman, against this url path, I am receiving a 404 error. When I attempt to inspect the JSON Returned, I see "Malformed JSON: Unexpected '<'".
Could someone suggest what it is I am missing / failing to understand? Thank you
#RestController
#RequestMapping("/World/")
public class RestfulController {
#RequestMapping(value = "/Country/", method = RequestMethod.GET, produces="application/json")
public ResponseEntity<Country> findAllCountrys(){
Country c = new Country(1, "Ethiopia", "Addis Abba", "94 Million");
return new ResponseEntity<Country>(c, HttpStatus.OK);
}
}
spring application context
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.restfulapp.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
</bean>
You should try this:
#RestController
#RequestMapping("/World")
public class RestfulController {
#RequestMapping(value = "/Country", method = RequestMethod.GET)
public Country findAllCountrys(){
Country c = new Country(1, "Ethiopia", "Addis Abba", "94 Million");
return c;
}
}
The request url: http://yourhost/World/Country
This should return a json of Country

Insert XmlFile (or other) from camel route to mongoDB

I've been trying to insert a XML file into mongoDB with camel and I can't manage to make it work.
I've followed this tutorial for the first steps:
http://www.pretechsol.com/2014/09/apache-camel-mongodb-component-example.html
In my route, I convert it in JSON then use 'convertBodyTo(string.class) for mongo to recognize the file.
The code works well with regular route (sending the file to another folder for example). But when I run it for mongoDB, all I get in the console is my Process message again and again with my databased never being filled.As I don't receive any error message, I don't know how to find where the problem come from.
The mongoDB name, ip, users, password have been already checked multiple times.
I would be very grateful if someone could help me on this one. Here is the files I am using. (I will spare you the process file).
camel-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="myDb" class="com.mongodb.Mongo">
<constructor-arg index="0">
<bean class="com.mongodb.MongoURI">
<constructor-arg index="0"
value="mongodb://username:password#192.168.3.29:27017/db" />
</bean>
</constructor-arg>
</bean>
<bean id="mongodb" class="org.apache.camel.component.mongodb.MongoDbComponent"></bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="camelRoute" />
</camelContext>
<bean id="camelRoute" class="infotel.camel.project01.CamelRoute" />
Here is my RoutingFile:
#Component
public class CamelRoute extends SpringRouteBuilder {
final Processor myProcessor = new MyProcessor();
final Processor myProcessorMongo = new MyProcessorMongo();
final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
#Override
public void configure() {
xmlJsonFormat.setForceTopLevelObject(true);
from("file:xml_files?noop=true").marshal(xmlJsonFormat).convertBodyTo(String.class).process(myProcessorMongo)
.to("mongodb:myDb?database=test_bignav&collection=doc&operation=insert");
}
}
And finally here is my main:
public class MyMain {
public static void main(String[] args) throws Exception {
ApplicationContext context =
new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
}
}
Thanks a lot.
Edit:
Here is MyProcessorMongo edited to get the error:
public class MyProcessorMongo implements Processor{
public void process(Exchange exchange) throws Exception {
System.out.println("\n file transfered to mongo: "+ exchange.getIn().getHeader("CamelFileName"));
exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).printStackTrace();
}
}
Enable tracing with trace="true":
<camelContext trace="true" xmlns="http://camel.apache.org/schema/spring">
Dirty but quick, to get the error you can add this to you configure() method before your from :
.onException(Exception.class).handled(true).process(new Processor() {
#Override
public void process(Exchange exchange) {
exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).printStackTrace();
}
})
The handled(true) prevents your message from being processed again and again.
thanks for your help I have been able to get the error message.
The problem actually came from mongoDB itself and not camel or code. With the change on users the connection works and I'm able to insert document inside a collection.
Remove the ".process(myProcessorMongo)" from route configuration . Input xml-> json conversion->string conversion -> Mongodb. Above route will work. And you are passing the exchange object to myProcessorMongo but Out message is null so nothing will be inserted into MongoDB . Put exchange.getOut().getBody(); in the myProcessorMongo and print it.If its coming as null u have to get the input message from exchange Obj and set it back it in to Out message property in the exchange Object.

JSON Response with Spring 3 MVC and jQuery

I have some huge trouble receiving JSON from my simple Spring Controller although I checked against many other tutorials and even the official spring blog and could not find any difference, so please help me.
So my dependencies in my project are:
spring-context 3.2.2 RELEASE
spring-web 3.2.2 RELEASE
spring-webmvc 3.2.2 RELEASE
spring-test 3.2.2 RELEASE
junit 4.10
servlet-api 2.5
atmosphere-runtime 1.1.0 RC4
logback-classic 1.0.13
libthrift 0.9.0
jackson-mapper-asl 1.9.12
jackson-core-asl 1.9.12
My Controller is very simple and just generates a random UUID and returns it. It looks as follows:
#Controller
public class SimpleController {
#RequestMapping(value="/new", method=RequestMethod.GET)
public #ResponseBody SimpleResponse new() throws JsonGenerationException, JsonMappingException, IOException {
SimpleResponse sr = new SimpleResponse();
sr.setId(UUID.randomUUID().toString());
return sr;
}
}
The model is just a simple POJO like
public class SimpleResponse {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Configuration is done like this
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<display-name>SimpleTest</display-name>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
and
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="de.tum.ibis.wsc" />
</beans>
So thats the server side. On the client side I have a html page with just one line of jQuery code
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON("http://localhost:8080/Frontend/app/new", function(data) { console.log("it works"); });
});
</script>
</head>
<body>
</body>
</html>
Now according to everything I have read this should work but it does not for me. If I call localhost:8080/Frontend/app/new directly in my browser I get something like this: {"id":"b46b8d67-5614-44ed-90ef-d2da14d260f6"} and Firebug tells me that the response header from the server is
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Server: Jetty(7.6.5.v20120716)
so content-type should be fine. Well if I now run the jquery ajax call I get the error "JSON.parse: unexpected end of data " in jquery.js and I have no cloue why. I hope anybody can help me with that. Thanks!
------ Update ------
Firebug: jQuery error
Firebug: All I get
Firebug: This is what I get if a access the url directly
Try configuring ContentNegotiationManagerFactoryBean in Spring XML config, see Spring docs
Set favorPathExtension to false and update method's #RequestMapping like so
#RequestMapping(value="/new", method=RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
In your AJAX request, you're using
http://localhost:8080/Frontend/app/new
And your servlet declares URL "/new", you should use "/app/new" instead.
#RequestMapping(value="/app/new", method=RequestMethod.GET)