Share via


Creating an Authority Using REST (Java)

[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]

As described in Creating an Authority Using REST, the following Java sample creates an authority. For information on creating a Java application, see Building and Executing Java Samples.

Note Credentials are required to access the service. To make a working sample, update following code and provide id value for the new authority being created.

import java.io.IOException;
import java.net.URL;
import java.io.*;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class CreateAuthorityUsingREST {
// Provide you own values for the member variables
 private static String UserId;
 private static String UserPw;
final private static String serviceUri = "https://data.database.windows.net/v1/"; 
final private static String authorityId = "finalauth"; 
final private static String SsdsContentType = "application/x-ssds+xml";
final private static String HttpPostMethod = "POST";

public static void main(String[] args) {
UserId = ReadFromConsole("UserName");
UserPw = ReadFromConsole("Password");

CreateAuthority();
}

private static void CreateAuthority()
throws IllegalArgumentException {

final String AuthorityTemplate = "<s:Authority xmlns:s='https://schemas.microsoft.com/sitka/2008/03/'>"
+ "<s:Id>%s</s:Id>" + "</s:Authority>";

if (serviceUri == null || serviceUri.isEmpty()) {
throw new IllegalArgumentException("serviceUri");
}
if (authorityId == null || authorityId.isEmpty()) {
throw new IllegalArgumentException("authorityId");
}

try {
String requestBody = String.format(AuthorityTemplate, authorityId);

HttpMethod response = IssueRequest(HttpPostMethod, serviceUri,
                           requestBody, HttpStatus.SC_CREATED);

// Read the response body if there is data to read from the stream.
if(response.getResponseBodyAsStream().available() > 0)
{
ObjectInputStream responseStream = new ObjectInputStream(response.getResponseBodyAsStream());
String responseMsg = responseStream.readUTF();
System.out.println("Response was: " + responseMsg);
}

} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

private static HttpMethod IssueRequest(String method, String uri,
String body, int expectedStatus) throws Exception {


// Define the HTTP client that we'll use.
HttpClient client = new HttpClient();
HttpMethod requestMethod = null;

if (method.equals(HttpPostMethod)) {
requestMethod = new PostMethod(uri);
((PostMethod) requestMethod).setRequestEntity(new StringRequestEntity(body, SsdsContentType, null));
} else {
throw new Exception("Unsupported method type provided");
}

URL requestUrl = new URL(uri);

try {
// Next, set the credentials we'll be using on the request.
Credentials defaultcreds = new UsernamePasswordCredentials(UserId, UserPw);
client.getState().setCredentials(
new AuthScope(requestUrl.getHost(), 443,
AuthScope.ANY_REALM), defaultcreds);

// Then, state that we do want to do authentication on these
// requests.
requestMethod.setDoAuthentication(true);

// Provide custom retry handler if a retry is necessary
requestMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                         new DefaultHttpMethodRetryHandler(3, false));

// Execute the method.
int statusCode = client.executeMethod(requestMethod);
if (statusCode != expectedStatus) {
System.err.println("Method failed: "
           + requestMethod.getStatusLine());

throw new Exception("Expected status code not returned!");
}

} catch (HttpException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}

return requestMethod;
}

private static String ReadFromConsole(String message)
{
String input = null;
System.out.print(message + ":");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
try
{
input = br1.readLine();
}
catch (IOException ioe) {
System.out.println("IO error reading login name!");
System.exit(1);
}

return input;
}
}

To verify that the authority was created, enter the authority URI in the browser.

https://<authority-id>.data.database.windows.net/v1/

Note

When querying for non-blob entities your browser need to be aware of the application/x-ssds+xml SDS content type. For Internet Explorer this can be done by adding a new key in the registry. For more information, see Guidelines and Limitations.

This action should return the metadata for the authority that you created.

See Also

Concepts

Creating an Authority Using REST
SDS Data Model Overview (Authorities, Containers, Entities and Flexible Entities)
Guidelines and Limitations