15
15
import com .alchemyapi .api .parameters .TaxonomyParameters ;
16
16
import com .alchemyapi .api .parameters .TextParameters ;
17
17
import org .apache .commons .io .IOUtils ;
18
+ import org .apache .commons .lang3 .StringUtils ;
18
19
import org .apache .log4j .Logger ;
20
+ import org .json .JSONObject ;
19
21
import org .jsoup .Jsoup ;
20
22
import org .jsoup .nodes .Document ;
21
23
import org .jsoup .nodes .Element ;
22
24
import org .jsoup .parser .Parser ;
23
- import org .xml .sax .SAXException ;
24
25
25
- import javax .xml .parsers .ParserConfigurationException ;
26
- import javax .xml .xpath .XPathExpressionException ;
27
26
import java .io .DataOutputStream ;
28
27
import java .io .IOException ;
29
28
import java .net .HttpURLConnection ;
33
32
import static org .apache .commons .lang3 .StringUtils .length ;
34
33
import static org .apache .commons .lang3 .StringUtils .trimToEmpty ;
35
34
35
+ /**
36
+ * Created by kenny
37
+ */
36
38
public class AlchemyApi {
37
39
38
40
private static final Logger LOGGER = Logger .getLogger (AlchemyApi .class );
@@ -146,8 +148,7 @@ public Document htmlGetRankedKeywords(final String html, final String url, final
146
148
return post ("HTMLGetRankedKeywords" , "html" , params );
147
149
}
148
150
149
- public Document textGetRankedKeywords (final String text ) throws IOException , SAXException ,
150
- ParserConfigurationException , XPathExpressionException {
151
+ public Document textGetRankedKeywords (final String text ) {
151
152
return textGetRankedKeywords (text , new KeywordParameters ());
152
153
}
153
154
@@ -403,11 +404,11 @@ public Document urlGetRelations(final String url, final RelationParameters param
403
404
return get ("URLGetRelations" , "url" , params );
404
405
}
405
406
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 ());
408
409
}
409
410
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 ) {
411
412
params .setUrl (url );
412
413
params .setHtml (html );
413
414
return post ("HTMLGetRelations" , "html" , params );
@@ -547,7 +548,7 @@ private Document post(final String callName, final String callPrefix, final Para
547
548
}
548
549
}
549
550
550
- // TODO add json handling
551
+ // TODO support json, by default
551
552
// TODO return pojo with parsed field, but allow a "raw" xml/json getter to protect against api updates
552
553
private Document doRequest (final HttpURLConnection httpURLConnection , final Parameters parameters ) {
553
554
try {
@@ -562,7 +563,8 @@ private Document doRequest(final HttpURLConnection httpURLConnection, final Para
562
563
return praseRdf (response , parameters );
563
564
564
565
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" );
566
568
567
569
default :
568
570
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
572
574
}
573
575
}
574
576
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
+
575
591
private Document parseXml (final String response , final Parameters parameters ) {
576
592
final Document document = Jsoup .parse (response , parameters .getEncoding (), Parser .xmlParser ());
577
593
@@ -586,11 +602,10 @@ private Document parseXml(final String response, final Parameters parameters) {
586
602
return document ;
587
603
}
588
604
589
- // TODO investigate rdf format
590
605
private Document praseRdf (final String response , final Parameters parameters ) {
591
606
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 ();
594
609
if (status == null || !status .text ().equals ("OK" )) {
595
610
throw new AlchemyApiException ("Error making API call: " + status );
596
611
}
@@ -601,10 +616,4 @@ private String buildBaseApiUrl() {
601
616
return API_URL .replace ("{SUB_DOMAIN}" , configuration .getApiSubDomain ());
602
617
}
603
618
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
-
610
619
}
0 commit comments