Skip to content

A bunch of code cleanup #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
7faa6e9
Migrate labkey-client-api to use a modern JSON library: JSON-java
labkey-adam Sep 27, 2022
5a58bc1
Publish with new SNAPSHOT version
labkey-adam Sep 27, 2022
8b4ad73
Fixes for BasicTest
labkey-adam Sep 27, 2022
4fd094e
Sync up org.json.* version
labkey-adam Sep 27, 2022
7eaf718
Hey, "exceptionClass" is optional
labkey-adam Sep 27, 2022
66391e1
Pass JSONObject instead of Map for better null handling
labkey-adam Sep 27, 2022
ecc87bf
Attempt to fix Domain response object
labkey-adam Sep 27, 2022
2a4cad6
Optional
labkey-adam Sep 28, 2022
78b73a2
More optional
labkey-adam Sep 28, 2022
416e41f
Default to null instead of empty string
labkey-adam Sep 28, 2022
420573b
Optional
labkey-adam Sep 28, 2022
3bb8399
Fix subtle issue in CommandResponse that was causing our standard fix…
labkey-adam Sep 28, 2022
1ec4b21
Null check
labkey-adam Sep 28, 2022
2275190
Optional
labkey-adam Sep 28, 2022
72d193d
More optional properties
labkey-adam Sep 28, 2022
83e7e19
Even more optional properties
labkey-adam Sep 28, 2022
8d0f5b5
These are all optional as well
labkey-adam Sep 28, 2022
a319ddd
JSONArray -> List<Map<String, Object>>
labkey-adam Sep 29, 2022
3ee740f
Fix expanded format handling
labkey-adam Sep 29, 2022
979c40d
Optional
labkey-adam Sep 29, 2022
6e23d9a
Optional
labkey-adam Sep 29, 2022
3a38c4d
Long -> Integer
labkey-adam Sep 29, 2022
c8f3ac6
Try optNumber()
labkey-adam Sep 29, 2022
01d6d37
Protocol tweaks
labkey-adam Sep 29, 2022
b392418
Optional dataFileURL
labkey-adam Sep 29, 2022
2df41a5
Optional
labkey-adam Sep 29, 2022
aa965ed
Adjust for NULL object
labkey-adam Sep 30, 2022
7854b59
Optional
labkey-adam Oct 3, 2022
1c81d84
Revamp ExpObject to remove json-simple workaround
labkey-adam Oct 4, 2022
1139715
Optional id
labkey-adam Oct 5, 2022
8461515
Optional
labkey-adam Oct 5, 2022
34b4a7e
Merge branch 'develop' into fb_jsonUpgrade
labkey-adam Oct 5, 2022
96b6d90
Optional
labkey-adam Oct 5, 2022
22f2515
Optional
labkey-adam Oct 5, 2022
1aa4dcd
Optional
labkey-adam Oct 5, 2022
be9f882
Optional PHI
labkey-adam Oct 6, 2022
477b2da
Optional Run properties
labkey-adam Oct 6, 2022
455d327
User IDs are Integers
labkey-adam Oct 6, 2022
7b98b03
PHI is optional
labkey-adam Oct 6, 2022
2730c78
Another round with ExpObject
labkey-adam Oct 6, 2022
1d51965
Chipping away
labkey-adam Oct 7, 2022
86f4613
Casting, fix invalid JSON
labkey-adam Oct 7, 2022
30e1b01
Wrap 'null' values in SaveRowsCommand
labkey-tchad Oct 7, 2022
dc20416
Get correct default value for ExpObject ID
labkey-tchad Oct 10, 2022
636ba05
Fix material ids
labkey-tchad Oct 11, 2022
1f5796e
Simplify optional boolean properties
labkey-tchad Oct 17, 2022
2c53948
Tidy more optional properties
labkey-tchad Oct 17, 2022
989cb6f
Fix 'PropertyDescriptor' constructor
labkey-tchad Oct 17, 2022
0189daa
Fix null string
labkey-tchad Oct 18, 2022
0e0dceb
A bunch of code cleanup.
labkey-tchad Oct 14, 2022
042547a
Remove generic 'copy' return types
labkey-tchad Oct 17, 2022
0540b89
Tidy up response parameters
labkey-tchad Oct 18, 2022
609af23
Fix ClassCastException
labkey-tchad Oct 18, 2022
a0b84b1
Merge branch 'fb_jsonUpgrade' into codeCleanup
labkey-tchad Oct 18, 2022
fe4fdc7
Remove copy constructors
labkey-tchad Oct 18, 2022
4da2d53
Add some documentation and make ResponseObject properties immutable
labkey-tchad Oct 20, 2022
09e7848
More docs and 'DomainDetailsCommand'
labkey-tchad Oct 20, 2022
91dabde
Merge remote-tracking branch 'origin/develop' into codeCleanup
labkey-tchad Oct 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion labkey-client-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The LabKey Remote API Library for Java - Change Log

