7 Replies Latest reply: Feb 15, 2013 5:04 AM by RahulV RSS

    REST WebService Problem..?

    VISHNU_95
      Hi All,

      I want to execute a RQL Query from Java Client program using ATG REST web services.. For that i did the following --

      1. Included REST module in the application MANIFEST.MF file..
      2. set value of allowAccessForUnSecuredRepository to true in the Component */atg/rest/processor/RestSecurityProcessor*
      3. Already there is an user with loginName and password as vishnu.bovilla@rediff.com and vishnu
      4. Now in Eclipse, i created a Client Program as --

      import atg.rest.client.RestClientException;
      import atg.rest.client.RestSession;

      public class RestClientRequest {
           public static void main(String[] args) {
                RestSession session = RestSession.createSession("localhost", 8080, "vishnu.bovilla@rediff.com", "vishnu");
                session.setUseHttpsForLogin(false);
                try {
                     String loginStatus = session.login();
                     if(loginStatus == null || "null".equals(loginStatus)){
                          session = null;
                          System.out.println("Login Failed");
                     }
                     else{
                          System.out.println("Login Successfully");
                     }
                } catch (RestClientException e) {
                     e.printStackTrace();
                }
           }

      }


      But it is throwing error --

      Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
           at com.www.atg.client.RestClientRequest.main(RestClientRequest.java:8)
      Caused by: java.lang.ClassNotFoundException: org.json.JSONException
           at java.net.URLClassLoader$1.run(Unknown Source)
           at java.security.AccessController.doPrivileged(Native Method)
           at java.net.URLClassLoader.findClass(Unknown Source)
           at java.lang.ClassLoader.loadClass(Unknown Source)
           at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
           at java.lang.ClassLoader.loadClass(Unknown Source)
           ... 1 more
        • 1. Re: REST WebService Problem..?
          Nooruddin Shaik
          The rest client is throwing some JSONException and you do not have json jar in your class path.

          So add json jar to the client module class path and it display the exact error.

          Peace
          Shaik
          • 2. Re: REST WebService Problem..?
            VISHNU_95
            hi Shik,

            i tried after including JSON jar files in classpath .. still getting the following the Error when i try get sessionId --


            Exception in thread "main" atg.rest.client.RestClientException: java.io.IOException: Unauthorized Server returned HTTP response code: 401 for URL: http://localhost:8180/rest/bean/atg/userprofiling/ProfileServices/loginUser
                 at atg.rest.client.RestSession.createHttpRequest(RestSession.java:755)
                 at atg.rest.client.RestSession.login(RestSession.java:496)
                 at com.www.atg.client.RestClientRequest.main(RestClientRequest.java:10)
            Caused by: java.io.IOException: Unauthorized Server returned HTTP response code: 401 for URL: http://localhost:8180/rest/bean/atg/userprofiling/ProfileServices/loginUser
                 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
                 at atg.rest.client.RestSession.createHttpRequest(RestSession.java:745)
                 ... 2 more

            What to do. Please suggest..


            Thanks,
            Vishnu
            • 3. Re: REST WebService Problem..?
              Nooruddin Shaik
              This is the exact error.
              It looks the user credentials: vishnu.bovilla@rediff.com/vishnu are not correct/configured.

              If you have set the security in restSecutiryConfiguratoin.xml, check the acl.

              Peace
              Shaik
              • 4. Re: REST WebService Problem..?
                VISHNU_95
                Hi Shik, there was an error while starting the server also as follows --

                2013-02-13 21:01:36,668 ERROR [nucleusNamespace.atg.rest.security.RestSecurityManager] (main)
                atg.rest.RestInitializationException: atg.security.InvalidPersonaException: Profile$login$#vishnu.bovilla@rediff.com#
                     at atg.rest.security.ComponentSecurityConfiguration.create(ComponentSecurityConfiguration.java:102)

                ..
                ..
                Caused by: atg.security.InvalidPersonaException: Profile$login$#vishnu.bovilla@rediff.com#
                     at atg.security.AccessControlListParser.setPersona(AccessControlListParser.java:239)
                     at atg.security.AccessControlListParser.parseAce(AccessControlListParser.java:277)
                ..
                ..
                2013-02-13 21:01:36,782 ERROR [nucleusNamespace.] (main) Unable to start service "/atg/rest/security/RestSecurityManager": atg.nucleus.ServiceException: atg.rest.RestInitializationException: atg.security.InvalidPersonaException: Profile$login$#vishnu.bovilla@rediff.com#
                ..
                ..



                But already there is an user with login as 'vishnu.bovilla@rediff.com'.. Still it throwing the error..
                • 5. Re: REST WebService Problem..?
                  lokivog
                  ATG's REST security implementation is very secure. Make sure you thoroughly read through the REST security documentation and you understand it before going to production. For testing purposes on your dev machine, you can turn off all REST security validation on your components/methods by:

                  <resource component="/com/my/component/to/call" secure="false">
                  <method name="methodtoexecute" secure="false" requiresSessionConfirmation="false">
                  <acl value="EVERYONE:read,write,execute"/>
                  </method>
                  </resource>

                  To invoke a REST GET call, you would invoke
                  http://server/bean/rest/com/my/component/to/call
                  This will return all your getter properties on your components

                  To invoke a REST POST call, you would invoke
                  curl -X POST http://server/bean/rest/com/my/component/to/cal/methodtoexecute
                  This will execute a method

                  Regards, Michael
                  • 6. Re: REST WebService Problem..?
                    karthik chopperla
                    Mark below flag to true instead of false for login purposes. You would not see that error.

                    session.setUseHttpsForLogin(true);

                    -karthik
                    • 7. Re: REST WebService Problem..?
                      RahulV
                      I had done a similar thing. A piece of code is below. See if its helpful:

                      import java.io.IOException;
                      import java.util.HashMap;
                      import java.util.Map;
                      import javax.servlet.http.HttpServlet;

                      import atg.repository.Repository;
                      import atg.repository.RepositoryException;
                      import atg.repository.rql.RqlQuery;
                      import atg.repository.rql.RqlStatement;
                      import atg.rest.client.RestClientException;
                      import atg.rest.client.RestComponentHelper;
                      import atg.rest.client.RestConstants;
                      import atg.rest.client.RestRepositoryHelper;
                      import atg.rest.client.RestResult;
                      import atg.rest.client.RestSession;

                      public class RestClient extends HttpServlet
                      {
                      public void getProducts()
                      {
                      RestSession session = RestSession.createSession("localhost", 8180, "admin", "admin");
                      session.setUseHttpsForLogin(false);
                      System.out.println("Session Id : " + session.getUserId());
                      System.out.println("Session:" + session);
                      Map<String, Object> mapparams = new HashMap<String, Object>();
                      mapparams.put("categoryId", "cat100004");
                      mapparams.put("brandId", "");
                      mapparams.put("start", 0);
                      mapparams.put("howMany", 12);
                      mapparams.put("pageNumber", 1);
                      mapparams.put("priceRange", "Under 10");
                      mapparams.put("atg-rest-depth", "0");
                      mapparams.put("atg-rest-output", "json");
                      System.out.println("*********Before Call**********");
                      try
                      {
                      RestResult resultProdList = RestComponentHelper.executeMethod("/abc/xyz/catalog/<Your Class Name that'll handle the request>", "<The method that'll handle the request>", null, mapparams, session);
                      String strProdList = resultProdList.readInputStream();
                      System.out.println("strProdList " + strProdList);
                      System.out.println("rSession.getSessionId() " + session.getSessionId());
                      }
                      catch (IOException e)
                      {
                      System.out.println("IOException**************" + e.getMessage());
                      }
                      catch (RestClientException restExcep)
                      {
                      System.out.println("RestClientException**************" + restExcep.getMessage());
                      restExcep.printStackTrace();
                      }
                      }
                      public static void main(String[] args)
                           {
                                new RestClient().getProducts();
                           }
                      }

                      Put all the querying item in mapparams map.

                      Regards,
                      Rahul