Skip to content

Commit 1fb920d

Browse files
author
Lowell Vaughn
committed
Edge purge for CDN
1 parent e36c58a commit 1fb920d

File tree

4 files changed

+155
-2
lines changed

4 files changed

+155
-2
lines changed

src/main/java/com/rackspacecloud/client/cloudfiles/FilesClient.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,133 @@ else if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
16071607
throw new FilesAuthorizationException("You must be logged in", null, null);
16081608
}
16091609
}
1610+
1611+
/**
1612+
* Purges all items from a given container from the CDN
1613+
*
1614+
* @param container The name of the container
1615+
* @param emailAddresses An optional comma separated list of email addresses to be notified when the purge is complete.
1616+
* <code>null</code> if desired.
1617+
* @throws IOException Error talking to the cdn management server
1618+
* @throws HttpException Error with HTTP
1619+
* @throws FilesAuthorizationException Log in was not successful, or account is suspended
1620+
* @throws FilesException Other error
1621+
*/
1622+
public void purgeCDNContainer(String container, String emailAddresses) throws IOException, HttpException, FilesAuthorizationException, FilesException {
1623+
if (! isLoggedin) {
1624+
throw new FilesAuthorizationException("You must be logged in", null, null);
1625+
}
1626+
if (!isValidContainerName(container)) {
1627+
throw new FilesInvalidNameException(container);
1628+
}
1629+
HttpDelete method = null;
1630+
try {
1631+
String deleteUri = cdnManagementURL + "/" + sanitizeForURI(container);
1632+
method = new HttpDelete(deleteUri);
1633+
method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
1634+
method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken);
1635+
if (emailAddresses != null) {
1636+
method.setHeader(FilesConstants.X_PURGE_EMAIL, emailAddresses);
1637+
}
1638+
1639+
FilesResponse response = new FilesResponse(client.execute(method));
1640+
1641+
if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
1642+
method.abort();
1643+
if(login()) {
1644+
method = new HttpDelete(deleteUri);
1645+
method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
1646+
method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken);
1647+
if (emailAddresses != null) {
1648+
method.setHeader(FilesConstants.X_PURGE_EMAIL, emailAddresses);
1649+
}
1650+
response = new FilesResponse(client.execute(method));
1651+
}
1652+
else {
1653+
throw new FilesAuthorizationException("Re-login failed", response.getResponseHeaders(), response.getStatusLine());
1654+
}
1655+
}
1656+
1657+
if (response.getStatusCode() == HttpStatus.SC_NO_CONTENT)
1658+
{
1659+
return;
1660+
}
1661+
else if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
1662+
throw new FilesAuthorizationException("User not Authorized!",response.getResponseHeaders(), response.getStatusLine());
1663+
}
1664+
else {
1665+
throw new FilesException("Unexpected server response",response.getResponseHeaders(), response.getStatusLine());
1666+
}
1667+
}
1668+
finally {
1669+
if (method != null) method.abort();
1670+
}
1671+
1672+
}
1673+
1674+
/**
1675+
* Purges all items from a given container from the CDN
1676+
*
1677+
* @param container The name of the container
1678+
* @param object The name of the object
1679+
* @param emailAddresses An optional comma separated list of email addresses to be notified when the purge is complete.
1680+
* <code>null</code> if desired.
1681+
* @throws IOException Error talking to the cdn management server
1682+
* @throws HttpException Error with HTTP
1683+
* @throws FilesAuthorizationException Log in was not successful, or account is suspended
1684+
* @throws FilesException Other error
1685+
*/
1686+
public void purgeCDNObject(String container, String object, String emailAddresses) throws IOException, HttpException, FilesAuthorizationException, FilesException {
1687+
if (! isLoggedin) {
1688+
throw new FilesAuthorizationException("You must be logged in", null, null);
1689+
}
1690+
if (!isValidContainerName(container)) {
1691+
throw new FilesInvalidNameException(container);
1692+
}
1693+
HttpDelete method = null;
1694+
try {
1695+
String deleteUri = cdnManagementURL + "/" + sanitizeForURI(container) +"/"+sanitizeForURI(object);
1696+
method = new HttpDelete(deleteUri);
1697+
method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
1698+
method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken);
1699+
if (emailAddresses != null) {
1700+
method.setHeader(FilesConstants.X_PURGE_EMAIL, emailAddresses);
1701+
}
1702+
1703+
FilesResponse response = new FilesResponse(client.execute(method));
1704+
1705+
if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
1706+
method.abort();
1707+
if(login()) {
1708+
method = new HttpDelete(deleteUri);
1709+
method.getParams().setIntParameter("http.socket.timeout", connectionTimeOut);
1710+
method.setHeader(FilesConstants.X_AUTH_TOKEN, authToken);
1711+
if (emailAddresses != null) {
1712+
method.setHeader(FilesConstants.X_PURGE_EMAIL, emailAddresses);
1713+
}
1714+
response = new FilesResponse(client.execute(method));
1715+
}
1716+
else {
1717+
throw new FilesAuthorizationException("Re-login failed", response.getResponseHeaders(), response.getStatusLine());
1718+
}
1719+
}
1720+
1721+
if (response.getStatusCode() == HttpStatus.SC_NO_CONTENT)
1722+
{
1723+
return;
1724+
}
1725+
else if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
1726+
throw new FilesAuthorizationException("User not Authorized!",response.getResponseHeaders(), response.getStatusLine());
1727+
}
1728+
else {
1729+
throw new FilesException("Unexpected server response",response.getResponseHeaders(), response.getStatusLine());
1730+
}
1731+
}
1732+
finally {
1733+
if (method != null) method.abort();
1734+
}
1735+
1736+
}
16101737

