/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2016 ForgeRock AS.
*
* Portions Copyright 2008 Sun Microsystems Inc.
*/
/**
* Construct REST endpoints and call them.
*/
public final class RESTEndpoint {
private enum HTTPMethod {
}
}
/**
* Call a REST endpoint, returning its response.
* @return RESTResponse object containing status and text of returned value.
* @throws IOException
*/
try {
} else {
urlConnect.setDoOutput(true);
}
urlConnect.setUseCaches(false);
}
}
// post data
}
}
// read response
try (BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnect.getInputStream()))) {
}
}
throw ex;
} catch (IOException ex) {
if (urlConnect != null) {
}
}
}
}
return response;
}
/**
* Convert the parameters into a string of ?name=value&othername=othervalue
* @return the parameters as a string
*/
if (!parameters.isEmpty()) {
} else {
}
}
}
}
/**
* ONLY EVER USE THIS FUNCTION FOR DEBUGGING PURPOSES. IT HIDES PASSWORDS. This obviously won't be what you
* want in real life.
*
* @return A string representing the headers of this endpoint, with a clumsy attempt to knock out clear text
* passwords
*/
} else {
}
}
}
/**
* Turn this RESTEndpoint into a string - ONLY for debugging purposes.
* @return a representation of this endpoint.
*/
result.append(postData.length() + " bytes of POST data (hidden as it appears to contain a password)");
} else {
}
}
}
}
/**
* For a little less than total debugging, try this:
* @return The path of the endpoint
*/
return path + paramsToString();
}
/**
* Build a RESTEndpoint
*/
public static class RESTEndpointBuilder {
public RESTEndpointBuilder() {
path = new StringBuilder();
parameters = new LinkedHashMap<>();
headers = new LinkedHashMap<>();
postData = "";
}
/**
* This is where we assemble the path (straightforward in itself) but substitute the realm. For a number of
* URLs we use, the realm is not involved (none of the OIDC calls use it) but for others (like the identity
* endpoint) it is very important. Unfortunately substituting it is painful as we can accidentally change
* <p/>
* path1/{REALM}/path2
* <p/>
* to
* <p/>
* path1//path2
* <p/>
* when the realm is undefined (i.e. it is the root realm), or even worse:
* <p/>
* path1///path2
* <p/>
* when the realm is set to "/".
*
* @return the carefully assembled path
*/
// Trim the realm. Note that in this way if the caller set the realm to "/" (root realm), we trim it
// such that it becomes zero length.
//
}
}
}
}
} else {
}
}
return result;
}
/**
* Add the specified value to the path carefully. We must never end up gluing together two "/" characters
* (one from the end of the previous path and another from the start of the next path).
* @param incoming the value to append to the path.
* @return the rest call builder object for fluency.
*/
return this;
}
}
}
}
return this;
}
/**
* Add the specified realm.
* @param s The realm.
* @return the rest call builder object for fluency.
*/
realm = s;
return this;
}
/**
* Add the specified name/value pair to the list of parameters. The list of parameters is preserved in order,
* even though this technically may not be necessary.
*
* @param name the name of the parameter
* @param value the value of the parameter
* @return the rest call builder object for added fluency
*/
}
return this;
}
/**
* Add the auth index name and auth index value to the list of parameters.
* @return the current rest call builder object
*/
return this;
}
/**
* Add HTTP POST data. No checking is done in the case where we're actually building a GET and the post
* data will still be written.
* @return the current rest call builder object
*/
this.postData = s;
return this;
}
/**
* Set the HTTP method to GET.
* @return the current rest call builder object
*/
return this;
}
/**
* Set the HTTP method to POST
* @return the current rest call builder object
*/
return this;
}
/**
* @param header The header name
* @param value The header value
* @return the current rest call builder object
*/
return this;
}
/**
* Set the API version for this endpoint.
* @param apiVersion The specified API version
* @return the current rest call builder object
*/
return this;
}
/**
* Build the rest endpoint object
* @return the built rest endpoint
*/
return new RESTEndpoint(this);
}
}
/**
* Class to encapsulate the response from REST API.
*/
public static class RESTResponse {
/**
* @return All the text returned by the endpoint, as a list of lines
*/
return content;
}
/**
* Set the content, as a list of lines, returned by the endpoint
* @param content The content, as a list of lines
*/
}
/**
* @return the response code of the endpoint
*/
public int getResponseCode() {
return responseCode;
}
/**
* set the response code of the endpoint
* @param responseCode the response code to set
*/
this.responseCode = responseCode;
}
/**
* @return A string representation of the endpoint's response
*/
}
}
}
}
}