10 Replies Latest reply on Jul 26, 2014 1:03 PM by rareddy

    Accessing secured ODATA service in Java

    teiiduser

      Hello,

       

      We have an ODATA service in place and I am trying to write a consumer in Java. So here is the code so far:

       

      public class TestReadWrite{

        public static final String endpoint="http://localhost:8080/mydataserver/dsl.svc/";

        public static void main(String[] args) {

        TestReadWrite example = new TestReadWrite();

           example.run(args);

        }

       

        private void run(String[] args) {

        ODataConsumer c = ODataJerseyConsumer.create(endpoint);

        // list all products

          for (OEntity product : c.getEntities("Products").execute()) {

            reportEntity("Products: " + product.getProperty("name").getValue(), product);

          }

        }

       

      }

       

      This is a secured ODATA service so I get the error: Exception in thread "main" java.lang.RuntimeException: Expected status OK, found Unauthorized. Server response:

       

      How do I pass the username/password to this service?I tried searching but did not find a decent example. Can anyone help with a code snippet or any clear documentation on the same?

       

      Thank you!

        • 1. Re: Accessing secured ODATA service in Java
          rareddy

          This seems like good question to framework that supplied ODataJerseyConsumer class, in this case I guess it is OData4J.

          • 2. Re: Accessing secured ODATA service in Java
            teiiduser

            Yes. OData4j

            • 3. Re: Accessing secured ODATA service in Java
              shawkins

              See https://searchcode.com/codesearch/view/9368038/

               

              //ODataConsumer c = ODataJerseyConsumer.create(endpoint);

              OClientBehavior basicAuth = OClientBehaviors.basicAuth("user", "password");

              ODataConsumer c = ODataJerseyConsumer.newBuilder(endpoint).setClientBehaviors(basicAuth).build();

              • 4. Re: Accessing secured ODATA service in Java
                teiiduser

                What could be the reason for this runtime exception. Doesnt give much information:

                 

                Exception in thread "main" java.lang.RuntimeException: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog

                at [row,col {unknown-source}]: [1,0]

                  at org.odata4j.core.Throwables.propagate(Throwables.java:11)

                  at org.odata4j.stax2.staximpl.StaxXMLFactoryProvider2$StaxXMLEventReader2.nextEvent(StaxXMLFactoryProvider2.java:113)

                  at org.odata4j.format.xml.EdmxFormatParser.parseMetadata(EdmxFormatParser.java:55)

                  at org.odata4j.consumer.AbstractODataClient.getMetadata(AbstractODataClient.java:44)

                  at org.odata4j.consumer.AbstractODataConsumer$CachedEdmDataServices.refreshDelegate(AbstractODataConsumer.java:212)

                  at org.odata4j.consumer.AbstractODataConsumer$CachedEdmDataServices.getDelegate(AbstractODataConsumer.java:205)

                  at org.odata4j.internal.EdmDataServicesDecorator.findEdmEntitySet(EdmDataServicesDecorator.java:46)

                  at org.odata4j.consumer.AbstractODataConsumer$CachedEdmDataServices.findEdmEntitySet(AbstractODataConsumer.java:221)

                  at org.odata4j.consumer.AbstractODataConsumer.getFeedCustomizationMapping(AbstractODataConsumer.java:235)

                  at org.odata4j.consumer.AbstractODataConsumer.getEntity(AbstractODataConsumer.java:99)

                  at org.odata4j.consumer.AbstractODataConsumer.getEntity(AbstractODataConsumer.java:91)

                  at org.odata4j.consumer.AbstractODataConsumer.getEntity(AbstractODataConsumer.java:83)

                  at com.lgc.test.TestReadWrite.run(TestReadWrite.java:36)

                  at com.lgc.test.TestReadWrite.main(TestReadWrite.java:24)

                Caused by: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog

                at [row,col {unknown-source}]: [1,0]

                  at com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:677)

                  at com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2119)

                  at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2025)

                  at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1117)

                  at org.codehaus.stax2.ri.Stax2EventReaderImpl.nextEvent(Stax2EventReaderImpl.java:255)

                  at org.odata4j.stax2.staximpl.StaxXMLFactoryProvider2$StaxXMLEventReader2.nextEvent(StaxXMLFactoryProvider2.java:111)

                  ... 12 more

                • 5. Re: Accessing secured ODATA service in Java
                  shawkins

                  If the server is returning a non-xml formatted 401 / unauthorized response, then you could see an exception like that.

                  • 6. Re: Accessing secured ODATA service in Java
                    teiiduser

                    How can I check that extra log information? In the Java console I see only the above error.

                     

                    Also my service actually needs an extra parameter.. so when I access my service I have to put this in the browser:

                     

                    http://localhost/mydataserver/dsl.svc/mymodel/1/mymodel-ds?param=value

                     

                     

                    but the endpoint in my java program is http://localhost/mydataserver/dsl.svc/mymodel/1/mymodel-ds

                    Would that be causing this problem? How do I pass this parameter?

                    Also would it matter if the service is using a different type of authentication, for eg: forms based?

                    • 7. Re: Accessing secured ODATA service in Java
                      shawkins

                      > How can I check that extra log information?

                       

                      That would be the server log.

                       

                      > Would that be causing this problem? How do I pass this parameter?

                       

                      Seems like that would just be part of your endpoint, but if you are thinking something else you should check the jersey docs.

                       

                      > Also would it matter if the service is using a different type of authentication, for eg: forms based?

                       

                      Are you talking about a Teiid odata service?

                      • 8. Re: Accessing secured ODATA service in Java
                        teiiduser

                        If you mean the JBOSS server log, it does not show anything regarding this error.

                        • 9. Re: Accessing secured ODATA service in Java
                          teiiduser

                          Does anyone know the syntax to do form based authentication in java for ODATA service?

                          • 10. Re: Accessing secured ODATA service in Java
                            rareddy

                            It would be specific to that form as what that form submission target is, and typically you need to execute a POST with form parameter values against the endpoint. As far as Teiid OData is concerned we only support BasicAuth right now OOB.