## version 4.0.0
## version 4.0.0-jsonUpgrade-SNAPSHOT
*Released*: TBD
* Replace the internal JSON library with [JSON-java](https://github.com/stleary/JSON-java). The previous library,
[json-simple](https://github.com/fangyidong/json-simple) is no longer maintained (last released in early 2012) and
Expand Down
3 changes: 2 additions & 1 deletion labkey-client-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.labkey.gradle.plugin.LabKey
import org.labkey.gradle.util.BuildUtils
import org.labkey.gradle.util.GroupNames
import org.labkey.gradle.util.PomFileHelper

Expand Down Expand Up @@ -48,7 +49,7 @@ repositories {

group "org.labkey.api"

version "4.0.0-SNAPSHOT"
version "4.0.0-jsonUpgrade-SNAPSHOT"

dependencies {
api "org.json:json:${jsonObjectVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
*/
package org.labkey.remoteapi;

import org.apache.hc.client5.http.auth.AuthenticationException;
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.client5.http.protocol.HttpClientContext;

import java.net.URI;

/**
* Created by adam on 4/15/2016.
*/
public class ApiKeyCredentialsProvider implements CredentialsProvider
{
private final String _apiKey;
Expand All @@ -34,7 +30,7 @@ public ApiKeyCredentialsProvider(String apiKey)
}

@Override
public void configureRequest(URI baseURI, HttpUriRequest request, HttpClientContext httpClientContext) throws AuthenticationException
public void configureRequest(URI baseURI, HttpUriRequest request, HttpClientContext httpClientContext)
{
request.setHeader("apikey", _apiKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@

import org.json.JSONObject;

/**
* User: adam
* Date: Feb 23, 2009
* Time: 1:13:26 PM
*/
public class ApiVersionException extends CommandException
{
ApiVersionException(String message, int statusCode, JSONObject jsonProperties, String responseText, String contentType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@
package org.labkey.remoteapi;

import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.AuthenticationException;
import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.protocol.HttpClientContext;

import java.net.URI;

/**
* Created by adam on 4/15/2016.
*/
public class BasicAuthCredentialsProvider implements CredentialsProvider
{
private final String _email;
Expand All @@ -41,7 +36,7 @@ public BasicAuthCredentialsProvider(String email, String password)
}

@Override
public void configureRequest(URI baseURI, HttpUriRequest request, HttpClientContext httpClientContext) throws AuthenticationException
public void configureRequest(URI baseURI, HttpUriRequest request, HttpClientContext httpClientContext)
{
BasicCredentialsProvider provider = new BasicCredentialsProvider();
AuthScope scope = new AuthScope(baseURI.getHost(), -1);
Expand Down
30 changes: 12 additions & 18 deletions labkey-client-api/src/org/labkey/remoteapi/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand All @@ -45,16 +52,13 @@
* However, if future versions of the LabKey Server expose new HTTP APIs
* that are not yet supported with a specialized class in this library,
* the developer may still invoke these APIs by creating an instance of the
* Command object directly, providing the controller and action name for
* the new API. Parameters may then be specified by calling the <code>setParameters()</code>
* {@link Command} class directly, providing the controller and action name for
* the new API. Parameters may then be specified by calling the {@link #setParameters(Map)}
* method, passing a populated parameter <code>Map&lt;String, Object&gt;</code>
* <p>
* Note that this class is not thread-safe. Do not share instances of this class
* or its descendants between threads, unless the descendant declares explicitly that
* it is thread-safe.
*
* @author Dave Stearns, LabKey Corporation
* @version 1.0
*/
public class Command<ResponseType extends CommandResponse>
{
Expand Down Expand Up @@ -386,7 +390,7 @@ private void throwError(Response r, boolean throwByDefault) throws IOException,
json = new JSONObject(responseText);
if (json.has("exception"))
{
message = (String)json.get("exception");
message = json.getString("exception");

if ("org.labkey.api.action.ApiVersionException".equals(json.opt("exceptionClass")))
throw new ApiVersionException(message, r.getStatusCode(), json, responseText, contentType);
Expand Down Expand Up @@ -419,7 +423,7 @@ private void throwError(Response r, boolean throwByDefault) throws IOException,
*/
protected ResponseType createResponse(String text, int status, String contentType, JSONObject json)
{
return (ResponseType)new CommandResponse(text, status, contentType, json, this.copy());
return (ResponseType)new CommandResponse(text, status, contentType, json, this);
}

/**
Expand Down Expand Up @@ -570,14 +574,4 @@ public void setRequiredVersion(double requiredVersion)
_requiredVersion = requiredVersion;
}

/**
* Returns a copy of this object. Derived classes should override this
* to copy their own data members
* @return A copy of this object
*/
// TODO: For next major release, genericize this return value (Command<ResponseType>)
public Command copy()
{
return new Command<>(this);
}
}
32 changes: 6 additions & 26 deletions labkey-client-api/src/org/labkey/remoteapi/CommandResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
* User: Dave
* Date: Jul 10, 2008
* Time: 1:21:24 PM
*/

/**
* Represents the details of a response returned from the LabKey Server.
* <p>
Expand All @@ -36,16 +30,13 @@
* SelectRowsResponse). However, if you are using the Command class directly to
* call an HTTP API that does not yet have a specialized command class,
* you would use this object to obtain the response details.
*
* @author Dave Stearns, LabKey Corporation
* @version 1.0
*/
public class CommandResponse
{
private final String _text;
private final int _statusCode;
private final String _contentType;
private final Command _sourceCommand;
private final double _requiredVersion;

private Map<String, Object> _data;

Expand All @@ -58,13 +49,13 @@ public class CommandResponse
* @param json The parsed JSONObject (or null if JSON was not returned).
* @param sourceCommand A copy of the command that created this response
*/
public CommandResponse(String text, int statusCode, String contentType, JSONObject json, Command sourceCommand)
public CommandResponse(String text, int statusCode, String contentType, JSONObject json, Command<?> sourceCommand)
{
_text = text;
_statusCode = statusCode;
_contentType = contentType;
_data = null != json ? json.toMap() : null;
_sourceCommand = sourceCommand;
_requiredVersion = sourceCommand != null ? sourceCommand.getRequiredVersion() : 0.0;
}

/**
Expand Down Expand Up @@ -110,16 +101,7 @@ public String getContentType()
*/
public double getRequiredVersion()
{
return _sourceCommand.getRequiredVersion();
}

/**
* Returns a reference to a copy of the command that created this response
* @return The command that created this response
*/
public Command getSourceCommand()
{
return _sourceCommand;
return _requiredVersion;
}

/**
Expand All @@ -131,7 +113,6 @@ public Command getSourceCommand()
* Lists, or Maps.
* @return The parsed data as a property map.
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getParsedData()
{
if (null == _data && null != getText()
Expand All @@ -153,7 +134,6 @@ public Map<String, Object> getParsedData()
* @param <T> the type of the property.
* @return The property value, or null if the property was not found.
*/
@SuppressWarnings("unchecked")
public <T> T getProperty(String path)
{
assert null != path;
Expand Down Expand Up @@ -192,8 +172,8 @@ protected <T> T getProperty(String[] path, int pathIndex, Map<String, Object> pa
Object prop = parent.get(key);
if (arrayIndex != null)
{
if (prop instanceof List && ((List) prop).size() > arrayIndex)
prop = ((List)prop).get(arrayIndex);
if (prop instanceof List<?> list && list.size() > arrayIndex)
prop = list.get(arrayIndex);
else
return null;
}
Expand Down
37 changes: 14 additions & 23 deletions labkey-client-api/src/org/labkey/remoteapi/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@
* Note that this class is not thread-safe. Do not share instances of Connection
* between threads.
* </p>
*
* @author Dave Stearns, LabKey Corporation
* @version 1.0
*/
public class Connection
{
Expand Down Expand Up @@ -318,35 +315,29 @@ protected void afterExecute()

/**
* Ensures that the credentials have been used to authenticate the users and returns a client that can be used for other requests
* @return an HTTP client
* @throws IOException if there is an IO problem executing the command to ensure login
*
* @throws IOException if there is an IO problem executing the command to ensure login
* @throws CommandException if the server returned a non-success status code.
*/
// TODO: For next major release, stop returning CloseableHttpClient?
public CloseableHttpClient ensureAuthenticated() throws IOException, CommandException
public void ensureAuthenticated() throws IOException, CommandException
{
EnsureLoginCommand command = new EnsureLoginCommand();
CommandResponse response = command.execute(this, "/home");
return getHttpClient();
new EnsureLoginCommand().execute(this, "/home");
}

/**
* Invalidates the current HTTP session on the server, if any
* @return an HTTP client
*
* @throws IOException if there is an IO problem executing the command to ensure logout
* @throws CommandException if the server returned a non-success status code.
*/
// TODO: For next major release, stop returning CloseableHttpClient?
public CloseableHttpClient logout() throws IOException, CommandException
public void logout() throws IOException, CommandException
{
LogoutCommand command = new LogoutCommand();
CommandResponse response = command.execute(this, "/home");
return getHttpClient();
new LogoutCommand().execute(this, "/home");
}

/**
* For site-admins only, start impersonating a user.
*
* <p>
* Admins may impersonate other users to perform actions on their behalf.
* Site admins may impersonate any user in any project. Project admins must
* provide a <code>projectPath</code> and may only impersonate within the
Expand All @@ -365,7 +356,7 @@ public Connection impersonate(/*@NotNull*/ String email) throws IOException, Com

/**
* For site-admins or project-admins only, start impersonating a user.
*
* <p>
* Admins may impersonate other users to perform actions on their behalf.
* Site admins may impersonate any user in any project. Project admins must
* provide a <code>projectPath</code> and may only impersonate within the
Expand Down Expand Up @@ -420,7 +411,7 @@ public Connection stopImpersonate() throws IOException, CommandException
return stopImpersonating();
}

CloseableHttpResponse executeRequest(HttpUriRequest request, Integer timeout) throws IOException, URISyntaxException, AuthenticationException
CloseableHttpResponse executeRequest(HttpUriRequest request, Integer timeout) throws IOException, AuthenticationException
{
// Delegate authentication setup to CredentialsProvider
_credentialsProvider.configureRequest(getBaseURI(), request, _httpClientContext);
Expand Down Expand Up @@ -468,8 +459,8 @@ public int getTimeout()
}

/**
* Sets the proxy host and port for this Connection.
* NOTE: Changing this setting will force the underlying http client to be recreated.
* Sets the proxy host and port for this Connection.<br>
* <i>NOTE: Changing this setting will force the underlying http client to be recreated.</i>
* @param host the proxy host
* @param port the proxy port
* @return this connection
Expand All @@ -496,8 +487,8 @@ public boolean isAcceptSelfSignedCerts()
/**
* Sets the accept-self-signed certificates option. Set to false
* to disable automatic acceptance of self-signed SSL certificates
* when using HTTPS.
* NOTE: Changing this setting will force the underlying http client to be recreated.
* when using HTTPS.<br>
* <i>NOTE: Changing this setting will force the underlying http client to be recreated.</i>
*
* @param acceptSelfSignedCerts set to false to not accept self-signed certificates
* @return this connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

import java.net.URI;

/**
* Created by adam on 4/15/2016.
*/
public interface CredentialsProvider
{
void configureRequest(URI baseURI, HttpUriRequest request, HttpClientContext httpClientContext) throws AuthenticationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.net.URI;

/**
* Created by adam on 4/15/2016.
*
* A credentials provider that provides no credentials. Connections using
* this provider will be granted guest access only.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import java.io.IOException;
import java.net.URI;


/**
* Created by adam on 4/15/2016.
*
* Attempts to find a .netrc or _netrc file and entry for the given host. If found, it will attempt basic auth using
* the email and password in the entry. If file or entry are not found, it connects as guest.
*/
Expand Down
Loading