Skip to content

Commit

Permalink
Fixed JAXBContext from leaking
Browse files Browse the repository at this point in the history
Moved JAXBContext to be a static final variable  and updated XeroClient to only create one instance of XeroJAXBMarshaller.
  • Loading branch information
SidneyAllen committed Apr 30, 2018
1 parent 8677946 commit 20564e9
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add this dependency and repository to your POM.xml
<dependency>
<groupId>com.xero</groupId>
<artifactId>xero-java-sdk</artifactId>
<version>1.0.0-beta-4</version>
<version>1.0.0</version>
</dependency>

<repositories>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.xero</groupId>
<artifactId>xero-java-sdk</artifactId>
<packaging>jar</packaging>
<version>1.0.0-beta-4</version>
<version>1.0.0</version>
<name>Xero-Java SDK</name>
<url>http://maven.apache.org</url>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/xero/api/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public String getAccessTokenUrl() {

@Override
public String getUserAgent() {
return USER_AGENT + " [Xero-Java-1.0.0-beta-4]";
return USER_AGENT + " [Xero-Java-1.0.0]";
}

@Override
Expand Down
137 changes: 68 additions & 69 deletions src/main/java/com/xero/api/XeroClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class XeroClient {
private String token = null;
private String tokenSecret = null;
final static Logger logger = Logger.getLogger(XeroClient.class);

static XeroJAXBMarshaller u = new XeroJAXBMarshaller();
protected static final DateFormat utcFormatter;

static {
Expand Down Expand Up @@ -67,7 +67,7 @@ protected Response get(String endPoint) throws IOException {
}

protected Response get(String endPoint, Date modifiedAfter, Map<String, String> params) throws IOException {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();

OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "GET", null, params);
req.setToken(token);
Expand All @@ -77,15 +77,15 @@ protected Response get(String endPoint, Date modifiedAfter, Map<String, String>
}

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}
/*
Expand All @@ -111,7 +111,7 @@ protected String getFile(String endPoint,
String fileName = null;

try {
ByteArrayInputStream input = req.executefile();
ByteArrayInputStream input = req.executefile();
fileName = req.getFileName();
saveFilePath = dirPath + File.separator + fileName;

Expand All @@ -133,11 +133,11 @@ protected String getFile(String endPoint,

return saveFilePath;
} catch (IOException ioe) {
logger.error(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

Expand All @@ -156,16 +156,16 @@ protected ByteArrayInputStream getInputStream(String endPoint,
ByteArrayInputStream byteStream = req.executefile();
return byteStream;
} catch (IOException ioe) {
logger.error(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

protected Response put(String endPoint, JAXBElement<?> object) {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();

String contents = u.marshall(object);

Expand All @@ -174,144 +174,143 @@ protected Response put(String endPoint, JAXBElement<?> object) {
req.setTokenSecret(tokenSecret);

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

protected Response put(String endPoint, String contentType, byte[] bytes) throws IOException {
return put(endPoint, contentType, bytes, null);
return put(endPoint, contentType, bytes, null);
}

protected Response put(String endPoint, String contentType, byte[] bytes, Map<? extends String, ?> params) throws IOException {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();
protected Response put(String endPoint, String contentType, byte[] bytes, Map<? extends String, ?> params) throws IOException {
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();

OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "PUT", contentType, bytes, params);
OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "PUT", contentType, bytes, params);

req.setToken(token);
req.setTokenSecret(tokenSecret);

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

protected Response put(String endPoint, JAXBElement<?> object, Map<String, String> params) {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();

String contents = u.marshall(object);
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();
String contents = u.marshall(object);

OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "PUT", contents, params);
req.setToken(token);
req.setTokenSecret(tokenSecret);

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

protected Response put(String endPoint, String contentType, File file) throws IOException {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();

OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "PUT", contentType, file, null);
OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "PUT", contentType, file, null);
req.setToken(token);
req.setTokenSecret(tokenSecret);

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

protected Response post(String endPoint, JAXBElement<?> object) throws IOException {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();

String contents = u.marshall(object);
String contents = u.marshall(object);
OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "POST", contents, null);
req.setToken(token);
req.setTokenSecret(tokenSecret);

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

protected Response post(String endPoint, JAXBElement<?> object, Map<String, String> params) throws IOException {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();

String contents = u.marshall(object);
String contents = u.marshall(object);
OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "POST", contents, params);
req.setToken(token);
req.setTokenSecret(tokenSecret);

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

protected Response delete(String endPoint) throws IOException {
XeroJAXBMarshaller u = new XeroJAXBMarshaller();
//XeroJAXBMarshaller u = new XeroJAXBMarshaller();

OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "DELETE", null, null);
OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "DELETE", null, null);
req.setToken(token);
req.setTokenSecret(tokenSecret);

try {
Map<String, String> resp = req.execute();
Object r = resp.get("content");
Map<String, String> resp = req.execute();
Object r = resp.get("content");
return u.unmarshall(r.toString(), Response.class);
} catch (IOException ioe) {
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
logger.error(ioe);
throw xeroExceptionHandler.convertException(ioe);
} catch (XeroApiException e) {
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
logger.error(e);
throw this.xeroExceptionHandler.handleBadRequest(e.getMessage(),e.getResponseCode());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public XeroApiException handleBadRequest(HttpResponseException httpResponseExcep
return convertException(httpResponseException);
}
} else {
return convertException(httpResponseException);
return newApiException(httpResponseException);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/xero/api/jaxb/XeroJAXBMarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@
*/
public class XeroJAXBMarshaller {


private final Marshaller marshaller;
private final Unmarshaller unmarshaller;
static final JAXBContext context = initContext();

private static JAXBContext initContext() {
try {
return JAXBContext.newInstance("com.xero.model", XeroJAXBMarshaller.class.getClassLoader());
} catch (Exception e) {
throw new XeroClientException(e.getMessage(), e);
}
}

public XeroJAXBMarshaller() {
try {
JAXBContext context = JAXBContext.newInstance("com.xero.model");
try {
//context = JAXBContext.newInstance("com.xero.model");
marshaller = context.createMarshaller();
unmarshaller = context.createUnmarshaller();
} catch (Exception e) {
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/com/xero/api/exception/XeroExceptionHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -67,6 +65,17 @@ public void handleOAuthErrorRequest() throws Exception {
assertNotNull(xeroApiException.getMessage());
}

@Test
public void handleOtherErrorRequest() throws Exception {
when(httpResponseException.getContent()).thenReturn("dummy");
when(httpResponseException.getStatusCode()).thenReturn(400);
XeroApiException xeroApiException = xeroExceptionHandler.handleBadRequest(httpResponseException);
assertNotNull(xeroApiException);
assertNotNull(xeroApiException.getMessages());
assertNotNull(xeroApiException.getMessage());
assertEquals(xeroApiException.getResponseCode(), 400);
}

/**
* sample oauth exception
* @return oauth exception
Expand Down

0 comments on commit 20564e9

Please sign in to comment.