Spring MVC RESTful interface - java

I'm new to spring and I would like to create RESTful interface. I can't get simple sample code working and I can't figure out where is the issue...
The below code triggers error:
Exception reading manifest from http://localhost:8080/myTestApp/test: the manifest may not be valid or the file could not be opened.
Source: System.Deployment
The code:
#RequestMapping(method=RequestMethod.GET, value="/test")
#ResponseBody
public String test(#RequestBody String body){
String testString = test.CreateXml();
return testString;
//return "home";
}
If I first launch the project in server with the below code
#RequestMapping(method=RequestMethod.GET, value="/test")
//#ResponseBody
public String test(#RequestBody String body){
String testString = test.CreateXml();
//return testString;
return "home";
}
Then I uncomment "#ResponseBody" and "return testString" and put comment for "return "home"" line while the server is up and running.
So the code looks precisely like the previous what didn't worked. After the changes to code I press Save and the new code is delivered to server on fly.
After these changes the "test" interface works. If I restart server I again receive error.
I'm using "Spring Tool Suite 3.4.0" and as servers tried "VMware Fabric tc Server Developer Edition v2.9" and "Tomcat v7". With both servers there is the same issue so the cause should be somewhere in spring configuration.
Could anyone please help?
Below I have attached error log
PLATFORM VERSION INFO
Windows : 6.1.7601.65536 (Win32NT)
Common Language Runtime : 4.0.30319.18444
System.Deployment.dll : 4.0.30319.18408 built by: FX451RTMGREL
clr.dll : 4.0.30319.18444 built by: FX451RTMGDR
dfdll.dll : 4.0.30319.18408 built by: FX451RTMGREL
dfshim.dll : 4.0.41209.0 (Main.041209-0000)
SOURCES
Deployment url : http://localhost:8080/myTestApp/test
ERROR SUMMARY
Below is a summary of the errors, details of these errors are listed later in the log.
* Activation of http://localhost:8080/myTestApp/test resulted in exception. Following failure messages were detected:
+ Exception reading manifest from http://localhost:8080/myTestApp/test: the manifest may not be valid or the file could not be opened.
+ Parsing and DOM creation of the manifest resulted in error. Following parsing errors were noticed:
-HRESULT: 0x8007001f
Start line: 0
Start column: 0
Host file:
+ A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F)
COMPONENT STORE TRANSACTION FAILURE SUMMARY
No transaction error was detected.
WARNINGS
There were no warnings during this operation.
OPERATION PROGRESS STATUS
* [3/30/2014 8:21:24 PM] : Activation of http://localhost:8080/myTestApp/test has started.
ERROR DETAILS
Following errors were detected during this operation.
* [3/30/2014 8:21:35 PM] System.Deployment.Application.InvalidDeploymentException (ManifestParse)
- Exception reading manifest from http://localhost:8080/myTestApp/test: the manifest may not be valid or the file could not be opened.
- Source: System.Deployment
- Stack trace:
at System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
at System.Deployment.Application.DownloadManager.DownloadDeploymentManifestDirectBypass(SubscriptionStore subStore, Uri& sourceUri, TempFile& tempFile, SubscriptionState& subState, IDownloadNotification notification, DownloadOptions options, ServerInformation& serverInformation)
at System.Deployment.Application.DownloadManager.DownloadDeploymentManifestBypass(SubscriptionStore subStore, Uri& sourceUri, TempFile& tempFile, SubscriptionState& subState, IDownloadNotification notification, DownloadOptions options)
at System.Deployment.Application.ApplicationActivator.PerformDeploymentActivation(Uri activationUri, Boolean isShortcut, String textualSubId, String deploymentProviderUrlFromExtension, BrowserSettings browserSettings, String& errorPageUrl)
at System.Deployment.Application.ApplicationActivator.ActivateDeploymentWorker(Object state)
--- Inner Exception ---
System.Deployment.Application.InvalidDeploymentException (ManifestParse)
- Parsing and DOM creation of the manifest resulted in error. Following parsing errors were noticed:
-HRESULT: 0x8007001f
Start line: 0
Start column: 0
Host file:
- Source: System.Deployment
- Stack trace:
at System.Deployment.Application.Manifest.AssemblyManifest.LoadCMSFromStream(Stream stream)
at System.Deployment.Application.Manifest.AssemblyManifest..ctor(FileStream fileStream)
at System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
--- Inner Exception ---
System.Runtime.InteropServices.COMException
- A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F)
- Source: System.Deployment
- Stack trace:
at System.Deployment.Internal.Isolation.IsolationInterop.CreateCMSFromXml(Byte[] buffer, UInt32 bufferSize, IManifestParseErrorCallback Callback, Guid& riid)
at System.Deployment.Application.Manifest.AssemblyManifest.LoadCMSFromStream(Stream stream)
COMPONENT STORE TRANSACTION DETAILS
No transaction information is available.
The "Test" is sample code for REST interface. Currently it generates "dummy" xml. Below is generated dummy xml.
<root>
<!--This is a comment-->
<test name="value">My node value</test>
</root>
Below is code for the "test"
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
CreateXml();
}
public static String CreateXml() {
String xmlString = null;
try {
//Creating an empty XML Document
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
//Creating the XML tree
//create the root element and add it to the document
Element root = doc.createElement("root");
doc.appendChild(root);
//create a comment and put it in the root element
Comment comment = doc.createComment("This is a comment");
root.appendChild(comment);
//create child element, add an attribute, and add to root
Element child = doc.createElement("test");
child.setAttribute("name", "value");
root.appendChild(child);
//add a text element to the child
Text text = doc.createTextNode("My node value");
child.appendChild(text);
//Output the XML to a string
//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
xmlString = sw.toString();
//print xml
System.out.println("xml output :\n\n" + xmlString);
} catch (Exception e) {
System.out.println(e);
}
return xmlString;
}
}
I haven't touched context xml and it was auto generated. Below is the servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.mobile.myTestApp" />
</beans:beans>

