/**
*
* Copyright (c) Microsoft and contributors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Warning: This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the
// code is regenerated.
package com.microsoft.windowsazure.management.sql;
import com.microsoft.windowsazure.core.OperationResponse;
import com.microsoft.windowsazure.core.ServiceOperations;
import com.microsoft.windowsazure.core.pipeline.apache.CustomHttpDelete;
import com.microsoft.windowsazure.core.utils.BOMInputStream;
import com.microsoft.windowsazure.core.utils.XmlUtility;
import com.microsoft.windowsazure.exception.ServiceException;
import com.microsoft.windowsazure.management.sql.models.FirewallRule;
import com.microsoft.windowsazure.management.sql.models.FirewallRuleCreateParameters;
import com.microsoft.windowsazure.management.sql.models.FirewallRuleCreateResponse;
import com.microsoft.windowsazure.management.sql.models.FirewallRuleGetResponse;
import com.microsoft.windowsazure.management.sql.models.FirewallRuleListResponse;
import com.microsoft.windowsazure.management.sql.models.FirewallRuleUpdateParameters;
import com.microsoft.windowsazure.management.sql.models.FirewallRuleUpdateResponse;
import com.microsoft.windowsazure.tracing.CloudTracing;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/**
* The Azure SQL Database Management API includes operations for managing the
* server-level Firewall Rules for Azure SQL Database Servers. You cannot
* manage the database-level firewall rules using the Azure SQL Database
* Management API; they can only be managed by running the Transact-SQL
* statements against the master or individual user databases.
*/
public class FirewallRuleOperationsImpl implements ServiceOperations<SqlManagementClientImpl>, FirewallRuleOperations {
/**
* Initializes a new instance of the FirewallRuleOperationsImpl class.
*
* @param client Reference to the service client.
*/
FirewallRuleOperationsImpl(SqlManagementClientImpl client) {
this.client = client;
}
private SqlManagementClientImpl client;
/**
* Gets a reference to the
* microsoft.windowsazure.management.sql.SqlManagementClientImpl.
* @return The Client value.
*/
public SqlManagementClientImpl getClient() {
return this.client;
}
/**
* Adds a new server-level Firewall Rule for an Azure SQL Database Server.
*
* @param serverName Required. The name of the Azure SQL Database Server to
* which this rule will be applied.
* @param parameters Required. The parameters for the Create Firewall Rule
* operation.
* @return Contains the response to a Create Firewall Rule operation.
*/
@Override
public Future<FirewallRuleCreateResponse> createAsync(final String serverName, final FirewallRuleCreateParameters parameters) {
return this.getClient().getExecutorService().submit(new Callable<FirewallRuleCreateResponse>() {
@Override
public FirewallRuleCreateResponse call() throws Exception {
return create(serverName, parameters);
}
});
}
/**
* Adds a new server-level Firewall Rule for an Azure SQL Database Server.
*
* @param serverName Required. The name of the Azure SQL Database Server to
* which this rule will be applied.
* @param parameters Required. The parameters for the Create Firewall Rule
* operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @throws IOException Signals that an I/O exception of some sort has
* occurred. This class is the general class of exceptions produced by
* failed or interrupted I/O operations.
* @throws ServiceException Thrown if an unexpected response is found.
* @return Contains the response to a Create Firewall Rule operation.
*/
@Override
public FirewallRuleCreateResponse create(String serverName, FirewallRuleCreateParameters parameters) throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
// Validate
if (serverName == null) {
throw new NullPointerException("serverName");
}
if (parameters == null) {
throw new NullPointerException("parameters");
}
if (parameters.getEndIPAddress() == null) {
throw new NullPointerException("parameters.EndIPAddress");
}
if (parameters.getName() == null) {
throw new NullPointerException("parameters.Name");
}
if (parameters.getStartIPAddress() == null) {
throw new NullPointerException("parameters.StartIPAddress");
}
// Tracing
boolean shouldTrace = CloudTracing.getIsEnabled();
String invocationId = null;
if (shouldTrace) {
invocationId = Long.toString(CloudTracing.getNextInvocationId());
HashMap<String, Object> tracingParameters = new HashMap<String, Object>();
tracingParameters.put("serverName", serverName);
tracingParameters.put("parameters", parameters);
CloudTracing.enter(invocationId, this, "createAsync", tracingParameters);
}
// Construct URL
String url = "/" + (this.getClient().getCredentials().getSubscriptionId() != null ? this.getClient().getCredentials().getSubscriptionId().trim() : "") + "/services/sqlservers/servers/" + serverName.trim() + "/firewallrules";
String baseUrl = this.getClient().getBaseUri().toString();
// Trim '/' character from the end of baseUrl and beginning of url.
if (baseUrl.charAt(baseUrl.length() - 1) == '/') {
baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0);
}
if (url.charAt(0) == '/') {
url = url.substring(1);
}
url = baseUrl + "/" + url;
url = url.replace(" ", "%20");
// Create HTTP transport objects
HttpPost httpRequest = new HttpPost(url);
// Set Headers
httpRequest.setHeader("Content-Type", "application/xml");
httpRequest.setHeader("x-ms-version", "2012-03-01");
// Serialize Request
String requestContent = null;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document requestDoc = documentBuilder.newDocument();
Element serviceResourceElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "ServiceResource");
requestDoc.appendChild(serviceResourceElement);
Element nameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Name");
nameElement.appendChild(requestDoc.createTextNode(parameters.getName()));
serviceResourceElement.appendChild(nameElement);
Element startIPAddressElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "StartIPAddress");
startIPAddressElement.appendChild(requestDoc.createTextNode(parameters.getStartIPAddress().getHostAddress()));
serviceResourceElement.appendChild(startIPAddressElement);
Element endIPAddressElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "EndIPAddress");
endIPAddressElement.appendChild(requestDoc.createTextNode(parameters.getEndIPAddress().getHostAddress()));
serviceResourceElement.appendChild(endIPAddressElement);
DOMSource domSource = new DOMSource(requestDoc);
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(domSource, streamResult);
requestContent = stringWriter.toString();
StringEntity entity = new StringEntity(requestContent);
httpRequest.setEntity(entity);
httpRequest.setHeader("Content-Type", "application/xml");
// Send Request
HttpResponse httpResponse = null;
try {
if (shouldTrace) {
CloudTracing.sendRequest(invocationId, httpRequest);
}
httpResponse = this.getClient().getHttpClient().execute(httpRequest);
if (shouldTrace) {
CloudTracing.receiveResponse(invocationId, httpResponse);
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_CREATED) {
ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse, httpResponse.getEntity());
if (shouldTrace) {
CloudTracing.error(invocationId, ex);
}
throw ex;
}
// Create Result
FirewallRuleCreateResponse result = null;
// Deserialize Response
InputStream responseContent = httpResponse.getEntity().getContent();
result = new FirewallRuleCreateResponse();
DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
documentBuilderFactory2.setNamespaceAware(true);
DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));
Element serviceResourceElement2 = XmlUtility.getElementByTagNameNS(responseDoc, "http://schemas.microsoft.com/windowsazure", "ServiceResource");
if (serviceResourceElement2 != null) {
FirewallRule serviceResourceInstance = new FirewallRule();
result.setFirewallRule(serviceResourceInstance);
Element startIPAddressElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "StartIPAddress");
if (startIPAddressElement2 != null) {
InetAddress startIPAddressInstance;
startIPAddressInstance = InetAddress.getByName(startIPAddressElement2.getTextContent());
serviceResourceInstance.setStartIPAddress(startIPAddressInstance);
}
Element endIPAddressElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "EndIPAddress");
if (endIPAddressElement2 != null) {
InetAddress endIPAddressInstance;
endIPAddressInstance = InetAddress.getByName(endIPAddressElement2.getTextContent());
serviceResourceInstance.setEndIPAddress(endIPAddressInstance);
}
Element nameElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "Name");
if (nameElement2 != null) {
String nameInstance;
nameInstance = nameElement2.getTextContent();
serviceResourceInstance.setName(nameInstance);
}
Element typeElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "Type");
if (typeElement != null) {
String typeInstance;
typeInstance = typeElement.getTextContent();
serviceResourceInstance.setType(typeInstance);
}
Element stateElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "State");
if (stateElement != null) {
String stateInstance;
stateInstance = stateElement.getTextContent();
serviceResourceInstance.setState(stateInstance);
}
}
result.setStatusCode(statusCode);
if (httpResponse.getHeaders("x-ms-request-id").length > 0) {
result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue());
}
if (shouldTrace) {
CloudTracing.exit(invocationId, result);
}
return result;
} finally {
if (httpResponse != null && httpResponse.getEntity() != null) {
httpResponse.getEntity().getContent().close();
}
}
}
/**
* Deletes a server-level Firewall Rule from an Azure SQL Database Server.
*
* @param serverName Required. The name of the Azure SQL Database Server
* that will have the Firewall Fule removed from it.
* @param ruleName Required. The name of the Firewall Fule to delete.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public Future<OperationResponse> deleteAsync(final String serverName, final String ruleName) {
return this.getClient().getExecutorService().submit(new Callable<OperationResponse>() {
@Override
public OperationResponse call() throws Exception {
return delete(serverName, ruleName);
}
});
}
/**
* Deletes a server-level Firewall Rule from an Azure SQL Database Server.
*
* @param serverName Required. The name of the Azure SQL Database Server
* that will have the Firewall Fule removed from it.
* @param ruleName Required. The name of the Firewall Fule to delete.
* @throws IOException Signals that an I/O exception of some sort has
* occurred. This class is the general class of exceptions produced by
* failed or interrupted I/O operations.
* @throws ServiceException Thrown if an unexpected response is found.
* @return A standard service response including an HTTP status code and
* request ID.
*/
@Override
public OperationResponse delete(String serverName, String ruleName) throws IOException, ServiceException {
// Validate
if (serverName == null) {
throw new NullPointerException("serverName");
}
if (ruleName == null) {
throw new NullPointerException("ruleName");
}
// Tracing
boolean shouldTrace = CloudTracing.getIsEnabled();
String invocationId = null;
if (shouldTrace) {
invocationId = Long.toString(CloudTracing.getNextInvocationId());
HashMap<String, Object> tracingParameters = new HashMap<String, Object>();
tracingParameters.put("serverName", serverName);
tracingParameters.put("ruleName", ruleName);
CloudTracing.enter(invocationId, this, "deleteAsync", tracingParameters);
}
// Construct URL
String url = "/" + (this.getClient().getCredentials().getSubscriptionId() != null ? this.getClient().getCredentials().getSubscriptionId().trim() : "") + "/services/sqlservers/servers/" + serverName.trim() + "/firewallrules/" + ruleName.trim();
String baseUrl = this.getClient().getBaseUri().toString();
// Trim '/' character from the end of baseUrl and beginning of url.
if (baseUrl.charAt(baseUrl.length() - 1) == '/') {
baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0);
}
if (url.charAt(0) == '/') {
url = url.substring(1);
}
url = baseUrl + "/" + url;
url = url.replace(" ", "%20");
// Create HTTP transport objects
CustomHttpDelete httpRequest = new CustomHttpDelete(url);
// Set Headers
httpRequest.setHeader("x-ms-version", "2012-03-01");
// Send Request
HttpResponse httpResponse = null;
try {
if (shouldTrace) {
CloudTracing.sendRequest(invocationId, httpRequest);
}
httpResponse = this.getClient().getHttpClient().execute(httpRequest);
if (shouldTrace) {
CloudTracing.receiveResponse(invocationId, httpResponse);
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
ServiceException ex = ServiceException.createFromXml(httpRequest, null, httpResponse, httpResponse.getEntity());
if (shouldTrace) {
CloudTracing.error(invocationId, ex);
}
throw ex;
}
// Create Result
OperationResponse result = null;
result = new OperationResponse();
result.setStatusCode(statusCode);
if (httpResponse.getHeaders("x-ms-request-id").length > 0) {
result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue());
}
if (shouldTrace) {
CloudTracing.exit(invocationId, result);
}
return result;
} finally {
if (httpResponse != null && httpResponse.getEntity() != null) {
httpResponse.getEntity().getContent().close();
}
}
}
/**
* Returns the Firewall rule for an Azure SQL Database Server with a
* matching name.
*
* @param serverName Required. The name of the Azure SQL Database Server to
* query for the Firewall Rule.
* @param ruleName Required. The name of the rule to retrieve.
* @return Contains the response from a request to Get Firewall Rule.
*/
@Override
public Future<FirewallRuleGetResponse> getAsync(final String serverName, final String ruleName) {
return this.getClient().getExecutorService().submit(new Callable<FirewallRuleGetResponse>() {
@Override
public FirewallRuleGetResponse call() throws Exception {
return get(serverName, ruleName);
}
});
}
/**
* Returns the Firewall rule for an Azure SQL Database Server with a
* matching name.
*
* @param serverName Required. The name of the Azure SQL Database Server to
* query for the Firewall Rule.
* @param ruleName Required. The name of the rule to retrieve.
* @throws IOException Signals that an I/O exception of some sort has
* occurred. This class is the general class of exceptions produced by
* failed or interrupted I/O operations.
* @throws ServiceException Thrown if an unexpected response is found.
* @throws ParserConfigurationException Thrown if there was a serious
* configuration error with the document parser.
* @throws SAXException Thrown if there was an error parsing the XML
* response.
* @return Contains the response from a request to Get Firewall Rule.
*/
@Override
public FirewallRuleGetResponse get(String serverName, String ruleName) throws IOException, ServiceException, ParserConfigurationException, SAXException {
// Validate
if (serverName == null) {
throw new NullPointerException("serverName");
}
if (ruleName == null) {
throw new NullPointerException("ruleName");
}
// Tracing
boolean shouldTrace = CloudTracing.getIsEnabled();
String invocationId = null;
if (shouldTrace) {
invocationId = Long.toString(CloudTracing.getNextInvocationId());
HashMap<String, Object> tracingParameters = new HashMap<String, Object>();
tracingParameters.put("serverName", serverName);
tracingParameters.put("ruleName", ruleName);
CloudTracing.enter(invocationId, this, "getAsync", tracingParameters);
}
// Construct URL
String url = "/" + (this.getClient().getCredentials().getSubscriptionId() != null ? this.getClient().getCredentials().getSubscriptionId().trim() : "") + "/services/sqlservers/servers/" + serverName.trim() + "/firewallrules/" + ruleName.trim();
String baseUrl = this.getClient().getBaseUri().toString();
// Trim '/' character from the end of baseUrl and beginning of url.
if (baseUrl.charAt(baseUrl.length() - 1) == '/') {
baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0);
}
if (url.charAt(0) == '/') {
url = url.substring(1);
}
url = baseUrl + "/" + url;
url = url.replace(" ", "%20");
// Create HTTP transport objects
HttpGet httpRequest = new HttpGet(url);
// Set Headers
httpRequest.setHeader("x-ms-version", "2012-03-01");
// Send Request
HttpResponse httpResponse = null;
try {
if (shouldTrace) {
CloudTracing.sendRequest(invocationId, httpRequest);
}
httpResponse = this.getClient().getHttpClient().execute(httpRequest);
if (shouldTrace) {
CloudTracing.receiveResponse(invocationId, httpResponse);
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
ServiceException ex = ServiceException.createFromXml(httpRequest, null, httpResponse, httpResponse.getEntity());
if (shouldTrace) {
CloudTracing.error(invocationId, ex);
}
throw ex;
}
// Create Result
FirewallRuleGetResponse result = null;
// Deserialize Response
InputStream responseContent = httpResponse.getEntity().getContent();
result = new FirewallRuleGetResponse();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document responseDoc = documentBuilder.parse(new BOMInputStream(responseContent));
Element serviceResourceElement = XmlUtility.getElementByTagNameNS(responseDoc, "http://schemas.microsoft.com/windowsazure", "ServiceResource");
if (serviceResourceElement != null) {
FirewallRule serviceResourceInstance = new FirewallRule();
result.setFirewallRule(serviceResourceInstance);
Element startIPAddressElement = XmlUtility.getElementByTagNameNS(serviceResourceElement, "http://schemas.microsoft.com/windowsazure", "StartIPAddress");
if (startIPAddressElement != null) {
InetAddress startIPAddressInstance;
startIPAddressInstance = InetAddress.getByName(startIPAddressElement.getTextContent());
serviceResourceInstance.setStartIPAddress(startIPAddressInstance);
}
Element endIPAddressElement = XmlUtility.getElementByTagNameNS(serviceResourceElement, "http://schemas.microsoft.com/windowsazure", "EndIPAddress");
if (endIPAddressElement != null) {
InetAddress endIPAddressInstance;
endIPAddressInstance = InetAddress.getByName(endIPAddressElement.getTextContent());
serviceResourceInstance.setEndIPAddress(endIPAddressInstance);
}
Element nameElement = XmlUtility.getElementByTagNameNS(serviceResourceElement, "http://schemas.microsoft.com/windowsazure", "Name");
if (nameElement != null) {
String nameInstance;
nameInstance = nameElement.getTextContent();
serviceResourceInstance.setName(nameInstance);
}
Element typeElement = XmlUtility.getElementByTagNameNS(serviceResourceElement, "http://schemas.microsoft.com/windowsazure", "Type");
if (typeElement != null) {
String typeInstance;
typeInstance = typeElement.getTextContent();
serviceResourceInstance.setType(typeInstance);
}
Element stateElement = XmlUtility.getElementByTagNameNS(serviceResourceElement, "http://schemas.microsoft.com/windowsazure", "State");
if (stateElement != null) {
String stateInstance;
stateInstance = stateElement.getTextContent();
serviceResourceInstance.setState(stateInstance);
}
}
result.setStatusCode(statusCode);
if (httpResponse.getHeaders("x-ms-request-id").length > 0) {
result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue());
}
if (shouldTrace) {
CloudTracing.exit(invocationId, result);
}
return result;
} finally {
if (httpResponse != null && httpResponse.getEntity() != null) {
httpResponse.getEntity().getContent().close();
}
}
}
/**
* Returns a list of server-level Firewall Rules for an Azure SQL Database
* Server.
*
* @param serverName Required. The name of the Azure SQL Database Server
* from which to list the Firewall Rules.
* @return Contains the response from a request to List Firewall Rules.
*/
@Override
public Future<FirewallRuleListResponse> listAsync(final String serverName) {
return this.getClient().getExecutorService().submit(new Callable<FirewallRuleListResponse>() {
@Override
public FirewallRuleListResponse call() throws Exception {
return list(serverName);
}
});
}
/**
* Returns a list of server-level Firewall Rules for an Azure SQL Database
* Server.
*
* @param serverName Required. The name of the Azure SQL Database Server
* from which to list the Firewall Rules.
* @throws IOException Signals that an I/O exception of some sort has
* occurred. This class is the general class of exceptions produced by
* failed or interrupted I/O operations.
* @throws ServiceException Thrown if an unexpected response is found.
* @throws ParserConfigurationException Thrown if there was a serious
* configuration error with the document parser.
* @throws SAXException Thrown if there was an error parsing the XML
* response.
* @return Contains the response from a request to List Firewall Rules.
*/
@Override
public FirewallRuleListResponse list(String serverName) throws IOException, ServiceException, ParserConfigurationException, SAXException {
// Validate
if (serverName == null) {
throw new NullPointerException("serverName");
}
// Tracing
boolean shouldTrace = CloudTracing.getIsEnabled();
String invocationId = null;
if (shouldTrace) {
invocationId = Long.toString(CloudTracing.getNextInvocationId());
HashMap<String, Object> tracingParameters = new HashMap<String, Object>();
tracingParameters.put("serverName", serverName);
CloudTracing.enter(invocationId, this, "listAsync", tracingParameters);
}
// Construct URL
String url = "/" + (this.getClient().getCredentials().getSubscriptionId() != null ? this.getClient().getCredentials().getSubscriptionId().trim() : "") + "/services/sqlservers/servers/" + serverName.trim() + "/firewallrules";
String baseUrl = this.getClient().getBaseUri().toString();
// Trim '/' character from the end of baseUrl and beginning of url.
if (baseUrl.charAt(baseUrl.length() - 1) == '/') {
baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0);
}
if (url.charAt(0) == '/') {
url = url.substring(1);
}
url = baseUrl + "/" + url;
url = url.replace(" ", "%20");
// Create HTTP transport objects
HttpGet httpRequest = new HttpGet(url);
// Set Headers
httpRequest.setHeader("x-ms-version", "2012-03-01");
// Send Request
HttpResponse httpResponse = null;
try {
if (shouldTrace) {
CloudTracing.sendRequest(invocationId, httpRequest);
}
httpResponse = this.getClient().getHttpClient().execute(httpRequest);
if (shouldTrace) {
CloudTracing.receiveResponse(invocationId, httpResponse);
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
ServiceException ex = ServiceException.createFromXml(httpRequest, null, httpResponse, httpResponse.getEntity());
if (shouldTrace) {
CloudTracing.error(invocationId, ex);
}
throw ex;
}
// Create Result
FirewallRuleListResponse result = null;
// Deserialize Response
InputStream responseContent = httpResponse.getEntity().getContent();
result = new FirewallRuleListResponse();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document responseDoc = documentBuilder.parse(new BOMInputStream(responseContent));
Element serviceResourcesSequenceElement = XmlUtility.getElementByTagNameNS(responseDoc, "http://schemas.microsoft.com/windowsazure", "ServiceResources");
if (serviceResourcesSequenceElement != null) {
for (int i1 = 0; i1 < com.microsoft.windowsazure.core.utils.XmlUtility.getElementsByTagNameNS(serviceResourcesSequenceElement, "http://schemas.microsoft.com/windowsazure", "ServiceResource").size(); i1 = i1 + 1) {
org.w3c.dom.Element serviceResourcesElement = ((org.w3c.dom.Element) com.microsoft.windowsazure.core.utils.XmlUtility.getElementsByTagNameNS(serviceResourcesSequenceElement, "http://schemas.microsoft.com/windowsazure", "ServiceResource").get(i1));
FirewallRule serviceResourceInstance = new FirewallRule();
result.getFirewallRules().add(serviceResourceInstance);
Element startIPAddressElement = XmlUtility.getElementByTagNameNS(serviceResourcesElement, "http://schemas.microsoft.com/windowsazure", "StartIPAddress");
if (startIPAddressElement != null) {
InetAddress startIPAddressInstance;
startIPAddressInstance = InetAddress.getByName(startIPAddressElement.getTextContent());
serviceResourceInstance.setStartIPAddress(startIPAddressInstance);
}
Element endIPAddressElement = XmlUtility.getElementByTagNameNS(serviceResourcesElement, "http://schemas.microsoft.com/windowsazure", "EndIPAddress");
if (endIPAddressElement != null) {
InetAddress endIPAddressInstance;
endIPAddressInstance = InetAddress.getByName(endIPAddressElement.getTextContent());
serviceResourceInstance.setEndIPAddress(endIPAddressInstance);
}
Element nameElement = XmlUtility.getElementByTagNameNS(serviceResourcesElement, "http://schemas.microsoft.com/windowsazure", "Name");
if (nameElement != null) {
String nameInstance;
nameInstance = nameElement.getTextContent();
serviceResourceInstance.setName(nameInstance);
}
Element typeElement = XmlUtility.getElementByTagNameNS(serviceResourcesElement, "http://schemas.microsoft.com/windowsazure", "Type");
if (typeElement != null) {
String typeInstance;
typeInstance = typeElement.getTextContent();
serviceResourceInstance.setType(typeInstance);
}
Element stateElement = XmlUtility.getElementByTagNameNS(serviceResourcesElement, "http://schemas.microsoft.com/windowsazure", "State");
if (stateElement != null) {
String stateInstance;
stateInstance = stateElement.getTextContent();
serviceResourceInstance.setState(stateInstance);
}
}
}
result.setStatusCode(statusCode);
if (httpResponse.getHeaders("x-ms-request-id").length > 0) {
result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue());
}
if (shouldTrace) {
CloudTracing.exit(invocationId, result);
}
return result;
} finally {
if (httpResponse != null && httpResponse.getEntity() != null) {
httpResponse.getEntity().getContent().close();
}
}
}
/**
* Updates an existing server-level Firewall Rule for an Azure SQL Database
* Server.
*
* @param serverName Required. The name of the Azure SQL Database Server
* that has the Firewall Rule to be updated.
* @param ruleName Required. The name of the Firewall Rule to be updated.
* @param parameters Required. The parameters for the Update Firewall Rule
* operation.
* @return Represents the firewall rule update response.
*/
@Override
public Future<FirewallRuleUpdateResponse> updateAsync(final String serverName, final String ruleName, final FirewallRuleUpdateParameters parameters) {
return this.getClient().getExecutorService().submit(new Callable<FirewallRuleUpdateResponse>() {
@Override
public FirewallRuleUpdateResponse call() throws Exception {
return update(serverName, ruleName, parameters);
}
});
}
/**
* Updates an existing server-level Firewall Rule for an Azure SQL Database
* Server.
*
* @param serverName Required. The name of the Azure SQL Database Server
* that has the Firewall Rule to be updated.
* @param ruleName Required. The name of the Firewall Rule to be updated.
* @param parameters Required. The parameters for the Update Firewall Rule
* operation.
* @throws ParserConfigurationException Thrown if there was an error
* configuring the parser for the response body.
* @throws SAXException Thrown if there was an error parsing the response
* body.
* @throws TransformerException Thrown if there was an error creating the
* DOM transformer.
* @throws IOException Signals that an I/O exception of some sort has
* occurred. This class is the general class of exceptions produced by
* failed or interrupted I/O operations.
* @throws ServiceException Thrown if an unexpected response is found.
* @return Represents the firewall rule update response.
*/
@Override
public FirewallRuleUpdateResponse update(String serverName, String ruleName, FirewallRuleUpdateParameters parameters) throws ParserConfigurationException, SAXException, TransformerException, IOException, ServiceException {
// Validate
if (serverName == null) {
throw new NullPointerException("serverName");
}
if (ruleName == null) {
throw new NullPointerException("ruleName");
}
if (parameters == null) {
throw new NullPointerException("parameters");
}
if (parameters.getEndIPAddress() == null) {
throw new NullPointerException("parameters.EndIPAddress");
}
if (parameters.getName() == null) {
throw new NullPointerException("parameters.Name");
}
if (parameters.getStartIPAddress() == null) {
throw new NullPointerException("parameters.StartIPAddress");
}
// Tracing
boolean shouldTrace = CloudTracing.getIsEnabled();
String invocationId = null;
if (shouldTrace) {
invocationId = Long.toString(CloudTracing.getNextInvocationId());
HashMap<String, Object> tracingParameters = new HashMap<String, Object>();
tracingParameters.put("serverName", serverName);
tracingParameters.put("ruleName", ruleName);
tracingParameters.put("parameters", parameters);
CloudTracing.enter(invocationId, this, "updateAsync", tracingParameters);
}
// Construct URL
String url = "/" + (this.getClient().getCredentials().getSubscriptionId() != null ? this.getClient().getCredentials().getSubscriptionId().trim() : "") + "/services/sqlservers/servers/" + serverName.trim() + "/firewallrules/" + ruleName.trim();
String baseUrl = this.getClient().getBaseUri().toString();
// Trim '/' character from the end of baseUrl and beginning of url.
if (baseUrl.charAt(baseUrl.length() - 1) == '/') {
baseUrl = baseUrl.substring(0, (baseUrl.length() - 1) + 0);
}
if (url.charAt(0) == '/') {
url = url.substring(1);
}
url = baseUrl + "/" + url;
url = url.replace(" ", "%20");
// Create HTTP transport objects
HttpPut httpRequest = new HttpPut(url);
// Set Headers
httpRequest.setHeader("Content-Type", "application/xml");
httpRequest.setHeader("x-ms-version", "2012-03-01");
// Serialize Request
String requestContent = null;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document requestDoc = documentBuilder.newDocument();
Element serviceResourceElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "ServiceResource");
requestDoc.appendChild(serviceResourceElement);
Element nameElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "Name");
nameElement.appendChild(requestDoc.createTextNode(parameters.getName()));
serviceResourceElement.appendChild(nameElement);
Element startIPAddressElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "StartIPAddress");
startIPAddressElement.appendChild(requestDoc.createTextNode(parameters.getStartIPAddress().getHostAddress()));
serviceResourceElement.appendChild(startIPAddressElement);
Element endIPAddressElement = requestDoc.createElementNS("http://schemas.microsoft.com/windowsazure", "EndIPAddress");
endIPAddressElement.appendChild(requestDoc.createTextNode(parameters.getEndIPAddress().getHostAddress()));
serviceResourceElement.appendChild(endIPAddressElement);
DOMSource domSource = new DOMSource(requestDoc);
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(domSource, streamResult);
requestContent = stringWriter.toString();
StringEntity entity = new StringEntity(requestContent);
httpRequest.setEntity(entity);
httpRequest.setHeader("Content-Type", "application/xml");
// Send Request
HttpResponse httpResponse = null;
try {
if (shouldTrace) {
CloudTracing.sendRequest(invocationId, httpRequest);
}
httpResponse = this.getClient().getHttpClient().execute(httpRequest);
if (shouldTrace) {
CloudTracing.receiveResponse(invocationId, httpResponse);
}
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
ServiceException ex = ServiceException.createFromXml(httpRequest, requestContent, httpResponse, httpResponse.getEntity());
if (shouldTrace) {
CloudTracing.error(invocationId, ex);
}
throw ex;
}
// Create Result
FirewallRuleUpdateResponse result = null;
// Deserialize Response
InputStream responseContent = httpResponse.getEntity().getContent();
result = new FirewallRuleUpdateResponse();
DocumentBuilderFactory documentBuilderFactory2 = DocumentBuilderFactory.newInstance();
documentBuilderFactory2.setNamespaceAware(true);
DocumentBuilder documentBuilder2 = documentBuilderFactory2.newDocumentBuilder();
Document responseDoc = documentBuilder2.parse(new BOMInputStream(responseContent));
Element serviceResourceElement2 = XmlUtility.getElementByTagNameNS(responseDoc, "http://schemas.microsoft.com/windowsazure", "ServiceResource");
if (serviceResourceElement2 != null) {
FirewallRule serviceResourceInstance = new FirewallRule();
result.setFirewallRule(serviceResourceInstance);
Element startIPAddressElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "StartIPAddress");
if (startIPAddressElement2 != null) {
InetAddress startIPAddressInstance;
startIPAddressInstance = InetAddress.getByName(startIPAddressElement2.getTextContent());
serviceResourceInstance.setStartIPAddress(startIPAddressInstance);
}
Element endIPAddressElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "EndIPAddress");
if (endIPAddressElement2 != null) {
InetAddress endIPAddressInstance;
endIPAddressInstance = InetAddress.getByName(endIPAddressElement2.getTextContent());
serviceResourceInstance.setEndIPAddress(endIPAddressInstance);
}
Element nameElement2 = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "Name");
if (nameElement2 != null) {
String nameInstance;
nameInstance = nameElement2.getTextContent();
serviceResourceInstance.setName(nameInstance);
}
Element typeElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "Type");
if (typeElement != null) {
String typeInstance;
typeInstance = typeElement.getTextContent();
serviceResourceInstance.setType(typeInstance);
}
Element stateElement = XmlUtility.getElementByTagNameNS(serviceResourceElement2, "http://schemas.microsoft.com/windowsazure", "State");
if (stateElement != null) {
String stateInstance;
stateInstance = stateElement.getTextContent();
serviceResourceInstance.setState(stateInstance);
}
}
result.setStatusCode(statusCode);
if (httpResponse.getHeaders("x-ms-request-id").length > 0) {
result.setRequestId(httpResponse.getFirstHeader("x-ms-request-id").getValue());
}
if (shouldTrace) {
CloudTracing.exit(invocationId, result);
}
return result;
} finally {
if (httpResponse != null && httpResponse.getEntity() != null) {
httpResponse.getEntity().getContent().close();
}
}
}
}