Share via


Creating a Container 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 a Container Using REST, the following Java sample creates a container in the specified authority. For details about creating 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 your existing authority id and unique id for the new container. Each container beneath an authority must have unique id.

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 CreateContainerUsingREST
{
// Provide you own values for the member variables
 private static String UserId = null;
 private static String UserPw = null;
final private static String authorityId = "<YourExistingAuthorityId>"; 
final private static String sampleContainerId = "<NewContainerId>"; 
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");
String AuthorityUri = String.format(
"https://%s.data.database.windows.net/v1/", authorityId);

CreateContainer(AuthorityUri);
}

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;
}

private static void CreateContainer(String authorityUri)
throws IllegalArgumentException
{
// container template
final String ContainerTemplate = "<s:Container xmlns:s='https://schemas.microsoft.com/sitka/2008/03/'>"
+ "<s:Id>%s</s:Id>" + "</s:Container>";

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

String containerUri = null;
try
{
// Container we want to create
String requestBody = String.format(ContainerTemplate,
sampleContainerId);

HttpMethod response = IssueRequest(HttpPostMethod, authorityUri,
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 authorityUri,
String body, int expectedStatus) throws Exception
{
// Define the HTTP client that we'll use.
HttpClient client = new HttpClient();
HttpMethod requestMethod = null;

// Choose the method we'll be using to issue the request.
if (method.equals(HttpPostMethod))
{
requestMethod = new PostMethod(authorityUri);
((PostMethod) requestMethod).setRequestEntity(new StringRequestEntity(body, ssdsContentType, null));
} else
{
throw new Exception("Unsupported method type provided");
}

URL requestUrl = new URL(authorityUri);

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;
}
}

See Also

Concepts

Creating a Container Using REST
SDS Data Model Overview (Authorities, Containers, Entities and Flexible Entities)