Related

FileNotFoundError for a properties file in Spring MVC

I'm getting a FileNotFoundException when I try to read a properties file in in my spring controller.
Here is the log that shows when and where it occurs:
java.io.FileNotFoundException: src\main\webapp\WEB-INF\props\configFile.properties (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.webclaims.translator.Translator.readPropertiesFile(Translator.java:65)
at com.webclaims.translator.Translator.createEditablePage(Translator.java:45)
at com.webclaims.translator.controllers.TestController.editableWebpage(TestController.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
The location of my properties file is src/main/webapp/WEB-INF/props/configFile.properties
Here is the controller that has the editableWebpage() that is showed in the log:
#RequestMapping(value = "/edit")
public ModelAndView editableWebpage() throws IOException {
final String source = "http://localhost:8080/translator/test";
final String target = "src/main/webapp/WEB-INF/jsp/editable_webpage.jsp";
final String config = "src/main/webapp/WEB-INF/props/configFile.properties";
Translator t = new Translator();
t.createEditablePage(source, target, config);
return new ModelAndView("editable_webpage");
}
Then we go to my Java class that has the createEditablePage() that is seen in the log above:
#PropertySource(value = "configFile.properties")
public class Translator {
#Autowired
private Properties properties;
private static final Logger LOGGER = Logger.getLogger(Translator.class.getName());
public Translator() {
this.properties = new Properties();
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void createEditablePage(String source, String target, String config) {
File file = new File(target);
try {
Document doc = Jsoup.connect(source).get();
Elements elements = doc.select("*");
readPropertiesFile(config);
for(Element element : elements) {
if(!element.ownText().equals("")) {
String key = getKeyFromPropertiesFile(element.text().toString());
if(!key.equals("")) {
element.addClass(key);
element.attr("contentEditable", "true");
}
}
}
FileUtils.writeStringToFile(file, doc.outerHtml(), "UTF-8");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "IOException has occured", e);
}
}
private void readPropertiesFile(String config) {
try {
File propsFile = new File(config);
FileInputStream inputStream = new FileInputStream(propsFile);
properties.load(inputStream);
inputStream.close();
} catch(FileNotFoundException e) {
LOGGER.log(Level.SEVERE, "FileNotFoundException has occured", e);
} catch(IOException e) {
LOGGER.log(Level.SEVERE, "IOException has occured", e);
}
}
Here is what I have in my web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>translator</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
My spring-servlet.xml contains two bean where one of them is telling spring about the properties file.
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.webclaims.*" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="propertiesFile" class="com.webclaims.translator.Translator">
<property name="properties" value="/WEB-INF/props/configFile.properties"></property>
</bean>
Your locations are mismatched!
In value attribute you specified value="/WEB-INF/props/configFile.properties"
but you mentioned that your file is at "/WEB-INF/config/configFile.properties"
If you still facing issue try to put property file in src folder
try this..
value="src/main/webapp/WEB-INF/props/configFile.properties"
Oups, there are some misunderstanding of key concepts here...
The location of my properties file is src/main/webapp/WEB-INF/props/configFile.properties. No. This is the location of the source of your property file. You have a build pass that will build a .war file, and that war file will be runned by a servlet container. At that moment (run time) all reference to source files have been lost.
The build process will (broadly speaking and assuming a maven build):
compile all java files to class files and put them under /WEB-INF/classes
copy all files from src/main/resources under /WEB-INF/classes where they will be accessible at run time as resources with ClassLoader.getResource or ClassLoader.getResourceAsStream
copy all files from src/main/webapp directly under / and will be accessible at run time as resources with ServletContext.getResource or ServletContext.getResourceAsStream.
Using resources for read-only data is better, because you can transparently use resources that lives in the META-INF/resources folder of a jar deployed inside your application.
Anyway, if you really need to open a file of the web application, the only correct way is to use the method ServletContext.getRealPath to translate a path relative to the root of the application into an absolute path on the server (and no longer in the developper's source)
So to read the property file, you should get it as a ressource from the ServletContex:
in you controller use the relative path and get the InputStream:
#RequestMapping(value = "/edit")
public ModelAndView editableWebpage(ServletRequest request) throws IOException {
final String source = "http://localhost:8080/translator/test";
final String config = "/WEB-INF/props/configFile.properties";
...
InputStream inputStreamConfig = request.getServletContext()
.getResourceAsStream(config);
t.createEditablePage(source, target, inputStreamConfig);
return new ModelAndView("editable_webpage");
}
in the readPropertiesFile method, just load the properties:
properties.load(inputStreamConfig);
But there are other problems is your code:
in Translator class you have both:
#Autowired
private Properties properties;
meaning that properties should be injected, and
public Translator() {
this.properties = new Properties();
}
initializing it in constructor. One is useless.
The Translator class uses Autowired annotation so it should be a Spring bean, but you create it with new. Do not do that but inject it in the controller
In web.xml, you declare the same file for the root context and for the DispatcherServlet context => every bean will be created twice, once in each context. But you did not declare any ContextLoaderListener to bootstrap Spring. It is common to find this:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Sending POST request in ESB mule for API

I try to send the post request to API using ESB mule. Hence i have created flow like below.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="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-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8110" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="api.bonanza.com" port="443" doc:name="HTTP Request Configuration" protocol="HTTPS"/>
<flow name="bonanza_fetchtoken_ceFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/fetchtoken" allowedMethods="GET" doc:name="HTTP"/>
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="X-BONANZLE-API-DEV-NAME" value="t**********I"/>
<add-message-property key="X-BONANZLE-API-CERT-NAME" value="l**********F"/>
</message-properties-transformer>
<set-payload value="fetchTokenRequest" doc:name="Set Payload"/>
<set-property propertyName="Content-Type" value="text/plain" doc:name="Property"/>
<set-property propertyName="Accept" value="application/json" doc:name="Property"/>
<http:request config-ref="HTTP_Request_Configuration" path="/api_requests/secure_request" method="POST" doc:name="HTTP" followRedirects="true" parseResponse="false">
<http:success-status-code-validator values="0..599"/>
</http:request>
</flow>
</mule>
In the API documentation, they have provided the sample code for java to send and receive response and it is working in my local too. The java code snippet is below.
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;
public class FetchToken {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String devId = "t******I";
String certId = "l*******F";
URL url = new URL("https://api.bonanza.com/api_requests/secure_request");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("X-BONANZLE-API-DEV-NAME", devId);
connection.setRequestProperty("X-BONANZLE-API-CERT-NAME", certId);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
String requestName = "fetchTokenRequest";
writer.write(requestName);
writer.flush();
writer.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = in.readLine();
JSONObject jsonResponse = new JSONObject(response);
if (jsonResponse.optString("ack").equals("Success")
&& jsonResponse.optJSONObject("fetchTokenResponse") != null) {
// Success! Now read more keys from the json object
JSONObject fetchTokenJson = jsonResponse.optJSONObject("fetchTokenResponse");
System.out.println("Your token: " + fetchTokenJson.optString("authToken"));
System.out.println("Token expiration time: " + fetchTokenJson.optString("hardExpirationTime"));
System.out.println("Authentication URL: " + fetchTokenJson.optString("authenticationURL"));
}
} catch (Exception e) {
System.out.println(e);
}
}
}
The above java program retuning token without issue. When i use my esb mule i am getting below API error. What are the mistakes i doing while building request. How can i get successful response from API ?
{
"ack": "Failure",
"version": "1.0beta",
"timestamp": "2016-03-09T10:35:16.000Z",
"errorMessage": {
"message": "Cannot determine what type of request you are making. Often this can be the result of data that has not been escaped before being passed to the API. If you are passing data with quotation marks or other special characters, you should translate it to JSON, then escape it, before sending it over the API."
}
}
Solving your problem implies performing debugging steps. I'm going to walk you through them.
Hypothesis: Maybe the receiving service is confused by some extra headers sent by Mule?
Try the following:
Send the Mule request to http://requestb.in/ and look exactly how it is structured.
Then send the same request to api.bonanza.com from Java, it should fail too.
Remove extra stuff bit by bit until it works.
Then come back to Mule and ensure that it creates the same request by tuning the headers it sends, fixing any other differences...

Spring Integration - how to send POST parameters with http outbound-gateway

I'm trying to put together a really simple HTTP POST example using Spring Integration and a http outbound-gateway.
I need to be able to send a HTTP POST message with some POST parameters, as I would with curl:
$ curl -d 'fName=Fred&sName=Bloggs' http://localhost
I can get it working (without the POST parameters) if I send a simple String as the argument to the interface method, but I need to send a pojo, where each property of the pojo becomes a POST parameter.
I have the following SI config:
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<int:gateway id="requestGateway"
service-interface="RequestGateway"
default-request-channel="requestChannel"/>
<int:channel id="requestChannel"/>
<int-http:outbound-gateway request-channel="requestChannel"
url="http://localhost"
http-method="POST"
expected-response-type="java.lang.String"/>
</beans>
My RequestGateway interface looks like this:
public interface RequestGateway {
String echo(Pojo request);
}
My Pojo class looks like this:
public class Pojo {
private String fName;
private String sName;
public Pojo(String fName, String sName) {
this.fName = fName;
this.sName = sName;
}
.... getters and setters
}
And my class to kick it all off looks like this:
public class HttpClientDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/si-email-context.xml");
RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class);
Pojo pojo = new Pojo("Fred", "Bloggs");
String reply = requestGateway.echo(pojo);
System.out.println("Replied with: " + reply);
}
}
When I run the above, I get:
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [Pojo] and content type [application/x-java-serialized-object]
I've googled a lot for this, but cannot find any examples of sending HTTP POST parameters with an outbound-gateway (I can find lots about setting HTTP Headers, but that's not what I'm trying to do here)
The only thing I did find was spring-integration: how to pass post request parameters to http-outbound but it's a slightly different use case as the OP was trying to send a JSON representation of his pojo which I am not, and the answer talks about setting headers, not POST parameters.
Any help with this would be very much appreciated;
Thanks
Nathan
Thanks to the pointers from #jra077 regarding Content-Type, this is how I solved it.
My SI config now looks like this - the important bit was adding the Content-Type header:
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<int:gateway id="requestGateway"
service-interface="RequestGateway"
default-request-channel="requestChannel">
<int:method name="sendConfirmationEmail">
<int:header name="Content-Type" value="application/x-www-form-urlencoded"/>
</int:method>
</int:gateway>
<int:channel id="requestChannel"/>
<int-http:outbound-gateway request-channel="requestChannel"
url="http://localhost"
http-method="POST"
expected-response-type="java.lang.String"/>
</beans>
Then I changed my interface to take a Map as it's argument rather than the pojo:
public interface RequestGateway {
String echo(Map<String, String> request);
}
The pojo itself remains as before; and the class that invokes the service is changed so that it creates a Map and passes it:
public class HttpClientDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/si-email-context.xml");
RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class);
Pojo pojo = new Pojo("Fred", "Bloggs");
Map<String, String> requestMap = new HashMap<String, String>();
requestMap.put("fName", pojo.getFName());
requestMap.put("sName", pojo.getSName());
String reply = requestGateway.echo(requestMap);
System.out.println("Replied with: " + reply);
}
}
I'm sure there are several more elegant ways of transforming the pojo into a Map, but for the time being this answers my question.
To send POST parameters with a http outbound-gateway you need to set the Content-Type to application/x-www-form-urlencoded and you need to pass a Map of key/values pairs.

