From 70bc9f025f17d3b443e0aa5ca39c3517f018e36c Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Thu, 2 Dec 2021 13:52:42 -0500 Subject: [PATCH] USRE-72 pull more metadata info back to session object --- .../usreio/client/CommonsHttpClient.java | 11 +- ...e.java => CommonsHttpClientResponseI.java} | 5 +- ...tor.java => NullINetworkEventMonitor.java} | 4 +- .../ossez/usreio/client/RetsHttpClient.java | 4 +- .../com/ossez/usreio/client/RetsSession.java | 758 +++++++++--------- .../ossez/usreio/client/RetsTransport.java | 38 +- .../INetworkEventMonitor.java} | 4 +- .../IRetsHttpResponse.java} | 6 +- .../{ => models/request}/LoginRequest.java | 5 +- .../{ => models/response}/LoginResponse.java | 3 +- .../usreio/client/retsapi/RETSConnection.java | 2 +- .../com/ossez/usreio/util/SessionUtils.java | 1 + .../usreio/tests/client/LoginRequestTest.java | 2 +- .../tests/client/LoginResponseTest.java | 2 +- .../usreio/tests/client/RetsSessionTest.java | 28 +- 15 files changed, 478 insertions(+), 395 deletions(-) rename rets-io-client/src/main/java/com/ossez/usreio/client/{CommonsHttpClientResponse.java => CommonsHttpClientResponseI.java} (93%) rename rets-io-client/src/main/java/com/ossez/usreio/client/{NullNetworkEventMonitor.java => NullINetworkEventMonitor.java} (52%) rename rets-io-client/src/main/java/com/ossez/usreio/client/{NetworkEventMonitor.java => interfaces/INetworkEventMonitor.java} (87%) rename rets-io-client/src/main/java/com/ossez/usreio/client/{RetsHttpResponse.java => interfaces/IRetsHttpResponse.java} (81%) rename rets-io-client/src/main/java/com/ossez/usreio/client/{ => models/request}/LoginRequest.java (76%) rename rets-io-client/src/main/java/com/ossez/usreio/client/{ => models/response}/LoginResponse.java (98%) diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClient.java b/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClient.java index 84a14ff..0933ef7 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClient.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClient.java @@ -4,6 +4,7 @@ import java.io.UnsupportedEncodingException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import com.ossez.usreio.client.interfaces.IRetsHttpResponse; import com.ossez.usreio.common.util.CaseInsensitiveTreeMap; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; @@ -88,12 +89,12 @@ public class CommonsHttpClient extends RetsHttpClient { this.httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds); } @Override - public RetsHttpResponse doRequest(String httpMethod, RetsHttpRequest request) throws RetsException { + public IRetsHttpResponse doRequest(String httpMethod, RetsHttpRequest request) throws RetsException { return "GET".equals(StringUtils.upperCase(httpMethod)) ? this.doGet(request) : this.doPost(request); } //----------------------method implementations - public RetsHttpResponse doGet(RetsHttpRequest request) throws RetsException { + public IRetsHttpResponse doGet(RetsHttpRequest request) throws RetsException { String url = request.getUrl(); String args = request.getHttpParameters(); if (args != null) { @@ -103,7 +104,7 @@ public class CommonsHttpClient extends RetsHttpClient { return execute(method, request.getHeaders()); } - public RetsHttpResponse doPost(RetsHttpRequest request) throws RetsException { + public IRetsHttpResponse doPost(RetsHttpRequest request) throws RetsException { String url = request.getUrl(); String body = request.getHttpParameters(); if (body == null) body = ""; // commons-httpclient 3.0 refuses to accept null entity (body) @@ -118,7 +119,7 @@ public class CommonsHttpClient extends RetsHttpClient { return execute(method, request.getHeaders()); } - protected RetsHttpResponse execute(final HttpRequestBase method, Map headers) throws RetsException { + protected IRetsHttpResponse execute(final HttpRequestBase method, Map headers) throws RetsException { try { // add the default headers if (this.defaultHeaders != null) { @@ -142,7 +143,7 @@ public class CommonsHttpClient extends RetsHttpClient { if (status.getStatusCode() != HttpStatus.SC_OK) { throw new InvalidHttpStatusException(status); } - return new CommonsHttpClientResponse(response, getCookies()); + return new CommonsHttpClientResponseI(response, getCookies()); } catch (Exception e) { throw new RetsException(e); } diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClientResponse.java b/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClientResponseI.java similarity index 93% rename from rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClientResponse.java rename to rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClientResponseI.java index 4236b63..2b3fe46 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClientResponse.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/CommonsHttpClientResponseI.java @@ -5,6 +5,7 @@ import java.io.InputStream; import java.util.Map; import java.util.zip.GZIPInputStream; +import com.ossez.usreio.client.interfaces.IRetsHttpResponse; import com.ossez.usreio.common.util.CaseInsensitiveTreeMap; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; @@ -13,12 +14,12 @@ import org.apache.http.HttpResponse; import com.google.common.io.Closeables; -public class CommonsHttpClientResponse implements RetsHttpResponse { +public class CommonsHttpClientResponseI implements IRetsHttpResponse { private HttpResponse response; private Map headers; private Map cookies; - public CommonsHttpClientResponse(HttpResponse response, Map cookies) { + public CommonsHttpClientResponseI(HttpResponse response, Map cookies) { this.response = response; this.cookies = new CaseInsensitiveTreeMap(cookies); diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/NullNetworkEventMonitor.java b/rets-io-client/src/main/java/com/ossez/usreio/client/NullINetworkEventMonitor.java similarity index 52% rename from rets-io-client/src/main/java/com/ossez/usreio/client/NullNetworkEventMonitor.java rename to rets-io-client/src/main/java/com/ossez/usreio/client/NullINetworkEventMonitor.java index 82526e5..107bee9 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/NullNetworkEventMonitor.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/NullINetworkEventMonitor.java @@ -1,6 +1,8 @@ package com.ossez.usreio.client; -public class NullNetworkEventMonitor implements NetworkEventMonitor { +import com.ossez.usreio.client.interfaces.INetworkEventMonitor; + +public class NullINetworkEventMonitor implements INetworkEventMonitor { public Object eventStart(String message) { return null; diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsHttpClient.java b/rets-io-client/src/main/java/com/ossez/usreio/client/RetsHttpClient.java index 0c77aae..089ff21 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsHttpClient.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/RetsHttpClient.java @@ -1,6 +1,8 @@ package com.ossez.usreio.client; +import com.ossez.usreio.client.interfaces.IRetsHttpResponse; + public abstract class RetsHttpClient { public static final String SESSION_ID_COOKIE = "RETS-Session-ID"; @@ -16,7 +18,7 @@ public abstract class RetsHttpClient { * @return * @throws RetsException */ - public abstract RetsHttpResponse doRequest(String httpMethod, RetsHttpRequest request) throws RetsException; + public abstract IRetsHttpResponse doRequest(String httpMethod, RetsHttpRequest request) throws RetsException; /** * diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsSession.java b/rets-io-client/src/main/java/com/ossez/usreio/client/RetsSession.java index 1406cdd..97394c4 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsSession.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/RetsSession.java @@ -2,403 +2,447 @@ package com.ossez.usreio.client; import java.util.Map; +import com.ossez.usreio.client.interfaces.IRetsHttpResponse; +import com.ossez.usreio.client.interfaces.INetworkEventMonitor; +import com.ossez.usreio.client.models.request.LoginRequest; +import com.ossez.usreio.client.models.response.LoginResponse; import com.ossez.usreio.common.rets.RetsVersion; import com.ossez.usreio.tests.common.metadata.Metadata; import com.ossez.usreio.tests.common.metadata.MetadataException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * RetsSession is the core class of the rets.client package. */ public class RetsSession { - public static final String METADATA_TABLES = "metadata_tables.xml"; - public static final String RETS_CLIENT_VERSION = "1.5";//change default version + private static final Logger logger = LoggerFactory.getLogger(RetsSession.class); - private static final Log LOG = LogFactory.getLog(RetsSession.class); - private static String sUserAgent = "crt-rets-client/" + RETS_CLIENT_VERSION; - - private CapabilityUrls capabilityUrls; - private RetsHttpClient httpClient; - private RetsTransport transport; - private String sessionId; + public static final String METADATA_TABLES = "metadata_tables.xml"; + public static final String RETS_CLIENT_VERSION = "1.5";//change default version - /** - * Creates a new RetsSession instance. - * You must call login(user, pass) before attempting any other - * transactions. - * - * Uses a default implementation of RetsHttpClient based on - * apache commons http client. - * - * Uses the RetsVersion.RETS_DEFAULT as the RetsVersion for - * this session. - * - * Uses sAgent at the User-Agent setting for this RetsSession. - * - * @param loginUrl URL of the Login transaction. - */ - public RetsSession(String loginUrl) { - this(loginUrl, new CommonsHttpClient()); - } + private static String sUserAgent = "crt-rets-client/" + RETS_CLIENT_VERSION; - /** - * Creates a new RetsSession instance. - * You must call login(user, pass) before attempting any other - * transactions. - * - * Uses the RetsVersion.RETS_DEFAULT as the RetsVersion for - * this session. - * - * Uses sAgent at the User-Agent setting for this RetsSession. - * - * @param loginUrl URL of the Login transaction - * @param httpClient a RetsHttpClient implementation. The default - * is CommonsHttpClient. - */ - public RetsSession(String loginUrl, RetsHttpClient httpClient) { - this(loginUrl, httpClient, RetsVersion.DEFAULT); - } + private RetsHttpClient httpClient; + private RetsTransport transport; + private String sessionId; + private CapabilityUrls capabilityUrls; + private String metadataVersion; + private String metadataTimestamp; + private String minMetadataVersion; + private String minMetadataTimestamp; - /** - * Creates a new RetsSession instance. - * You must call login(user, pass) before attempting any other - * transactions. - * - * Uses sAgent at the User-Agent setting for this RetsSession. - * - * @param loginUrl URL of the Login transaction - * @param httpClient a RetsHttpClient implementation. The default - * is CommonsHttpClient. - * @param retsVersion The RetsVersion used by this RetsSession. - */ - public RetsSession(String loginUrl, RetsHttpClient httpClient, RetsVersion retsVersion) { - this(loginUrl, httpClient, retsVersion, sUserAgent,false); - } - /** - * Creates a new RetsSession instance. - * You must call login(user, pass) before attempting any other - * transactions. - * - * @param loginUrl URL of the Login transaction - * @param httpClient a RetsHttpClient implementation. The default - * is CommonsHttpClient. - * @param retsVersion The RetsVersion used by this RetsSession. - * @param userAgent specific User-Agent to use for this session. - */ - public RetsSession(String loginUrl, RetsHttpClient httpClient, RetsVersion retsVersion, String userAgent, boolean strict) { - this.capabilityUrls = new CapabilityUrls(); - this.capabilityUrls.setLoginUrl(loginUrl); + /** + * Creates a new RetsSession instance. + * You must call login(user, pass) before attempting any other + * transactions. + *

+ * Uses a default implementation of RetsHttpClient based on + * apache commons http client. + *

+ * Uses the RetsVersion.RETS_DEFAULT as the RetsVersion for + * this session. + *

+ * Uses sAgent at the User-Agent setting for this RetsSession. + * + * @param loginUrl URL of the Login transaction. + */ + public RetsSession(String loginUrl) { + this(loginUrl, new CommonsHttpClient()); + } - this.httpClient = httpClient; - this.transport = new RetsTransport(httpClient, this.capabilityUrls, retsVersion, strict); - this.httpClient.addDefaultHeader("User-Agent", userAgent); - } + /** + * Creates a new RetsSession instance. + * You must call login(user, pass) before attempting any other + * transactions. + *

+ * Uses the RetsVersion.RETS_DEFAULT as the RetsVersion for + * this session. + *

+ * Uses sAgent at the User-Agent setting for this RetsSession. + * + * @param loginUrl URL of the Login transaction + * @param httpClient a RetsHttpClient implementation. The default + * is CommonsHttpClient. + */ + public RetsSession(String loginUrl, RetsHttpClient httpClient) { + this(loginUrl, httpClient, RetsVersion.DEFAULT); + } - /** - * Query the current RetsVersion being used in this session. - * - * Initially, this will be the value passed to the RetsTransport. - * However, if during auto-negotiation the RetsTransport changes - * the RetsSession, this value may change throughout the session. - * - * @return the current RetsVersion value being used by the - * RetsTransport. - */ - public RetsVersion getRetsVersion() { - return this.transport.getRetsVersion(); - } + /** + * Creates a new RetsSession instance. + * You must call login(user, pass) before attempting any other + * transactions. + *

+ * Uses sAgent at the User-Agent setting for this RetsSession. + * + * @param loginUrl URL of the Login transaction + * @param httpClient a RetsHttpClient implementation. The default + * is CommonsHttpClient. + * @param retsVersion The RetsVersion used by this RetsSession. + */ + public RetsSession(String loginUrl, RetsHttpClient httpClient, RetsVersion retsVersion) { + this(loginUrl, httpClient, retsVersion, sUserAgent, false); + } - /** - * Get the current RETS Session ID - * - * @return the current RETS Session ID or null is the server has - * not specified one - */ - public String getSessionId() { - return this.sessionId; - } + /** + * Creates a new RetsSession instance. + * You must call login(user, pass) before attempting any other + * transactions. + * + * @param loginUrl URL of the Login transaction + * @param httpClient a RetsHttpClient implementation. The default + * is CommonsHttpClient. + * @param retsVersion The RetsVersion used by this RetsSession. + * @param userAgent specific User-Agent to use for this session. + */ + public RetsSession(String loginUrl, RetsHttpClient httpClient, RetsVersion retsVersion, String userAgent, boolean strict) { + this.capabilityUrls = new CapabilityUrls(); + this.capabilityUrls.setLoginUrl(loginUrl); - public void setSessionId(String sessionId) { - LOG.debug("setting Session-ID to: " + sessionId); - this.sessionId = sessionId; - } + this.httpClient = httpClient; + this.transport = new RetsTransport(httpClient, this.capabilityUrls, retsVersion, strict); + this.httpClient.addDefaultHeader("User-Agent", userAgent); + } - public void setMonitor(NetworkEventMonitor monitor) { - this.transport.setMonitor(monitor); - } + /** + * Query the current RetsVersion being used in this session. + *

+ * Initially, this will be the value passed to the RetsTransport. + * However, if during auto-negotiation the RetsTransport changes + * the RetsSession, this value may change throughout the session. + * + * @return the current RetsVersion value being used by the + * RetsTransport. + */ + public RetsVersion getRetsVersion() { + return this.transport.getRetsVersion(); + } - public void setStrict(boolean strict) { - this.transport.setStrict(strict); - } - public boolean isStrict() { - return this.transport.isStrict(); - } - /** - * Sets the default User-Agent value for RetsSessions created without - * a specified User-Agent value. - * - * @param userAgent Default User-Agent value to use for all RetsSession - * objects created in the future. - */ - public static void setUserAgent(String userAgent) { - sUserAgent = userAgent; - } + public void setMonitor(INetworkEventMonitor monitor) { + this.transport.setMonitor(monitor); + } - public String getLoginUrl() { - return this.capabilityUrls.getLoginUrl(); - } + public void setStrict(boolean strict) { + this.transport.setStrict(strict); + } - public Metadata getIncrementalMetadata() throws RetsException { - try { - return new Metadata(new MetaCollectorImpl(this.transport)); - } catch (MetadataException e) { - throw new RetsException(e); - } - } + public boolean isStrict() { + return this.transport.isStrict(); + } - /** - * Get the complete RETS metadata. - * - * @return The RETS metadata object for these credentials. - * - * @throws RetsException - */ - public Metadata getMetadata() throws RetsException { - return this.transport.getMetadata("null"); - } - /** - * Ability to download the raw metadata to a location - * @param location - * @return - * @throws RetsException - */ - public Metadata getMetadata(String location) throws RetsException { - return this.transport.getMetadata(location); - } + /** + * Sets the default User-Agent value for RetsSessions created without + * a specified User-Agent value. + * + * @param userAgent Default User-Agent value to use for all RetsSession + * objects created in the future. + */ + public static void setUserAgent(String userAgent) { + sUserAgent = userAgent; + } - /** - * Perform a low level GetMetadatRequest. To retrieve - * structured metadata, - * - * @see #getMetadata() - * - * @param req GetMetadataRequest - * @return GetMetadataResponse, containing all MetaObjects - * returned - * - * @throws RetsException if an error occurs - */ - public GetMetadataResponse getMetadata(GetMetadataRequest req) throws RetsException { - return this.transport.getMetadata(req); - } + public String getLoginUrl() { + return this.capabilityUrls.getLoginUrl(); + } - /** - * Fetches the action (MOTD) from the server. - * - * @exception RetsException if an error occurs - */ - private void getAction() throws RetsException { - String actionUrl = this.capabilityUrls.getActionUrl(); - if (actionUrl == null) { - LOG.warn("No Action-URL available, skipping"); - return; - } - GenericHttpRequest actionRequest = new GenericHttpRequest(actionUrl){ - @Override - public Map getHeaders() { - return null; - } - }; - RetsHttpResponse httpResponse = this.httpClient.doRequest("GET", actionRequest); - try { - httpResponse.getInputStream().close(); - } catch (Exception e) { - LOG.error("Action URL weirdness", e); - } - } + public Metadata getIncrementalMetadata() throws RetsException { + try { + return new Metadata(new MetaCollectorImpl(this.transport)); + } catch (MetadataException e) { + throw new RetsException(e); + } + } - /** - * Implementation that allow for single or multi-part - * GetObject requests. - * - * @param req - * @return - * @exception RetsException if an error occurs - */ - public GetObjectResponse getObject(GetObjectRequest req) throws RetsException { - return this.transport.getObject(req); - } + /** + * Get the complete RETS metadata. + * + * @return The RETS metadata object for these credentials. + * @throws RetsException + */ + public Metadata getMetadata() throws RetsException { + return this.transport.getMetadata("null"); + } - /** - * - * @param resource - * @param type - * @param entity - * @param id - * @return response - * @exception RetsException if an error occurs - */ - public GetObjectResponse getObject(String resource, String type, String entity, String id) throws RetsException { - GetObjectRequest req = new GetObjectRequest(resource, type); - req.addObject(entity, id); - return getObject(req); - } + /** + * Ability to download the raw metadata to a location + * + * @param location + * @return + * @throws RetsException + */ + public Metadata getMetadata(String location) throws RetsException { + return this.transport.getMetadata(location); + } - /** - * Log into the RETS server (see RETS 1.5, section 4). No other - * transactions will work until you have logged in. - * - * @param userName Username to authenticate - * @param password Password to authenticate with - * @return LoginResponse if success. - * @exception RetsException if authentication was denied - */ - public LoginResponse login(String userName, String password) throws RetsException { - return login(userName, password, null, null); - } + /** + * Perform a low level GetMetadatRequest. To retrieve + * structured metadata, + * + * @param req GetMetadataRequest + * @return GetMetadataResponse, containing all MetaObjects + * returned + * @throws RetsException if an error occurs + * @see #getMetadata() + */ + public GetMetadataResponse getMetadata(GetMetadataRequest req) throws RetsException { + return this.transport.getMetadata(req); + } - /** - * Log into the RETS server (see RETS 1.5, section 4). No other - * transactions will work until you have logged in. - * - * @param userName username to authenticate - * @param password password to authenticate with - * @param brokerCode broker code if the same user belongs to multiple - * brokerages. May be null. - * @param brokerBranch branch code if the same user belongs to multiple - * branches. May be null. brokerCode is required if you want - * brokerBranch to work. - * @return LoginResponse if success. - * @exception RetsException if authentication was denied - */ + /** + * Fetches the action (MOTD) from the server. + * + * @throws RetsException if an error occurs + */ + private void getAction() throws RetsException { + String actionUrl = this.capabilityUrls.getActionUrl(); + if (actionUrl == null) { + logger.warn("No Action-URL available, skipping"); + return; + } + GenericHttpRequest actionRequest = new GenericHttpRequest(actionUrl) { + @Override + public Map getHeaders() { + return null; + } + }; + IRetsHttpResponse httpResponse = this.httpClient.doRequest("GET", actionRequest); + try { + httpResponse.getInputStream().close(); + } catch (Exception e) { + logger.error("Action URL weirdness", e); + } + } - public LoginResponse login(String userName, String password, String brokerCode, String brokerBranch) throws RetsException { - this.httpClient.setUserCredentials(userName, password); + /** + * Implementation that allow for single or multi-part + * GetObject requests. + * + * @param req + * @return + * @throws RetsException if an error occurs + */ + public GetObjectResponse getObject(GetObjectRequest req) throws RetsException { + return this.transport.getObject(req); + } - LoginRequest request = new LoginRequest(); - request.setBrokerCode(brokerCode, brokerBranch); + /** + * @param resource + * @param type + * @param entity + * @param id + * @return response + * @throws RetsException if an error occurs + */ + public GetObjectResponse getObject(String resource, String type, String entity, String id) throws RetsException { + GetObjectRequest req = new GetObjectRequest(resource, type); + req.addObject(entity, id); + return getObject(req); + } - LoginResponse response = this.transport.login(request); - this.capabilityUrls = response.getCapabilityUrls(); - this.transport.setCapabilities(this.capabilityUrls); - this.setSessionId(response.getSessionId()); - this.getAction(); + /** + * Log into the RETS server (see RETS 1.5, section 4). No other + * transactions will work until you have logged in. + * + * @param userName Username to authenticate + * @param password Password to authenticate with + * @return LoginResponse if success. + * @throws RetsException if authentication was denied + */ + public LoginResponse login(String userName, String password) throws RetsException { + return login(userName, password, null, null); + } - return response; - } + /** + * Log into the RETS server (see RETS 1.5, section 4). No other + * transactions will work until you have logged in. + * + * @param userName username to authenticate + * @param password password to authenticate with + * @param brokerCode broker code if the same user belongs to multiple + * brokerages. May be null. + * @param brokerBranch branch code if the same user belongs to multiple + * branches. May be null. brokerCode is required if you want + * brokerBranch to work. + * @return LoginResponse if success. + * @throws RetsException if authentication was denied + */ - /** - * Log out of the current session. Another login _may_ re-establish a new connection - * depending the the behavior of the {#link RetsHttpClient} and its' ability to - * maintain and restablish a connection. - * - * @return a LogoutResponse - * @throws RetsException if the logout transaction failed - */ - public LogoutResponse logout() throws RetsException { - try { - return this.transport.logout(); - } finally { - this.setSessionId(null); - } - } + public LoginResponse login(String userName, String password, String brokerCode, String brokerBranch) throws RetsException { + this.httpClient.setUserCredentials(userName, password); - /** - * Will perform a search as requested and return a filled - * SearchResult object. This method caches all result information - * in memory in the SearchResult object. - * - * @param req Contains parameters on which to search. - * @return a completed SearchResult - * @exception RetsException if an error occurs - */ - public SearchResult search(SearchRequest req) throws RetsException { - SearchResultImpl res = new SearchResultImpl(); - search(req, res); - return res; - } + LoginRequest request = new LoginRequest(); + request.setBrokerCode(brokerCode, brokerBranch); - /** - * Execute a RETS Search. The collector object will be filled - * when this method is returned. See RETS 1.52d, Section 5. - * - * @param req Contains parameters on which to search. - * @param collector SearchResult object which will be informed of the results - * as they come in. If you don't need live results, see the other - * search invocation. - * @exception RetsException if an error occurs - */ - public void search(SearchRequest req, SearchResultCollector collector) throws RetsException { - this.transport.search(req, collector); - } + LoginResponse response = this.transport.login(request); + this.capabilityUrls = response.getCapabilityUrls(); + this.transport.setCapabilities(this.capabilityUrls); + this.setSessionId(response.getSessionId()); + this.setMetadataVersion(response.getMetadataVersion()); + this.setMetadataTimestamp(response.getMetadataTimestamp()); + this.getAction(); - /** - * Search and process the Search using a given SearchResultProcessor. - * - * @param req the search request - * @param processor the result object that will process the data - */ - public SearchResultSet search(SearchRequest req, SearchResultProcessor processor) throws RetsException { - return this.transport.search(req, processor); - } + return response; + } - /** - * The lowest level integration. This method is not recommened for general use. - */ - public RetsHttpResponse request(RetsHttpRequest request) throws RetsException{ - return this.transport.doRequest(request); - } + /** + * Log out of the current session. Another login _may_ re-establish a new connection + * depending the the behavior of the {#link RetsHttpClient} and its' ability to + * maintain and restablish a connection. + * + * @return a LogoutResponse + * @throws RetsException if the logout transaction failed + */ + public LogoutResponse logout() throws RetsException { + try { + return this.transport.logout(); + } finally { + this.setSessionId(null); + } + } - /** - * switch to a specific HttpMethodName, POST/GET, where the - * method is supported. Where GET is not supported, POST - * will be used. - * @param method the HttpMethodName to use - */ - public void setMethod(String method) { - this.transport.setMethod(method); - } + /** + * Will perform a search as requested and return a filled + * SearchResult object. This method caches all result information + * in memory in the SearchResult object. + * + * @param req Contains parameters on which to search. + * @return a completed SearchResult + * @throws RetsException if an error occurs + */ + public SearchResult search(SearchRequest req) throws RetsException { + SearchResultImpl res = new SearchResultImpl(); + search(req, res); + return res; + } - /** Make sure GC'd sessions are logged out. */ - @Override - protected void finalize() throws Throwable { - try { - if( this.sessionId != null ) this.logout(); - } finally { - super.finalize(); - } - } - /** - * Performs a search returning only the number of records resulting from a query. - * - * Convenience method to get number records from a query - * - * @param req the search request - * @return the number of records that returned from the search request - * @throws RetsException - */ - public int getQueryCount(SearchRequest req) throws RetsException { - req.setCountOnly(); - SearchResult res = this.search(req); - return res.getCount(); - } + /** + * Execute a RETS Search. The collector object will be filled + * when this method is returned. See RETS 1.52d, Section 5. + * + * @param req Contains parameters on which to search. + * @param collector SearchResult object which will be informed of the results + * as they come in. If you don't need live results, see the other + * search invocation. + * @throws RetsException if an error occurs + */ + public void search(SearchRequest req, SearchResultCollector collector) throws RetsException { + this.transport.search(req, collector); + } - /** - * Gives the URL's of an Object request instead of object themselves - * - * Convenience method to get the URL's of the requeseted object only - * - * @param req - * @return - * @throws RetsException - */ - public GetObjectResponse getObjectUrl(GetObjectRequest req) throws RetsException { - req.setLocationOnly(true); - GetObjectResponse res = this.getObject(req); - return res; - } + /** + * Search and process the Search using a given SearchResultProcessor. + * + * @param req the search request + * @param processor the result object that will process the data + */ + public SearchResultSet search(SearchRequest req, SearchResultProcessor processor) throws RetsException { + return this.transport.search(req, processor); + } + + /** + * The lowest level integration. This method is not recommened for general use. + */ + public IRetsHttpResponse request(RetsHttpRequest request) throws RetsException { + return this.transport.doRequest(request); + } + + /** + * switch to a specific HttpMethodName, POST/GET, where the + * method is supported. Where GET is not supported, POST + * will be used. + * + * @param method the HttpMethodName to use + */ + public void setMethod(String method) { + this.transport.setMethod(method); + } + + /** + * Make sure GC'd sessions are logged out. + */ + @Override + protected void finalize() throws Throwable { + try { + if (this.sessionId != null) this.logout(); + } finally { + super.finalize(); + } + } + + /** + * Performs a search returning only the number of records resulting from a query. + *

+ * Convenience method to get number records from a query + * + * @param req the search request + * @return the number of records that returned from the search request + * @throws RetsException + */ + public int getQueryCount(SearchRequest req) throws RetsException { + req.setCountOnly(); + SearchResult res = this.search(req); + return res.getCount(); + } + + /** + * Gives the URL's of an Object request instead of object themselves + *

+ * Convenience method to get the URL's of the requeseted object only + * + * @param req + * @return + * @throws RetsException + */ + public GetObjectResponse getObjectUrl(GetObjectRequest req) throws RetsException { + req.setLocationOnly(true); + GetObjectResponse res = this.getObject(req); + return res; + } + + + // GET AND SET + public String getSessionId() { + return this.sessionId; + } + + public void setSessionId(String sessionId) { + this.sessionId = sessionId; + } + + public String getMetadataVersion() { + return metadataVersion; + } + + public void setMetadataVersion(String metadataVersion) { + this.metadataVersion = metadataVersion; + } + + public String getMetadataTimestamp() { + return metadataTimestamp; + } + + public void setMetadataTimestamp(String metadataTimestamp) { + this.metadataTimestamp = metadataTimestamp; + } + + public String getMinMetadataVersion() { + return minMetadataVersion; + } + + public void setMinMetadataVersion(String minMetadataVersion) { + this.minMetadataVersion = minMetadataVersion; + } + + public String getMinMetadataTimestamp() { + return minMetadataTimestamp; + } + + public void setMinMetadataTimestamp(String minMetadataTimestamp) { + this.minMetadataTimestamp = minMetadataTimestamp; + } } diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsTransport.java b/rets-io-client/src/main/java/com/ossez/usreio/client/RetsTransport.java index 92d32d2..93d2b79 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsTransport.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/RetsTransport.java @@ -5,6 +5,10 @@ import java.io.FileWriter; import java.util.HashMap; import java.util.Map; +import com.ossez.usreio.client.interfaces.IRetsHttpResponse; +import com.ossez.usreio.client.interfaces.INetworkEventMonitor; +import com.ossez.usreio.client.models.request.LoginRequest; +import com.ossez.usreio.client.models.response.LoginResponse; import com.ossez.usreio.common.rets.RetsVersion; import com.ossez.usreio.tests.common.metadata.JDomCompactBuilder; import com.ossez.usreio.tests.common.metadata.JDomStandardBuilder; @@ -31,7 +35,7 @@ public class RetsTransport { private String method = "GET"; private RetsVersion version; private boolean strict; - private NetworkEventMonitor monitor; + private INetworkEventMonitor monitor; private static final Log LOG = LogFactory.getLog(RetsTransport.class); @@ -73,7 +77,7 @@ public class RetsTransport { this.doVersionHeader(version); this.strict = strict; this.client.addDefaultHeader("Accept", "*/*"); - this.monitor = new NullNetworkEventMonitor(); + this.monitor = new NullINetworkEventMonitor(); } /** @@ -99,9 +103,9 @@ public class RetsTransport { this.strict = strict; } - public void setMonitor(NetworkEventMonitor monitor) { + public void setMonitor(INetworkEventMonitor monitor) { if (monitor == null) { - monitor = new NullNetworkEventMonitor(); + monitor = new NullINetworkEventMonitor(); } this.monitor = monitor; } @@ -141,7 +145,7 @@ public class RetsTransport { /** * Available as an integration last resort */ - public RetsHttpResponse doRequest(RetsHttpRequest req) throws RetsException { + public IRetsHttpResponse doRequest(RetsHttpRequest req) throws RetsException { Object monitorobj = null; String msg = getMonitorMessage(req); monitorobj = this.monitor.eventStart(msg); @@ -149,7 +153,7 @@ public class RetsTransport { req.setVersion(this.version); req.setUrl(this.capabilities); - RetsHttpResponse httpResponse; + IRetsHttpResponse httpResponse; try { httpResponse = this.client.doRequest(this.method, req); } finally { @@ -179,9 +183,9 @@ public class RetsTransport { * @see #setCapabilities */ public LoginResponse login(LoginRequest req) throws RetsException { - RetsHttpResponse retsHttpResponse = this.doRequest(req); + IRetsHttpResponse IRetsHttpResponse = this.doRequest(req); - String versionHeader = retsHttpResponse.getHeader(RetsVersion.RETS_VERSION_HEADER); + String versionHeader = IRetsHttpResponse.getHeader(RetsVersion.RETS_VERSION_HEADER); // may be null, which is fine, return null, dont throw RetsVersion retsVersion = RetsVersion.getVersion(versionHeader); if( retsVersion == null && this.strict ) @@ -191,10 +195,10 @@ public class RetsTransport { LoginResponse response = new LoginResponse(this.capabilities.getLoginUrl()); - String sessionId = retsHttpResponse.getCookie(RETS_SESSION_ID_HEADER); + String sessionId = IRetsHttpResponse.getCookie(RETS_SESSION_ID_HEADER); response.setSessionId(sessionId); response.setStrict(this.strict); - response.parse(retsHttpResponse.getInputStream(), this.version); + response.parse(IRetsHttpResponse.getInputStream(), this.version); return response; } @@ -212,7 +216,7 @@ public class RetsTransport { return null; } RetsHttpRequest req = new LogoutRequest(); - RetsHttpResponse httpResponse = doRequest(req); + IRetsHttpResponse httpResponse = doRequest(req); LogoutResponse response = new LogoutResponse(); response.setStrict(this.strict); try { @@ -235,7 +239,7 @@ public class RetsTransport { * @param collector the result object that will store the data */ public void search(SearchRequest req, SearchResultCollector collector) throws RetsException { - RetsHttpResponse httpResponse = doRequest(req); + IRetsHttpResponse httpResponse = doRequest(req); new SearchResultHandler(collector).parse(httpResponse.getInputStream(), httpResponse.getCharset()); } @@ -247,7 +251,7 @@ public class RetsTransport { * @param processor the result object that will process the data */ public SearchResultSet search(SearchRequest req, SearchResultProcessor processor) throws RetsException { - RetsHttpResponse httpResponse = doRequest(req); + IRetsHttpResponse httpResponse = doRequest(req); return processor.parse(httpResponse.getInputStream()); } @@ -263,7 +267,7 @@ public class RetsTransport { throw new RetsException("Server does not support GetObject transaction."); } req.setUrl(this.capabilities); - RetsHttpResponse httpResponse = this.client.doRequest(this.method, req); + IRetsHttpResponse httpResponse = this.client.doRequest(this.method, req); GetObjectResponse result = new GetObjectResponse(httpResponse.getHeaders(), httpResponse.getInputStream()); return result; } @@ -275,7 +279,7 @@ public class RetsTransport { req.setCompactFormat(); } try { - RetsHttpResponse httpResponse = doRequest(req); + IRetsHttpResponse httpResponse = doRequest(req); Object monitorobj = null; monitorobj = this.monitor.eventStart("Parsing metadata"); try { @@ -310,7 +314,7 @@ public class RetsTransport { } public GetMetadataResponse getMetadata(GetMetadataRequest req) throws RetsException { - RetsHttpResponse httpResponse = doRequest(req); + IRetsHttpResponse httpResponse = doRequest(req); Object monitorobj = null; monitorobj = this.monitor.eventStart("Parsing metadata"); try { @@ -326,7 +330,7 @@ public class RetsTransport { } public boolean changePassword(ChangePasswordRequest req) throws RetsException { - RetsHttpResponse httpResponse = doRequest(req); + IRetsHttpResponse httpResponse = doRequest(req); ChangePasswordResponse response = new ChangePasswordResponse(httpResponse.getInputStream()); // response will throw an exception if there is an error code return (response != null); diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/NetworkEventMonitor.java b/rets-io-client/src/main/java/com/ossez/usreio/client/interfaces/INetworkEventMonitor.java similarity index 87% rename from rets-io-client/src/main/java/com/ossez/usreio/client/NetworkEventMonitor.java rename to rets-io-client/src/main/java/com/ossez/usreio/client/interfaces/INetworkEventMonitor.java index 04ffe4b..c71ef4c 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/NetworkEventMonitor.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/interfaces/INetworkEventMonitor.java @@ -1,9 +1,9 @@ -package com.ossez.usreio.client; +package com.ossez.usreio.client.interfaces; /** * A client can register a monitor for network events */ -public interface NetworkEventMonitor +public interface INetworkEventMonitor { /** * inform the client app that an event has started. diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsHttpResponse.java b/rets-io-client/src/main/java/com/ossez/usreio/client/interfaces/IRetsHttpResponse.java similarity index 81% rename from rets-io-client/src/main/java/com/ossez/usreio/client/RetsHttpResponse.java rename to rets-io-client/src/main/java/com/ossez/usreio/client/interfaces/IRetsHttpResponse.java index 315464e..bcf6801 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/RetsHttpResponse.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/interfaces/IRetsHttpResponse.java @@ -1,4 +1,6 @@ -package com.ossez.usreio.client; +package com.ossez.usreio.client.interfaces; + +import com.ossez.usreio.client.RetsException; import java.io.InputStream; import java.util.Map; @@ -8,7 +10,7 @@ import java.util.Map; * * @author YuCheng Hu */ -public interface RetsHttpResponse { +public interface IRetsHttpResponse { public int getResponseCode() throws RetsException; public Map getHeaders() throws RetsException; diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/LoginRequest.java b/rets-io-client/src/main/java/com/ossez/usreio/client/models/request/LoginRequest.java similarity index 76% rename from rets-io-client/src/main/java/com/ossez/usreio/client/LoginRequest.java rename to rets-io-client/src/main/java/com/ossez/usreio/client/models/request/LoginRequest.java index 1576132..ec2aa23 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/LoginRequest.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/models/request/LoginRequest.java @@ -1,4 +1,7 @@ -package com.ossez.usreio.client; +package com.ossez.usreio.client.models.request; + +import com.ossez.usreio.client.CapabilityUrls; +import com.ossez.usreio.client.VersionInsensitiveRequest; public class LoginRequest extends VersionInsensitiveRequest { diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/LoginResponse.java b/rets-io-client/src/main/java/com/ossez/usreio/client/models/response/LoginResponse.java similarity index 98% rename from rets-io-client/src/main/java/com/ossez/usreio/client/LoginResponse.java rename to rets-io-client/src/main/java/com/ossez/usreio/client/models/response/LoginResponse.java index a6a558e..6f14d25 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/LoginResponse.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/models/response/LoginResponse.java @@ -1,5 +1,6 @@ -package com.ossez.usreio.client; +package com.ossez.usreio.client.models.response; +import com.ossez.usreio.client.*; import com.ossez.usreio.common.rets.RetsVersion; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; diff --git a/rets-io-client/src/main/java/com/ossez/usreio/client/retsapi/RETSConnection.java b/rets-io-client/src/main/java/com/ossez/usreio/client/retsapi/RETSConnection.java index 793e3ea..86249f9 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/client/retsapi/RETSConnection.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/client/retsapi/RETSConnection.java @@ -84,7 +84,7 @@ public class RETSConnection extends java.lang.Object { */ public RETSConnection() { setRequestHeaderField("User-Agent", "Mozilla/4.0"); - setRequestHeaderField("RETS-Version", "RETS/1.0"); + setRequestHeaderField("RETS-Version", "RETS/1.7.2"); } /** diff --git a/rets-io-client/src/main/java/com/ossez/usreio/util/SessionUtils.java b/rets-io-client/src/main/java/com/ossez/usreio/util/SessionUtils.java index 637484b..2a332a1 100644 --- a/rets-io-client/src/main/java/com/ossez/usreio/util/SessionUtils.java +++ b/rets-io-client/src/main/java/com/ossez/usreio/util/SessionUtils.java @@ -1,6 +1,7 @@ package com.ossez.usreio.util; import com.ossez.usreio.client.*; +import com.ossez.usreio.client.models.response.LoginResponse; import com.ossez.usreio.common.rets.RetsConfigurator; import com.ossez.usreio.common.rets.RetsVersion; import org.apache.commons.lang3.ObjectUtils; diff --git a/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginRequestTest.java b/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginRequestTest.java index 19bc732..06afbbc 100644 --- a/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginRequestTest.java +++ b/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginRequestTest.java @@ -1,6 +1,6 @@ package com.ossez.usreio.tests.client; -import com.ossez.usreio.client.LoginRequest; +import com.ossez.usreio.client.models.request.LoginRequest; public class LoginRequestTest extends RetsTestCase { public void testGetUrl() { diff --git a/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginResponseTest.java b/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginResponseTest.java index 0b945de..8948b06 100644 --- a/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginResponseTest.java +++ b/rets-io-client/src/test/java/com/ossez/usreio/tests/client/LoginResponseTest.java @@ -2,7 +2,7 @@ package com.ossez.usreio.tests.client; import com.ossez.usreio.client.CapabilityUrls; -import com.ossez.usreio.client.LoginResponse; +import com.ossez.usreio.client.models.response.LoginResponse; import com.ossez.usreio.client.RetsException; import com.ossez.usreio.common.rets.RetsVersion; import org.junit.Test; diff --git a/rets-io-client/src/test/java/com/ossez/usreio/tests/client/RetsSessionTest.java b/rets-io-client/src/test/java/com/ossez/usreio/tests/client/RetsSessionTest.java index 1f93d74..e84d6bd 100644 --- a/rets-io-client/src/test/java/com/ossez/usreio/tests/client/RetsSessionTest.java +++ b/rets-io-client/src/test/java/com/ossez/usreio/tests/client/RetsSessionTest.java @@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; public class RetsSessionTest extends RetsTestCase { private final Logger logger = LoggerFactory.getLogger(RetsSessionTest.class); - /** * Test Login should return SessionID from server */ @@ -33,13 +32,34 @@ public class RetsSessionTest extends RetsTestCase { try { RetsSession session = SessionUtils.retsLogin(retsLoginUrl, retsUsername, retsPassword, RetsVersion.RETS_1_7_2); assertNotNull(session.getSessionId()); + + // CHECK MetaData + logger.error("Session ID - [{}]", session.getSessionId()); + logger.error("MetaData version - [{}]", session.getMetadataVersion()); + logger.error("MetaData Timestamp - [{}]", session.getMetadataTimestamp()); + + } catch (RetsException ex) { + logger.error("Session Login Error", ex); + } + } + + /** + * Test Login should return SessionID from server + */ + @Test + public void testLoginWithConfigurator() { + logger.debug("Test Rets Session Login by URL: [{}]", retsLoginUrl); + + try { + RetsSession session = SessionUtils.retsLogin(retsConfigurator); +// session.login() + assertNotNull(session.getSessionId()); } catch (RetsException ex) { logger.debug("Session Login Error", ex); } - - } + /** * TEST Logout */ @@ -50,7 +70,9 @@ public class RetsSessionTest extends RetsTestCase { try { session = SessionUtils.retsLogin(retsLoginUrl, retsUsername, retsPassword, RetsVersion.RETS_1_7_2); + assertNotNull(session.getSessionId()); + } catch (RetsException ex) { logger.debug("Session Login Error", ex); }