1+ package oicclient .webfinger ;
2+
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .google .common .base .Strings ;
5+ import java .net .URI ;
6+ import java .net .URISyntaxException ;
7+ import java .net .URLEncoder ;
8+ import java .util .ArrayList ;
9+ import java .util .Arrays ;
10+ import java .util .HashMap ;
11+ import java .util .List ;
12+ import java .util .Map ;
13+ import oicclient .exceptions .WebFingerError ;
14+ import oicclient .tuples .Tuple ;
15+
16+ public class WebFinger {
17+ public String defaultRelt ;
18+ private JRD jrd ;
19+ private List <Map <String , Object >> events ;
20+ private static final String WF_URL = "https://%s/.well-known/webfinger" ;
21+
22+ public WebFinger (String defaultRelt ) {
23+ this .defaultRelt = defaultRelt ;
24+ this .jrd = null ;
25+ this .events = new ArrayList <>();
26+ }
27+
28+ public String query (String resource , List <String > rel ) throws URISyntaxException , WebFingerError {
29+ resource = new URINormalizer ().normalize (resource );
30+ List <Tuple > queryParamsTuple = new ArrayList <>(Arrays .asList (new Tuple ("resource" , resource )));
31+
32+ if (rel == null ) {
33+ if (!Strings .isNullOrEmpty (this .defaultRelt )) {
34+ queryParamsTuple .add (new Tuple ("rel" , this .defaultRelt ));
35+ }
36+ } else {
37+ for (String index : rel ) {
38+ queryParamsTuple .add (new Tuple ("rel" , index ));
39+ }
40+ }
41+
42+ String host ;
43+ if (resource .startsWith ("http" )) {
44+ URI uri = new URI (resource );
45+ host = uri .getHost ();
46+ int port = uri .getPort ();
47+ if (port != -1 ) {
48+ host += ":" + port ;
49+ }
50+ } else if (resource .startsWith ("acct:" )) {
51+ String [] arr = resource .split ("@" );
52+ host = arr [arr .length - 1 ];
53+ arr = host .replace ("/" , "#" ).replace ("?" , "#" ).split ("#" );
54+ host = arr [0 ];
55+ } else if (resource .startsWith ("device:" )) {
56+ String [] arr = resource .split (":" );
57+ host = arr [1 ];
58+ } else {
59+ throw new WebFingerError ("Unknown schema" );
60+ }
61+
62+ String queryParams = "" ;
63+ for (int i = 0 ; i < queryParamsTuple .size (); i ++) {
64+ queryParams += queryParamsTuple .get (i ).getA () + "=" + queryParamsTuple .get (i ).getB ();
65+ if (i != queryParamsTuple .size () - 1 ) {
66+ queryParams += "&" ;
67+ }
68+ }
69+
70+ return String .format (WF_URL , host ) + "?" + URLEncoder .encode (queryParams );
71+ }
72+
73+ public String query (String resource ) throws URISyntaxException , WebFingerError {
74+ return query (resource , null );
75+ }
76+
77+ public Map <String , Object > httpArgs (JRD jrd ) throws JsonProcessingException {
78+ if (jrd == null ) {
79+ if (this .jrd != null ) {
80+ jrd = this .jrd ;
81+ } else {
82+ return null ;
83+ }
84+ }
85+
86+ Map <String , String > hMap = new HashMap <String , String >() {{
87+ put ("Access-Control-Allow-Origin" , "*" );
88+ put ("Content-Type" , "application/json; charset=UTF-8" );
89+ }};
90+
91+ Map <String , Object > headersAndBody = new HashMap <>();
92+ headersAndBody .put ("headers" , hMap );
93+ headersAndBody .put ("body" , jrd .toJSON ());
94+
95+ return headersAndBody ;
96+ }
97+ }
0 commit comments