diff --git a/project/common.scala b/project/common.scala index 7eebf35b..e7da3cb0 100644 --- a/project/common.scala +++ b/project/common.scala @@ -90,7 +90,8 @@ object Config { Dependencies.mockito, Dependencies.scalatest, Dependencies.scalacheck, - Dependencies.junit + Dependencies.junit, + Dependencies.jsoup ) ) @@ -125,6 +126,7 @@ object Version { val jodaConvert = "1.8.1" val jodaTime = "2.9.3" val base64 = "2.3.9" + val jsoup = "1.17.2" } object Dependencies { @@ -138,6 +140,7 @@ object Dependencies { lazy val mockito = "org.mockito" % "mockito-all" % Version.mockito % "test" lazy val scalacheck = "org.scalacheck" %% "scalacheck" % Version.scalacheck % "test" lazy val junit = "com.novocode" % "junit-interface" % Version.junit % "test" + lazy val jsoup = "org.jsoup" % "jsoup" % Version.jsoup } diff --git a/src/main/java/com/solidfire/core/client/ServiceBase.java b/src/main/java/com/solidfire/core/client/ServiceBase.java index 2532e92d..5b1a542c 100644 --- a/src/main/java/com/solidfire/core/client/ServiceBase.java +++ b/src/main/java/com/solidfire/core/client/ServiceBase.java @@ -32,8 +32,10 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; -import java.util.regex.Matcher; -import java.util.regex.Pattern; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; import static java.lang.String.format; @@ -206,14 +208,14 @@ protected TResult decodeResponse(String response, Class resul return result; } catch (ClassCastException e) { - final Pattern pattern = Pattern.compile("

(.*?)

"); - final Matcher matcher = pattern.matcher(response); - if (matcher.find()) { - throw new ApiServerException("Not Found", "404", matcher.group(1)); + Document parsedResponse = Jsoup.parse(response); + Element pTag = parsedResponse.selectFirst("p"); + if(pTag != null) { + throw new ApiServerException("Not Found", "404", pTag.text()); } // Removes the html tags from the response. - response.replaceAll("<.*?>", ""); - throw new ApiException(format("There was a problem parsing the response from the server. ( response=%s )", response), e); + String responseText = parsedResponse.text(); + throw new ApiException(format("There was a problem parsing the response from the server. ( response=%s )", responseText), e); } catch (NullPointerException | JsonParseException e) { log.debug(response); throw new ApiException(format("There was a problem parsing the response from the server. ( response=%s )", response), e);