16111738
/**
16121739
* Gets list of all of the containers associated with this account.

src/main/java/com/rackspacecloud/client/cloudfiles/FilesConstants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class FilesConstants
1212
{
1313
private static Logger logger = Logger.getLogger(FilesConstants.class);
1414

15-
public static final String USER_AGENT = "java-cloudfiles/1.7.3";
15+
public static final String USER_AGENT = "java-cloudfiles/1.8.0";
1616

1717
/** HTTP Header token that identifies the username to Cloud Files **/
1818
public static final String X_STORAGE_USER_DEFAULT = "x-auth-user";
@@ -56,6 +56,8 @@ public class FilesConstants
5656
public static final int METADATA_NAME_LENGTH = 1024;
5757
public static final int METADATA_VALUE_LENGTH = 1024;
5858

59+
public static final String X_PURGE_EMAIL = "X-Purge-Email";
60+
5961
/** Prefix Cloud Files expects on all Meta data headers on Objects **/
6062
public static final String X_OBJECT_META = "X-Object-Meta-";
6163

src/main/java/com/rackspacecloud/client/cloudfiles/FilesResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.apache.http.Header;
88
import org.apache.http.HttpResponse;
99
import org.apache.http.StatusLine;
10-
import org.apache.http.HttpStatus;
1110
import org.apache.http.util.EntityUtils;
1211
import org.apache.http.HttpEntity;
1312
import org.apache.log4j.Logger;

src/test/java/com/rackspacecloud/client/cloudfiles/FilesClientTestCase.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,31 @@ public void testCDNUrlOnObject() {
15721572

15731573
}
15741574

1575+
public void testCDNPurge() {
1576+
String containerName = createTempContainerName("cdnPurgeTest");
1577+
try {
1578+
FilesClient client = new FilesClient();
1579+
// client.setUseETag(false);
1580+
assertTrue(client.login());
1581+
1582+
// Set up
1583+
client.createContainer(containerName);
1584+
String cdnUrl = client.cdnEnableContainer(containerName);
1585+
assertNotNull(cdnUrl);
1586+
1587+
client.purgeCDNContainer(containerName, "[email protected]");
1588+
client.purgeCDNContainer(containerName, null);
1589+
client.purgeCDNObject(containerName, "object.txt", "[email protected]");
1590+
1591+
assertTrue(client.deleteContainer(containerName));
1592+
1593+
} catch (Exception e) {
1594+
e.printStackTrace();
1595+
fail(e.getMessage());
1596+
}
1597+
1598+
}
1599+
15751600
public void testCDNContainerFullListingAll() {
15761601
FilesClient client = new FilesClient();
15771602
try {

0 commit comments

Comments
 (0)