1717
1818/** Client for the canonical ODP directory. */
1919public final class DirectoryClient {
20+ private static final String METHOD_GET = "GET" ;
2021 private static final int MAXIMUM_BYTES = 524_288 ;
2122 private static final int MAXIMUM_REDIRECTS = 5 ;
2223 private final DirectoryEnvironment selectedEnvironment ;
@@ -47,6 +48,16 @@ public DirectoryEnvironment environment() {
4748 return selectedEnvironment ;
4849 }
4950
51+ public DirectoryModels .SearchResponse search (DirectoryModels .ResourceSearchRequest request ) {
52+ Objects .requireNonNull (request , "request" );
53+ return DirectoryResults .decode (
54+ send (selectedEnvironment .origin ().resolve ("/v1/directory/search" ), "POST" , encode (request )));
55+ }
56+
57+ public DirectoryModels .SearchResponse continueSearch (String next ) {
58+ return DirectoryResults .decode (send (resolveContinuation (next ), METHOD_GET , null ));
59+ }
60+
5061 public DirectoryModels .SearchPage searchServices (DirectoryModels .SearchRequest request ) {
5162 Objects .requireNonNull (request , "request" );
5263 return decodeSearchPage (
@@ -55,10 +66,18 @@ public DirectoryModels.SearchPage searchServices(DirectoryModels.SearchRequest r
5566
5667 public DirectoryModels .SearchPage continueSearchServices (String next ) {
5768 URI uri = resolveContinuation (next );
58- return decodeSearchPage (send (uri , "GET" , null ));
69+ return decodeSearchPage (send (uri , METHOD_GET , null ));
5970 }
6071
6172 public List <String > suggestServices (String prefix , Integer limit ) {
73+ return suggestions ("/v1/services/suggestions" , prefix , limit );
74+ }
75+
76+ public List <String > suggest (String prefix , Integer limit ) {
77+ return suggestions ("/v1/directory/suggestions" , prefix , limit );
78+ }
79+
80+ private List <String > suggestions (String path , String prefix , Integer limit ) {
6281 if (prefix == null || prefix .isBlank () || prefix .length () > 128 ) {
6382 throw new IllegalArgumentException ("prefix must contain from 1 through 128 characters" );
6483 }
@@ -67,7 +86,7 @@ public List<String> suggestServices(String prefix, Integer limit) {
6786 }
6887 String query = "?prefix=" + URLEncoder .encode (prefix , StandardCharsets .UTF_8 )
6988 + (limit == null ? "" : "&limit=" + limit );
70- String json = send (selectedEnvironment .origin ().resolve ("/v1/services/suggestions" + query ), "GET" , null );
89+ String json = send (selectedEnvironment .origin ().resolve (path + query ), METHOD_GET , null );
7190 try {
7291 return OdpJson .read (json , DirectoryModels .Suggestions .class ).items ();
7392 } catch (IllegalArgumentException exception ) {
@@ -111,7 +130,7 @@ private String send(URI uri, String method, String body) {
111130 .orElseThrow (() -> new IllegalStateException ("Directory redirect omitted Location" ));
112131 current = requireDirectoryOrigin (current .resolve (location ));
113132 if (status == 303 || ((status == 301 || status == 302 ) && "POST" .equals (currentMethod ))) {
114- currentMethod = "GET" ;
133+ currentMethod = METHOD_GET ;
115134 hasBody = false ;
116135 }
117136 }
@@ -179,18 +198,22 @@ static DirectoryModels.SearchPage decodeSearchPage(String json) {
179198 throw new IllegalArgumentException ("Directory response is empty" );
180199 }
181200 DirectoryModels .SearchPage page = OdpJson .treeToValue (value , DirectoryModels .SearchPage .class );
182- if (page .facets () != null
183- && page .facets ().trust ().stream ()
184- .anyMatch (facet -> facet .value () == null
185- || !"tap" .equals (facet .value ().name ()))) {
186- throw new IllegalArgumentException ("Directory trust facets are invalid" );
187- }
201+ validateFacets (page .facets ());
188202 return page ;
189203 } catch (IllegalArgumentException exception ) {
190204 throw new IllegalArgumentException ("Directory response is invalid" , exception );
191205 }
192206 }
193207
208+ static void validateFacets (DirectoryModels .Facets facets ) {
209+ if (facets != null
210+ && facets .trust ().stream ()
211+ .anyMatch (facet -> facet .value () == null
212+ || !"tap" .equals (facet .value ().name ()))) {
213+ throw new IllegalArgumentException ("Directory trust facets are invalid" );
214+ }
215+ }
216+
194217 private static void normalizeServiceProtocols (OdpJsonNode value ) {
195218 if (!value .isObject () || value .get ("protocols" ) == null ) {
196219 return ;
0 commit comments