Fixed some formatting issues

This commit is contained in:
Joshua Darnell 2020-04-09 01:47:10 -07:00
parent aca7bbe709
commit e80d1fc172
2 changed files with 98 additions and 83 deletions

Binary file not shown.

View File

@ -46,20 +46,17 @@ import java.util.function.Function;
* the ones the Client programmer is expected to use.
*/
public class Commander {
private static final Logger LOG = LogManager.getLogger(Commander.class);
public static final int OK = 0;
public static final int NOT_OK = 1;
public static final String AMPERSAND = "&"; //TODO: find the corresponding query params constant for this
public static final String EQUALS = "="; //TODO: find the corresponding query params constant for this
public static final Integer DEFAULT_PAGE_SIZE = 10;
public static final Integer DEFAULT_PAGE_LIMIT = 1;
public static final String REPORT_DIVIDER = "==============================================================";
public static final String REPORT_DIVIDER_SMALL = "===========================";
public static final String RESOSCRIPT_EXTENSION = ".resoscript";
public static final String EDMX_EXTENSION = ".xml";
private static final Logger LOG = LogManager.getLogger(Commander.class);
private static final String EDM_4_0_3_XSD = "edm.4.0.errata03.xsd", EDMX_4_0_3_XSD = "edmx.4.0.errata03.xsd";
private static String bearerToken;
@ -93,8 +90,8 @@ public class Commander {
factory.setNamespaceAware(true);
factory.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource[]{
new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(EDM_4_0_3_XSD)),
new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(EDMX_4_0_3_XSD))
new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(EDM_4_0_3_XSD)),
new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(EDMX_4_0_3_XSD))
}));
SAXParser parser = factory.newSAXParser();
@ -252,8 +249,88 @@ public class Commander {
return reportBuilder.toString();
}
/**
* Static version of the metadata validator that can work with a given client
*
* @param metadata the XML Metadata to validate
* @param client the OData client to use for validation
* @return true if the given XML metadata is valid, false otherwise
*/
public static boolean validateMetadata(XMLMetadata metadata, ODataClient client) {
try {
// call the probably-useless metadata validator. can't hurt though
// SEE: https://github.com/apache/olingo-odata4/blob/master/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java#L77-L116
client.metadataValidation().validateMetadata(metadata);
// also check whether metadata contains a valid service document in OData v4 format
return client.metadataValidation().isServiceDocument(metadata)
&& client.metadataValidation().isV4Metadata(metadata);
} catch (NullPointerException nex) {
LOG.error("ERROR: Validation Failed! Null pointer exception while trying to validate metadata.");
} catch (Exception ex) {
LOG.error("ERROR: Validation Failed! General error occurred when validating metadata.\n" + ex.getMessage());
if (ex.getCause() != null) {
LOG.error("Caused by: " + ex.getCause().getMessage());
}
}
return false;
}
/**
* Static version of the metadata validator that can work with a given client
*
* @param edm the Edm to validate
* @param client the OData client to use for validation
* @return true if the given XML metadata is valid, false otherwise
*/
public static boolean validateMetadata(Edm edm, ODataClient client) {
try {
// call the probably-useless metadata validator. can't hurt though
// SEE: https://github.com/apache/olingo-odata4/blob/master/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java#L77-L116
client.metadataValidation().validateMetadata(edm);
//if Edm metadata are invalid, the previous line will throw an exception and this line won't be reached.
return true;
} catch (NullPointerException nex) {
LOG.error("ERROR: Validation Failed! Null pointer exception while trying to validate metadata.");
} catch (Exception ex) {
LOG.error("ERROR: Validation Failed! General error occurred when validating metadata.\n" + ex.getMessage());
if (ex.getCause() != null) {
LOG.error("Caused by: " + ex.getCause().getMessage());
}
}
return false;
}
/**
* Deserializes XML Metadata from a string
*
* @param xmlMetadataString a string containing XML Metadata
* @param client an instance of an OData Client
* @return the XML Metadata contained within the string
*/
public static XMLMetadata deserializeXMLMetadata(String xmlMetadataString, ODataClient client) {
//deserialize response into XML Metadata - will throw an exception if metadata are invalid
return client.getDeserializer(ContentType.APPLICATION_XML)
.toMetadata(new ByteArrayInputStream(xmlMetadataString.getBytes(StandardCharsets.UTF_8)));
}
/**
* Deserializes Edm from XML Metadata
*
* @param xmlMetadataString a string containing XML metadata
* @param client an instance of an OData Client
* @return the Edm contained within the xmlMetadataString
* <p>
* TODO: rewrite the separate Edm request in the Web API server test code to only make one request and convert
* to Edm from the XML Metadata that was received.
*/
public static Edm deserializeEdm(String xmlMetadataString, ODataClient client) {
return client.getReader().readMetadata(new ByteArrayInputStream(xmlMetadataString.getBytes(StandardCharsets.UTF_8)));
}
/**
* OData client getter
*
* @return the OData client for the current Commander instance
*/
public ODataClient getClient() {
@ -262,6 +339,7 @@ public class Commander {
/**
* OData client setter
*
* @param client sets the current Commander instance to use the given client
*/
public void setClient(ODataClient client) {
@ -270,6 +348,7 @@ public class Commander {
/**
* Token URI getter
*
* @return the tokenUri used by the current Commander instance, or null
*/
public String getTokenUri() {
@ -278,6 +357,7 @@ public class Commander {
/**
* Service Root getter
*
* @return the serviceRoot used by the current Commander instance, or null
*/
public String getServiceRoot() {
@ -304,6 +384,7 @@ public class Commander {
/**
* Prepares an Edm Metadata request
*
* @return a prepared Edm metadata request
*/
public EdmMetadataRequest prepareEdmMetadataRequest() {
@ -315,6 +396,7 @@ public class Commander {
/**
* Prepares an XML Metadata request
*
* @return a prepared XML Metadata request
*/
public XMLMetadataRequest prepareXMLMetadataRequest() {
@ -360,32 +442,6 @@ public class Commander {
return validateMetadata(metadata, client);
}
/**
* Static version of the metadata validator that can work with a given client
* @param metadata the XML Metadata to validate
* @param client the OData client to use for validation
* @return true if the given XML metadata is valid, false otherwise
*/
public static boolean validateMetadata(XMLMetadata metadata, ODataClient client) {
try {
// call the probably-useless metadata validator. can't hurt though
// SEE: https://github.com/apache/olingo-odata4/blob/master/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java#L77-L116
client.metadataValidation().validateMetadata(metadata);
// also check whether metadata contains a valid service document in OData v4 format
return client.metadataValidation().isServiceDocument(metadata)
&& client.metadataValidation().isV4Metadata(metadata);
} catch (NullPointerException nex) {
LOG.error("ERROR: Validation Failed! Null pointer exception while trying to validate metadata.");
} catch (Exception ex) {
LOG.error("ERROR: Validation Failed! General error occurred when validating metadata.\n" + ex.getMessage());
if (ex.getCause() != null) {
LOG.error("Caused by: " + ex.getCause().getMessage());
}
}
return false;
}
/**
* Validates given XMLMetadata
*
@ -396,30 +452,6 @@ public class Commander {
return validateMetadata(metadata, client);
}
/**
* Static version of the metadata validator that can work with a given client
* @param edm the Edm to validate
* @param client the OData client to use for validation
* @return true if the given XML metadata is valid, false otherwise
*/
public static boolean validateMetadata(Edm edm, ODataClient client) {
try {
// call the probably-useless metadata validator. can't hurt though
// SEE: https://github.com/apache/olingo-odata4/blob/master/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ODataMetadataValidationImpl.java#L77-L116
client.metadataValidation().validateMetadata(edm);
//if Edm metadata are invalid, the previous line will throw an exception and this line won't be reached.
return true;
} catch (NullPointerException nex) {
LOG.error("ERROR: Validation Failed! Null pointer exception while trying to validate metadata.");
} catch (Exception ex) {
LOG.error("ERROR: Validation Failed! General error occurred when validating metadata.\n" + ex.getMessage());
if (ex.getCause() != null) {
LOG.error("Caused by: " + ex.getCause().getMessage());
}
}
return false;
}
/**
* Ensures that the input stream contains valid XMLMetadata.
*
@ -449,31 +481,6 @@ public class Commander {
return false;
}
/**
* Deserializes XML Metadata from a string
* @param xmlMetadataString a string containing XML Metadata
* @param client an instance of an OData Client
* @return the XML Metadata contained within the string
*/
public static XMLMetadata deserializeXMLMetadata(String xmlMetadataString, ODataClient client) {
//deserialize response into XML Metadata - will throw an exception if metadata are invalid
return client.getDeserializer(ContentType.APPLICATION_XML)
.toMetadata(new ByteArrayInputStream(xmlMetadataString.getBytes(StandardCharsets.UTF_8)));
}
/**
* Deserializes Edm from XML Metadata
* @param xmlMetadataString a string containing XML metadata
* @param client an instance of an OData Client
* @return the Edm contained within the xmlMetadataString
*
* TODO: rewrite the separate Edm request in the Web API server test code to only make one request and convert
* to Edm from the XML Metadata that was received.
*/
public static Edm deserializeEdm(String xmlMetadataString, ODataClient client) {
return client.getReader().readMetadata(new ByteArrayInputStream(xmlMetadataString.getBytes(StandardCharsets.UTF_8)));
}
/**
* Validates the given metadata contained in the given file path.
*
@ -553,6 +560,7 @@ public class Commander {
/**
* Executes a raw OData request in the current commander instance without trying to intepret the results
*
* @param requestUri the URI to make the request to
* @return a string containing the serialized response, or null
*/
@ -723,6 +731,7 @@ public class Commander {
/**
* Service root setter
*
* @param serviceRoot the Web API service root
* @return a Builder containing the given Web API service root
*/
@ -733,6 +742,7 @@ public class Commander {
/**
* Bearer token setter
*
* @param bearerToken the token to use to connect to the server
* @return a Builder set with the given bearerToken
*/
@ -743,6 +753,7 @@ public class Commander {
/**
* Client Identification setter
*
* @param clientId the OAuth2 client_id to use to authenticate against the server
* @return a Builder set with the given clientId
*/
@ -753,6 +764,7 @@ public class Commander {
/**
* Client Secret setter
*
* @param clientSecret the OAuth2 client_secret to use to authenticate against the server
* @return a Builder set with the given clientSecret
*/
@ -763,6 +775,7 @@ public class Commander {
/**
* Token URI setter
*
* @param tokenUri the OAuth2 token_uri to use to authenticate against the server
* @return a Builder set with the given tokenUri
*/
@ -773,6 +786,7 @@ public class Commander {
/**
* Scope setter
*
* @param scope the OAuth2 scope to use to authenticate against the server
* @return a Builder set with the given scope
*/
@ -795,6 +809,7 @@ public class Commander {
/**
* Commander builder is used to create instances of the RESO Commander, which should not be instantiated directly.
*
* @return a Commander instantiated with the given properties set
*/
public Commander build() {
@ -811,8 +826,8 @@ public class Commander {
//items required for OAuth client
isOAuthClient = clientId != null && clientId.length() > 0
&& clientSecret != null && clientSecret.length() > 0
&& tokenUri != null && tokenUri.length() > 0;
&& clientSecret != null && clientSecret.length() > 0
&& tokenUri != null && tokenUri.length() > 0;
//items required for token client
isTokenClient = bearerToken != null && bearerToken.length() > 0;