Input needed for twitter cloud connector in Mule

I have a flow with a custon filter that works(with an echo) but if I try tu use a twitter cloud connector i get this error
ERROR 2015-04-04 19:09:02,451 [[CopyTrabajosd2].trabajosdFlow.stage1.02] org.mule.retry.notifiers.ConnectNotifier: Failed to connect/reconnect: Work Descriptor. Root Exception was: Response code 403 mapped as failure. Message payload is of type: BufferInputStream. Type: class org.mule.module.http.internal.request.ResponseValidatorException
ERROR 2015-04-04 19:09:02,458 [[CopyTrabajosd2].trabajosdFlow.stage1.02] org.mule.exception.CatchMessagingExceptionStrategy:
********************************************************************************
Message : Response code 403 mapped as failure. Message payload is of type: BufferInputStream
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Response code 403 mapped as failure. Message payload is of type: BufferInputStream (org.mule.module.http.internal.request.ResponseValidatorException)
org.mule.module.http.internal.request.SuccessStatusCodeValidator:37 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/http/internal/request/ResponseValidatorException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.module.http.internal.request.ResponseValidatorException: Response code 403 mapped as failure. Message payload is of type: BufferInputStream
at org.mule.module.http.internal.request.SuccessStatusCodeValidator.validate(SuccessStatusCodeValidator.java:37)
at org.mule.module.http.internal.request.DefaultHttpRequester.innerProcess(DefaultHttpRequester.java:202)
at org.mule.module.http.internal.request.DefaultHttpRequester.process(DefaultHttpRequester.java:166)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
I think that the clout connector is not receiving the input type that it needs but I'm not sure ,any help?
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:twitter="http://www.mulesoft.org/schema/mule/twitter" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/smtp http://www.mulesoft.org/schema/mule/smtp/current/mule-smtp.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/twitter http://www.mulesoft.org/schema/mule/twitter/current/mule-twitter.xsd">
<smtp:gmail-connector name="gmail" doc:name="Gmail"/>
<http:request-config name="HTTP_Request_Configuration" host="api.openweathermap.org" port="80" basePath="data/2.5/forecast/daily?q=San Fernando,es&lang=es&units=metric&cnt=2&mode=json" doc:name="HTTP Request Configuration"/>
<spring:beans>
<spring:bean id="transformador" name="Bean" class="com.trabajosd.Transformador"/>
</spring:beans>
<twitter:config name="Twitter__Configuration" accessKey="3006010565-rUz6h4xnNoHO2juxGYQEK5jcM5DvSpxv7P3hv07" accessSecret="KxcqDQEo6JbHHR2S10lFN5luxXLEJauNhiBdZIeY7tUXO" consumerKey="LBxBQUYhbAFb5PeBtR7Jg4Evo" consumerSecret="AdD8lJYRS1OadJsn2BXhEbnhdcUNc1t3uM9pnOGH7eNfE2JxZu" doc:name="Twitter: Configuration"/>
<flow name="trabajosdFlow">
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="10000"/>
<http:request config-ref="HTTP_Request_Configuration" path="/" method="GET" doc:name="HTTP"/>
</poll>
<logger level="INFO" doc:name="Logger"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<json:json-to-object-transformer doc:name="JSON to Object"/>
<custom-filter class="com.trabajosd.Filtro" doc:name="Custom"/>
<twitter:update-status config-ref="Twitter__Configuration" status="Predicción de lluvia para hoy(Prueba Mule)" doc:name="Twitter"/>
<catch-exception-strategy doc:name="Catch Exception Strategy"/>
</flow>
<catch-exception-strategy name="trabajosdCatch_Exception_Strategy">
<logger level="INFO" doc:name="Logger"/>
</catch-exception-strategy>
</mule>
And the custon filter class
package com.trabajosd;
import org.json.JSONArray;
import org.json.JSONObject;
import org.mule.api.MuleMessage;
import org.mule.api.routing.filter.Filter;
public class Filtro implements Filter{
#Override
public boolean accept(MuleMessage message) {
try {
String json=message.getPayloadAsString();
JSONObject obj = new JSONObject(json);
JSONArray lista = obj.getJSONArray("list");
JSONObject elemento0 =lista.getJSONObject(0);
JSONArray clima = elemento0.getJSONArray("weather");
JSONObject prediccion = clima.getJSONObject(0);
return prediccion.getString("main").equalsIgnoreCase("Clouds");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
It seems that there is an issue with WWW-authenticate header header in Mule 3.6.x.
The error usually is "401 response received, but no WWW-Authenticate header was present". Not sure why it is http response 403 in your case.
I tried a basic twitter flow and it works on Mule 3.5 but not 3.6.
The reported issues are:
https://www.mulesoft.org/jira/browse/MULE-8282
https://www.mulesoft.org/jira/browse/MULE-8127
https://www.mulesoft.org/jira/browse/MULE-8249

AOP in spring aspect warning

I start learn AOP with spring framework 3.1.2. But I have trouble. In Beans.xml context file i wrote:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id = "audience" class="com.MySpring.Audience"></bean>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* com.MySpring.Performer.perform(..))"/>
<aop:before pointcut-ref="performance" method="seatDown"/>
<aop:before pointcut-ref="performance" method="turnOffPhone"/>
<aop:after-returning pointcut-ref="performance" method="applauz"/>
<aop:after-throwing pointcut-ref="performance" method="demandRefund"/>
</aop:aspect>
</aop:config>
</beans>
And Java class
public class AOPTest {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(
"Beans.xml");
}
}
// ///
class Audience {
public void seatDown() {
out.println("Seat down");
}
public void turnOffPhone() {
out.println("Turn Off Phone");
}
public void applauz() {
out.println("Aplauz");
}
public void demandRefund() {
out.println("We have money back");
}
}
////
interface Performer{
void perform();
}
But in error console i see:
Description Resource Path Location Type
Error occured processing XML 'class path resource [org/aopalliance/aop/Advice.class] cannot be opened because it does not exist'. See Error Log for more details Beans.xml /AnnotationsWiring/src line 11 Spring Beans Problem
And second error:
Description Resource Path Location Type
Error occured processing XML 'org/springframework/aop/aspectj/AspectJMethodBeforeAdvice'. See Error Log for more details Beans.xml /AOP/src line 12 Spring Beans Problem
I guess you are learning spring using examples from spring in action. In fact, you are missing a com.springsource.org.apoalliance.jar file, which is not included in org.springframework.aop.jar(though i don't know why).
Please download it at: http://sourceforge.net/projects/aopalliance/files/aopalliance/
You have the Caused by: java.lang.ClassNotFoundException: org.aopalliance.aop.Advice because you don't include the jar into your classpath.

Categories