Friday, May 19, 2017

Java PayPal API integration through proxy server

Java PayPal API integration through proxy server

Getting the below exception while integrating PayPal API with Java, based on the analysis the direct communication to the API is not enabled from the server and the communication should be directed via Proxy server.

09:10:50.525 [main] ERROR com.paypal.base.HttpConnection -  Retry  No : 1...
09:11:54.577 [main] ERROR com.paypal.base.HttpConnection - Caught exception while handling error response java.net.ConnectException: Connection timed out
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_67]

Setting the proxy server in the API:

 HashMap<String,String> config=new HashMap<String,String>();
config.put("http.UseProxy", "true");
config.put("http.ProxyPort", "80");
config.put("http.ProxyHost", "proxy.server.com");
config.put("http.ProxyUserName", null);
config.put("http.ProxyPassword", null);

APIContext context = new APIContext(clientId, clientSecret, "sandbox",config);

Change the values of  ProxyHost,  ProxyPort, ProxyUserName and ProxyPassword accordingly.

The communication will be success after this.


Tuesday, September 29, 2015

org.apache.sling.commons.json.JSONException: Misplaced array - JSONWriter

org.apache.sling.commons.json.JSONException: Misplaced array - JSONWriter

The Misplaced array exception is thrown,While we are trying to create the JSON Array of objects from the servlet using - org.apache.sling.commons.json.io.JSONWriter.

Code:-

JSONWriter writer=new JSONWriter(response.getWriter());
try {
writer.object();
writer.key("authorName").value("test");
writer.key("biography").value("test");
writer.key("image").value("test");
writer.array();
writer.object();
writer.key("authorName").value("test");
writer.key("biography").value("test");
writer.endObject();
writer.endArray();
writer.endObject();
} catch (JSONException e) {

}

The root cause of the exception is not providing the key for initialized array(there will be different scenarios we will receive this exception)

To fix the issue as shown below in the code, add the key before starting the array.

JSONWriter writer=new JSONWriter(response.getWriter());
try {
writer.object();
writer.key("authorName").value("test");
writer.key("biography").value("test");
writer.key("image").value("test");
writer.key("list");
writer.array();
writer.object();
writer.key("authorName").value("test");
writer.key("biography").value("test");
writer.endObject();
writer.endArray();
writer.endObject();
} catch (JSONException e) {

}  

O/P

{"authorName":"test","biography":"test","image":"test","list":[{"authorName":"test","biography":"test"}]}       



Thursday, September 17, 2015

a:ActionNotSupported: The message with Action '"' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher.

a:ActionNotSupported: The message with Action '"' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher.

Getting the below error while invoking the SOAP service via WebServiceTemplate.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</faultcode>
<faultstring xml:lang="en-US">The message with Action '"' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>

The root cause of the issue is the SOAPAction is not set in the request header while invoking the service.

To fix the issue set the SOAPAction specified in the WSDL binding while invoking the service.

  <binding name="binding" type="service">
    <binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="process">
      <operation soapAction="process" style="document"/>
      <input>
        <body namespace="request" use="literal"/>
      </input>
      <output>
        <body namespace="response" use="literal"/>
      </output>
    </operation>
  </binding>

webServiceTemplate.marshalSendAndReceive(url,request, new WebServiceMessageCallback() {

    public void doWithMessage(WebServiceMessage message) {
        ((SoapMessage)message).setSoapAction("process");
    }
});


Sunday, May 31, 2015

org.springframework.a op.framework.AopConfigException: Could not generate CGLIB subclass of class [class net.sf.ehcache.CacheManager]

org.springframework.a op.framework.AopConfigException: Could not generate CGLIB subclass of class [class net.sf.ehcache.CacheManager]

While using Ehcache with spring, spring context initialization fails with the below exception

Caused by: org.springframework.beans.factory.BeanCreationException: Error creati
ng bean with name 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceI
mpl#0': Cannot resolve reference to bean 'ehcacheManager' while setting bean pro
perty 'cacheManager'; nested exception is org.springframework.beans.factory.Bean
CreationException: Error creating bean with name 'ehcacheManager': Post-processi
ng of the FactoryBean's object failed; nested exception is org.springframework.a
op.framework.AopConfigException: Could not generate CGLIB subclass of class [cla
ss net.sf.ehcache.CacheManager]: Common causes of this problem include using a f
inal class or a non-visible class; nested exception is org.springframework.cglib
.core.CodeGenerationException: net.sf.ehcache.CacheException-->Another unnamed C
acheManager already exists in the same VM. Please provide unique names for each
CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same Cac
heManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stre
am=java.io.BufferedInputStream@128647a]

Configuration Details:

<?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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven cache-manager="cacheManager"/>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="configLocation" value="classpath:config/ehcache/ehcache.xml" />
  <property name="shared" value="true" />
</bean>
</beans>



Friday, May 8, 2015

Removing the Cookie is not working in IE10 and IE11

Removing the Cookie is not working in IE10 and IE11

While trying to remove the cookies in IE10 and IE11 through Java, the cookies are not getting removed in the browser.

The code snippet used to remove the cookie:


Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
String cookieName = "SampleCookieName";
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName())) {
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}

The root cause of  the issue is IE10 and IE11 expecting the value for the attribute Expires as 0 not for Max-Age.

Fix for the issue - Use the below code to remove the cookies

Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
String cookieName = "SampleCookie";
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName())) {
StringBuilder cookieString = new StringBuilder(cookieHeaderName + "=" + "" + "; ");

cookieString.append("Expires=0" + "; ");
cookieString.append("Version=0; ");
cookieString.append("Path=/; ");
cookieString.append("Max-Age=0" + "; ");
//cookieString.append("HttpOnly");

response.addHeader("Set-Cookie", cookieString.toString());
}
}
}

Refer the following blog to set cookies in IE10 and IE11 - cookie-values-are-not-setting-in-ie10-and-ie11