0

How can I write a condition in java for a webservice request, In my webservice request I am passing an element that has SpeedInfo. In SpeedInfo element there is two sub elements Bandwidth and AccessSpeed.

This is my Request In SoapUI:

 <sch:SpeedInfo>
    <ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">4000000</ns:Bandwidth>
    <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">4000000</ns:AccessSpeed>
 </sch:SpeedInfo>

My Java condition:

if (request.getSpeedInfo() == null || (request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null)){
    throw new Exception(" SpeedInfo Bandwidth must be passed in the request ");
}

I need my condition to check for 3 scenarios:

 1. if <speedInfo> itself is not present

 2. <sch:SpeedInfo> is present, but bandwidth not present:

  <sch:SpeedInfo>
      <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">40000000</ns:AccessSpeed>
  </sch:SpeedInfo>

 3. Bandwidth is present but no value  
   <sch:SpeedInfo>
      <ns:Bandwidth xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1"></ns:Bandwidth>
      <ns:AccessSpeed xmlns:ns="http://lpp.att.com/logical/classofservice/schema/v1">40000000</ns:AccessSpeed>
   </sch:SpeedInfo>

error I am getting:

2016-02-02 09:45:48,349  WARN http-bio-8080-exec-3--[c0a8381152a2a943f51] c0a8381152a2a943f51 fw.ws.MessageInInterceptor:49 - Content input stream is NOT null, nothing to log in handleFault()?
2016-02-02 09:45:48,351  WARN http-bio-8080-exec-3--[c0a8381152a2a943f51] c0a8381152a2a943f51 cxf.phase.PhaseInterceptorChain:452 - Interceptor for {http://lpp.att.com/logical/v1}LogicalService#{http://lpp.att.com/logical/v1}classOfServiceAllocation has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Unmarshalling Error: cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'. 
at    org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:908)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:712)
yesco1
  • 371
  • 2
  • 7
  • 22
  • 2
    `request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null` doesn't make much sense. It is like asking `if (foo == null && foo.bar() == null)`. Now if `foo` will be null, then your second condition will be executed which means that `foo.bar()` will be like `null.bar()` but since `null` doesn't have any members you will get NPE. – Pshemo Feb 01 '16 at 23:16

2 Answers2

4
if (request.getSpeedInfo() == null || (request.getSpeedInfo() == null && request.getSpeedInfo().getBandwidth() == null)){

It is pointless to test getSpeedInfo() for null twice. Surely that is obvious? And this doesn't test the case where getBandwidth() alone is null.

To your requirement:

  1. If <speedInfo> itself is not present
request.getSpeedInfo() == null
  1. <sch:SpeedInfo> is present, but bandwidth not present:
|| request.getSpeedInfo().getBandwidth() == null
  1. Bandwidth is present but no value
|| request.getSpeedInfo().getBandwidth().isEmpty() // or whatever API call is appropriate to the datatype

Putting it all together:

if (request.getSpeedInfo() == null
||
request.getSpeedInfo().getBandwidth() == null
||
request.getSpeedInfo().getBandwidth().isEmpty())
{
    // request is invalid
}
else
    // request is valid ...
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    So use whatever API the class has to test for emptiness. As you didn't tell us what class the bandwidth is held in, I took a guess that it was a `String.` It would work for a `Collection` too. – user207421 Feb 02 '16 at 00:21
  • Its an interger type `public Integer getBandwidth() { return bandwidth; }` – yesco1 Feb 02 '16 at 15:41
  • since its an Integer it cannot be empty, xsd automatically throws an error if it's empty. The 3rd condition is not needed. – yesco1 Feb 02 '16 at 20:13
  • If it's an Integer, the part of your question about 'no value' is meaningless. – user207421 Feb 02 '16 at 21:01
0
SpeedInfo speedInfo = request.getSpeedInfo();
if (speedInfo == null ||                // short circuit here means
    speedInfo.getBandwidth() == null || // that here speedInfo is not null
    speedInfo.getBandwidth().isEmpty()) { // Assuming getBandwidth() is a String or similar

        throw new Exception(" SpeedInfo Bandwidth must be passed in the request ");
}
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • 1
    `.equas("")` since there is no such method, please don't "fix" it to `.equals("")` and use `.isEmpty()` instead. `.equals("")` and `.length() == 0` always looks a bit weird. – Tom Feb 01 '16 at 23:15
  • @Tom Thanks. http://stackoverflow.com/questions/3321526/should-i-use-string-isempty-or-equalsstring – Stewart Feb 01 '16 at 23:17
  • ..getBandwidth() is an integer it won't work using isEmpty() also .length() == 0 did not work either – yesco1 Feb 01 '16 at 23:30
  • 1
    Then please explain what you mean by "Bandwidth is present but no value" – Stewart Feb 02 '16 at 00:15
  • @Steward its in the conditions at the top `` is empty – yesco1 Feb 02 '16 at 15:40
  • 1
    Does that not just give you a null object? I mean, if the type is `java.lang.Integer`, how can you have an "empty" Integer? – Stewart Feb 02 '16 at 18:00