1515import com .alchemyapi .api .parameters .TaxonomyParameters ;
1616import com .alchemyapi .api .parameters .TextParameters ;
1717import org .apache .commons .io .IOUtils ;
18+ import org .apache .commons .lang3 .StringUtils ;
1819import org .apache .log4j .Logger ;
20+ import org .json .JSONObject ;
1921import org .jsoup .Jsoup ;
2022import org .jsoup .nodes .Document ;
2123import org .jsoup .nodes .Element ;
2224import org .jsoup .parser .Parser ;
23- import org .xml .sax .SAXException ;
2425
25- import javax .xml .parsers .ParserConfigurationException ;
26- import javax .xml .xpath .XPathExpressionException ;
2726import java .io .DataOutputStream ;
2827import java .io .IOException ;
2928import java .net .HttpURLConnection ;
3332import static org .apache .commons .lang3 .StringUtils .length ;
3433import static org .apache .commons .lang3 .StringUtils .trimToEmpty ;
3534
35+ /**
36+ * Created by kenny
37+ */
3638public class AlchemyApi {
3739
3840 private static final Logger LOGGER = Logger .getLogger (AlchemyApi .class );
@@ -146,8 +148,7 @@ public Document htmlGetRankedKeywords(final String html, final String url, final
146148 return post ("HTMLGetRankedKeywords" , "html" , params );
147149 }
148150
149- public Document textGetRankedKeywords (final String text ) throws IOException , SAXException ,
150- ParserConfigurationException , XPathExpressionException {
151+ public Document textGetRankedKeywords (final String text ) {
151152 return textGetRankedKeywords (text , new KeywordParameters ());
152153 }
153154
@@ -403,11 +404,11 @@ public Document urlGetRelations(final String url, final RelationParameters param
403404 return get ("URLGetRelations" , "url" , params );
404405 }
405406
406- public Document HTMLGetRelations (final String html , final String url ) {
407- return HTMLGetRelations (html , url , new RelationParameters ());
407+ public Document htmlGetRelations (final String html , final String url ) {
408+ return htmlGetRelations (html , url , new RelationParameters ());
408409 }
409410
410- public Document HTMLGetRelations (final String html , final String url , final RelationParameters params ) {
411+ public Document htmlGetRelations (final String html , final String url , final RelationParameters params ) {
411412 params .setUrl (url );
412413 params .setHtml (html );
413414 return post ("HTMLGetRelations" , "html" , params );
@@ -547,7 +548,7 @@ private Document post(final String callName, final String callPrefix, final Para
547548 }
548549 }
549550
550- // TODO add json handling
551+ // TODO support json, by default
551552 // TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates
552553 private Document doRequest (final HttpURLConnection httpURLConnection , final Parameters parameters ) {
553554 try {
@@ -562,7 +563,8 @@ private Document doRequest(final HttpURLConnection httpURLConnection, final Para
562563 return praseRdf (response , parameters );
563564
564565 case Parameters .OUTPUT_JSON :
565- throw new AlchemyApiException ("Json Response not supported yet" );
566+ // return parseJson(response, parameters);
567+ throw new AlchemyApiException ("Json responses are not currently supported" );
566568
567569 default :
568570 throw new AlchemyApiException ("Unknown output mode, must be one of [xml,rdf,json]" );
@@ -572,6 +574,20 @@ private Document doRequest(final HttpURLConnection httpURLConnection, final Para
572574 }
573575 }
574576
577+ private JSONObject parseJson (final String response , final Parameters parameters ) {
578+ final JSONObject json = new JSONObject (response );
579+ if (json .has ("results" )) {
580+ final JSONObject results = json .getJSONObject ("results" );
581+ if (!StringUtils .equals (results .optString ("status" ), "OK" )) {
582+ if (results .has ("statusInfo" )) {
583+ throw new AlchemyApiException ("Error making API call: " + results .optString ("statusInfo" ));
584+ }
585+ throw new AlchemyApiException ("Error making API call: " + results .optString ("status" ));
586+ }
587+ }
588+ return json ;
589+ }
590+
575591 private Document parseXml (final String response , final Parameters parameters ) {
576592 final Document document = Jsoup .parse (response , parameters .getEncoding (), Parser .xmlParser ());
577593
@@ -586,11 +602,10 @@ private Document parseXml(final String response, final Parameters parameters) {
586602 return document ;
587603 }
588604
589- // TODO investigate rdf format
590605 private Document praseRdf (final String response , final Parameters parameters ) {
591606 final Document document = Jsoup .parse (response , parameters .getEncoding (), Parser .xmlParser ());
592-
593- final Element status = document .select ("RDF > Description > ResultStatus" ).first ();
607+ System . out . println ( "RAW: " + response );
608+ final Element status = document .select ("rdf| RDF > rdf| Description > aapi| ResultStatus" ).first ();
594609 if (status == null || !status .text ().equals ("OK" )) {
595610 throw new AlchemyApiException ("Error making API call: " + status );
596611 }
@@ -601,10 +616,4 @@ private String buildBaseApiUrl() {
601616 return API_URL .replace ("{SUB_DOMAIN}" , configuration .getApiSubDomain ());
602617 }
603618
604- private String parseBaseUrl (final HttpURLConnection httpURLConnection ) {
605- final URL url = httpURLConnection .getURL ();
606- String path = url .getFile ().substring (0 , url .getFile ().lastIndexOf ('/' ));
607- return url .getProtocol () + "://" + url .getHost () + path ;
608- }
609-
610619}
0 commit comments