Deleting an Entity 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 an Entity Using REST, the following Java sample deletes the specified entity. 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, container id and id for the entity you wish to delete.
import java.io.IOException;
import java.net.*;
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 DeleteEntityUsingREST
{
// Provide you own values for the member variables
final private static String HttpDeleteMethod = "DELETE";
private static String UserId;
private static String UserPw;
final private static String authorityId = "<YourExistingAuthorityId>";
final private static String containerId = "<YourExistingContainerId>";
final private static String entityId = "<EntityIdToDelete>";
final private static String HttpPostMethod = "POST";
public static void main(String[] args)
{
UserId = ReadFromConsole("UserName");
UserPw = ReadFromConsole("Password");
// URI of the entity we want to delete
String bookUri = String.format(
"https://%s.data.database.windows.net/v1/%s/%s", authorityId,
containerId, entityId);
try
{
String requestBody = "";
HttpMethod response = IssueRequest(HttpDeleteMethod, bookUri,
requestBody, HttpStatus.SC_OK);
if (response.getStatusCode() != HttpStatus.SC_OK)
{
throw new IllegalArgumentException("Entity deletion failed");
}
} 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 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) // when delete, there is no body.
// Otherwise, we expect body
{
if (body == null || body.isEmpty())
{
throw new IllegalArgumentException("body");
}
}
// 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, null, null));
} 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 an Entity Using REST
SDS Data Model Overview (Authorities, Containers, Entities and Flexible Entities)