Deleting 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 Deleting a Container Using REST, this Java sample first creates a sample container and then deletes it. For details about 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 your existing authority id and unique id for the sample container. Each container beneath an authority must have unique id.
import java.io.IOException;
import java.net.*;
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;
import java.io.*;
public class DeleteContainerUsingREST
{
private static String UserId;
private static String UserPw;
final private static String authorityId = "<YourExistingAuthorityId>";
final private static String sampleContainerId = "<SampleContainerId>";
final private static String ssdsContentType = "application/x-ssds+xml";
final private static String HttpPostMethod = "POST";
final private static String HttpDeleteMethod = "DELETE";
public static void main(String[] args)
{
UserId = ReadFromConsole("UserName");
UserPw = ReadFromConsole("Password");
String AuthorityUri = String.format(
"https://%s.data.database.windows.net/v1/", authorityId);
try
{
// create a sample container for deletion
String containerUri = CreateContainer(AuthorityUri);
// now delete the container
DeleteContainer(containerUri);
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
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 DeleteContainer(String containerUri)
throws IllegalArgumentException, Exception
{
if (containerUri == null || containerUri.isEmpty())
{
throw new IllegalArgumentException("containerUri");
}
String requestBody = "";
HttpMethod response = IssueRequest(HttpDeleteMethod, containerUri,
requestBody, HttpStatus.SC_OK);
if (response.getStatusCode() != HttpStatus.SC_OK)
{
System.out.println("Container deletion failed");
}
}
private static String CreateContainer(String authorityUri)
throws IllegalArgumentException, Exception
{
// 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;
// Container we want to create
String requestBody = String
.format(ContainerTemplate, sampleContainerId);
HttpMethod response2 = IssueRequest(HttpPostMethod, authorityUri,
requestBody, HttpStatus.SC_CREATED);
// Read the response body.
//byte[] responseBytes = response2.getResponseBody();
// NOTE: Simplifying assumption. Assume all returned content at this
// is string data.
// String responseMsg = new String(responseBytes);
//Header[] responseHeaders = response2.getResponseHeaders("LOCATION");
//for (int index = 0; index < responseHeaders.length; index++)
//{
//containerUri = responseHeaders[index].getValue();
//}
// Read the response body if there is data to read from the stream.
if(response2.getResponseBodyAsStream().available() > 0)
{
ObjectInputStream responseStream = new ObjectInputStream(response2.getResponseBodyAsStream());
String responseMsg = responseStream.readUTF();
System.out.println("Response was: " + responseMsg);
}
containerUri = authorityUri + sampleContainerId;
return containerUri;
}
private static HttpMethod IssueRequest(String method, String url,
String body, int expectedStatus) throws Exception,
IllegalArgumentException
{
if (method == null || method.isEmpty())
{
throw new IllegalArgumentException("method");
}
if (url == null || url.isEmpty())
{
throw new IllegalArgumentException("url");
}
if (method != HttpDeleteMethod) // then we expect body. When deleting
// body is empty
{
if (body == null || body.isEmpty())
{
throw new IllegalArgumentException("body");
}
}
String response = "";
// 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(url);
((PostMethod) requestMethod).setRequestEntity(new StringRequestEntity(body, ssdsContentType, null));
//((PostMethod) requestMethod).setRequestBody(body);
//((PostMethod) requestMethod).setRequestHeader("Content-Type",
//XmlContentType);
} else if (method.equals(HttpDeleteMethod))
{
requestMethod = new DeleteMethod(url);
} else
{
throw new Exception("Unsupported method type provided");
}
URL requestUrl = new URL(url);
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 t`hat 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 ex)
{
System.out.println(ex.getMessage());
}
return requestMethod;
}
}
See Also
Concepts
Deleting a Container Using REST
SDS Data Model Overview (Authorities, Containers, Entities and Flexible Entities)