Announcement Announcement Module
Collapse
No announcement yet.
Spring Rest Template Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Rest Template

    Hi,

    I have an application in which i am trying to call a rest web service using spring rest template---
    My application is not a Spring mvc . Business flow is that i run a scheduler which pulls some data periodically and for every input POST an XML to rest web service which only accept a XML .

    We are using public private key for generating signature too.

    MY PROBLEM STATEMENT IS --- I am getting a 401 UNAUTHORIZED exception which using the restTemplate.exchaneg method. I tried calling the same service through Rest client and its works fine.I use the code generated keys/headers/xml in both the case.

    application-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" xmlns="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:drools="http://drools.org/schema/drools-spring"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schem...ng-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schem...curity-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd
    http://www.springframework.org/schema/jms http://www.springframework.org/schem...ng-jms-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.0.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">



    <!-- Enable the Context Annotation Configuration -->
    <context:annotation-config />

    <!-- Scans the classpath of this application for @Controllers to deploy as
    beans -->

    <context:component-scan base-package="com.marketplace.billing.integration.conta ct" />

    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>

    </list>
    </property>
    </bean>


    <bean id="jmsBillContactFactory" class="org.apache.activemq.pool.PooledConnectionFa ctory" destroy-method="stop">
    <property name="connectionFactory">
    <bean class="org.apache.activemq.ActiveMQConnectionFacto ry">
    <property name="brokerURL">
    <value>${BILLING.JMS.BROKER.URL}</value>
    </property>
    <property name="userName">
    <value>${BILLING.JMS.USER}</value>
    </property>
    <property name="password">
    <value>${BILLING.JMS.PASSWORD}</value>
    </property>
    </bean>
    </property>
    </bean>

    <bean id="jmsContactQueueConnectionFactory"
    class="org.springframework.jms.connection.CachingC onnectionFactory" >
    <!--<property name="targetConnectionFactory" ref="jmsConnectionFactory"/> -->
    <property name="targetConnectionFactory" ref="jmsBillContactFactory"/>
    <!--<property name="sessionCacheSize" value="1"/> -->
    </bean>

    <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTrans actionManager">
    <property name="connectionFactory" ref="jmsContactQueueConnectionFactory"/>
    </bean>

    <jms:listener-container concurrency="1-1" transaction-manager="jmsTransactionManager" connection-factory="jmsContactQueueConnectionFactory">
    <jms:listener id="billContactQueueListener" destination="${BILL.CONTACT.QUEUE.NAME}" ref="billJMSContactSubscriber" />
    </jms:listener-container>


    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate ">
    <property name="messageConverters">
    <list>
    <bean id="messageConverter" class="org.springframework.http.converter.xml.Mars hallingHttpMessageConverter">
    <property name="marshaller" ref="jaxb2Marshaller" />
    <property name="unmarshaller" ref="jaxb2Marshaller" />
    <property name="supportedMediaTypes">
    <list>
    <bean class="org.springframework.http.MediaType">
    <constructor-arg index="0" value="application"/>
    <constructor-arg index="1" value="x-www-form-urlencoded"/>
    <constructor-arg index="2" value="UTF-8"/>
    </bean>
    <bean class="org.springframework.http.MediaType">
    <constructor-arg index="0" value="application"/>
    <constructor-arg index="1" value="xml"/>
    <constructor-arg index="2" value="UTF-8"/>
    </bean>
    </list>
    </property>
    </bean>
    <bean class="org.springframework.http.converter.StringHt tpMessageConverter">
    <property name="supportedMediaTypes">
    <list>
    <bean class="org.springframework.http.MediaType">
    <constructor-arg index="0" value="application" />
    <constructor-arg index="1" value="xml" />
    <constructor-arg index="2" value="UTF-8" />
    </bean>
    <bean class="org.springframework.http.MediaType">
    <constructor-arg index="0" value="text" />
    <constructor-arg index="1" value="html" />
    <constructor-arg index="2" value="UTF-8" />
    </bean>
    <bean class="org.springframework.http.MediaType">
    <constructor-arg index="0" value="application"/>
    <constructor-arg index="1" value="json"/>
    <constructor-arg index="2" value="UTF-8"/>
    </bean>
    </list>
    </property>
    </bean>
    </list>
    </property>
    </bean>

    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshalle r">
    <property name="classesToBeBound">
    <list>
    <value>com.marketplace.billing.integration.contact .model.ContactRequest</value>
    <value>com.marketplace.billing.integration.contact .model.AccountContactData</value>
    <value>com.marketplace.billing.integration.contact .model.AccountData</value>
    <value>com.marketplace.billing.integration.contact .model.AccountUserData</value>
    <value>com.marketplace.billing.integration.contact .model.Address</value>
    <value>com.marketplace.billing.integration.contact .model.Addresses</value>
    <value>com.marketplace.billing.integration.contact .model.BillingData</value>
    <value>com.marketplace.billing.integration.contact .model.NotificationAddress</value>
    <value>com.marketplace.billing.integration.contact .model.NotificationAddresses</value>
    <value>com.marketplace.billing.integration.contact .response.vo.ReqResponseVO</value>
    </list>
    </property>
    </bean>

    <!-- Should be defined last! -->
    <mvc:annotation-driven />

    </beans>

    Code for Signature generation --

    public static String getContactApikey(String httpVerb, String publicKey, String privateKey, String absolutePath,
    String relativeURI) {
    String computedSignature = null;
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    String timeStamp = sdf.format(new Date());
    System.out.println(timeStamp);
    String sigSpecGen = publicKey + "$" + httpVerb
    + "$" + absolutePath + "$"+ relativeURI
    + "$" + timeStamp;
    SecretKeySpec sigSpec = new SecretKeySpec(privateKey.getBytes(), "*****");

    try {
    Mac mac = Mac.getInstance("****");
    mac.init(sigSpec);
    byte[] rawHmac = mac.doFinal(sigSpecGen.getBytes());
    String localSignature = new String(Base64.encodeBase64(rawHmac)).trim();
    computedSignature = publicKey + ":" + localSignature;
    } catch (Exception e) {
    e.printStackTrace();
    }

    return computedSignature;
    }

    Generating Header as --

    // final KeyGenerator keyGen = new KeyGenerator();
    String signature;
    final HttpHeaders header = new HttpHeaders();

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    String timeStamp = sdf.format(new Date());
    signature = keyAuthorize.authenticateUser(publicKey, httpMethod, relativeURI, absPath, privateKey,
    timeStamp + "");


    header.setContentType("application/xml");
    header.setAccept(new ArrayList<MediaType>(Arrays.asList(MediaType.APPLI CATION_XML)));

    header.add("api-key", publicKey + ":" + signature);

    header.add"x-mp-date",timeStamp);

    }


    Code for calling the Rest service ---

    HttpEntity<Object> httpEntity = new HttpEntity<Object>(obj, httpHeader);
    ResponseEntity<Object> res = restTemplate.exchange(finalUrl, HttpMethod.POST, httpEntity, Object.class);

  • #2
    This isn't a question to Spring WS. Please ask your question on the StackOverflow

    Comment

    Working...
    X