Skip to content

Commit

Permalink
Merge pull request #3 from openstandia/dev
Browse files Browse the repository at this point in the history
fix: some improvements
  • Loading branch information
wadahiro authored Jun 6, 2022
2 parents 2e5f1dc + 27d0726 commit 89118ce
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package jp.openstandia.connector.smarthr;

import org.identityconnectors.common.logging.Log;
import org.identityconnectors.framework.common.exceptions.UnknownUidException;
import org.identityconnectors.framework.common.objects.*;

import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package jp.openstandia.connector.smarthr;

import org.identityconnectors.common.logging.Log;
import org.identityconnectors.framework.common.exceptions.UnknownUidException;
import org.identityconnectors.framework.common.objects.*;

import java.util.Set;
Expand Down Expand Up @@ -110,7 +109,7 @@ public SchemaDefinition getSchema() {

@Override
public Uid create(Set<Attribute> attributes) {
throw new UnsupportedOperationException("SmartHR API doesn't support creating company");
throw new UnsupportedOperationException("SmartHR API doesn't support creating company");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public class SmartHRConfiguration extends AbstractConfiguration {
private String endpointURL;
private GuardedString apiAccessToken;
private String httpProxyHost;
private Integer httpProxyPort = 3128;
private int httpProxyPort = 3128;
private String httpProxyUser;
private GuardedString httpProxyPassword;
private Integer defaultQueryPageSize = 50;
private Integer connectionTimeoutInSeconds = 10;
private Integer readTimeoutInSeconds = 10;
private Integer writeTimeoutInSeconds = 10;
private int defaultQueryPageSize = 50;
private int connectionTimeoutInSeconds = 10;
private int readTimeoutInSeconds = 10;
private int writeTimeoutInSeconds = 10;

@ConfigurationProperty(
order = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,19 +678,15 @@ private void throwExceptionIfServerError(Response response) throws ConnectorIOEx
}

private Response get(String url) throws IOException {
return get(url, null, -1, -1);
return get(url, null, 0, 0);
}

private Response get(String url, int page, int pageSize) throws IOException {
return get(url, null, page, pageSize);
}

private Response get(String url, Map<String, String> params, int page, int pageSize) throws IOException {
private Response get(String url, Map<String, String> params, int pageNumber, int pageSize) throws IOException {
HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
if (page != -1) {
httpBuilder.addQueryParameter("page", String.valueOf(page));
if (pageNumber > 0) {
httpBuilder.addQueryParameter("page", String.valueOf(pageNumber));
}
if (pageSize != 1) {
if (pageSize > 0) {
httpBuilder.addQueryParameter("per_page", String.valueOf(pageSize));
}
if (params != null) {
Expand All @@ -717,60 +713,139 @@ protected <T> int getAll(SmartHRQueryHandler<T> handler, OperationOptions option

protected <T> int getAll(SmartHRQueryHandler<T> handler, OperationOptions options, Map<String, String> params, int pageSize, int pageOffset,
String endpointURL, TypeReference<List<T>> valueTypeRef, ObjectClass objectClass) {
// Start from 1 in SmartHR
int page = 1;
PageInfo pageInfo = newPageInfo(pageOffset, pageSize);

if (pageInfo.isRequestedFullPage()) {
// Start from 1 in SmartHR
int pageNumber = 1;
int total;

while (true) {
try (Response response = get(endpointURL, params, pageNumber, pageSize)) {
if (response.code() != 200) {
ErrorResponse error = MAPPER.readValue(response.body().byteStream(), ErrorResponse.class);
throw new ConnectorIOException(String.format("Failed to get SmartHR %s. statusCode: %d, message: %s",
objectClass.getObjectClassValue(), response.code(), response.message()));
}

// If pageOffset is 1, it means showing first page only
if (pageOffset > 1) {
page = (int) Math.ceil(pageOffset / pageSize) + 1;
}
// Success
total = getTotalCount(response);

int totalCount;
List<T> objects = MAPPER.readValue(response.body().byteStream(), valueTypeRef);
if (objects.size() == 0) {
break;
}

// If no requested pageOffset, fetch all pages
while (true) {
try (Response response = get(endpointURL, params, page, pageSize)) {
if (response.code() != 200) {
ErrorResponse error = MAPPER.readValue(response.body().byteStream(), ErrorResponse.class);
throw new ConnectorIOException(String.format("Failed to get SmartHR %s. statusCode: %d, message: %s",
objectClass.getObjectClassValue(), response.code(), response.message()));
}
for (T object : objects) {
if (!handler.handle(object)) {
break;
}
}

// Success
totalCount = getTotalCount(response);
pageNumber = getPage(response);
pageSize = getPerPage(response);

List<T> objects = MAPPER.readValue(response.body().byteStream(), valueTypeRef);
if (objects.size() == 0) {
if ((pageNumber * pageSize) < total) {
pageNumber++;
continue;
}
break;

} catch (IOException e) {
throw new ConnectorIOException(String.format("Failed to call SmartHR list %s API", objectClass.getObjectClassValue()), e);
}
}
return total;

} else {
// Start from 1 in SmartHR
int pageNumber = pageInfo.initPage;
int total = 0;

for (T object : objects) {
if (!handler.handle(object)) {
for (int i = 0; i < pageInfo.times; i++) {
final int skipCount = i == 0 ? pageInfo.skipCount : 0;

try (Response response = get(endpointURL, params, pageNumber, pageSize)) {
if (response.code() != 200) {
ErrorResponse error = MAPPER.readValue(response.body().byteStream(), ErrorResponse.class);
throw new ConnectorIOException(String.format("Failed to get SmartHR %s. statusCode: %d, message: %s",
objectClass.getObjectClassValue(), response.code(), response.message()));
}

// Success
total = getTotalCount(response);

List<T> objects = MAPPER.readValue(response.body().byteStream(), valueTypeRef);
if (objects.size() == 0) {
break;
}
}

if (pageOffset > 0) {
// If requested pageOffset, don't process paging
int count = 0;
for (T object : objects) {
if (count < skipCount) {
count++;
continue;
}

if (!handler.handle(object)) {
break;
}
}

pageNumber = getPage(response);
pageSize = getPerPage(response);

if ((pageNumber * pageSize) < total) {
pageNumber++;
continue;
}

break;

} catch (IOException e) {
throw new ConnectorIOException(String.format("Failed to call SmartHR list %s API", objectClass.getObjectClassValue()), e);
}
}

page = getPage(response);
pageSize = getPerPage(response);
return total;
}
}

if ((page * pageSize) < totalCount) {
page++;
continue;
}
protected static class PageInfo {
public final int pageOffset;
public final int initPage;
public final int skipCount;
public final int times;

break;
public PageInfo(int pageOffset, int initPage, int skipCount, int times) {
this.pageOffset = pageOffset;
this.initPage = initPage;
this.skipCount = skipCount;
this.times = times;
}

} catch (IOException e) {
throw new ConnectorIOException(String.format("Failed to call SmartHR list %s API", objectClass.getObjectClassValue()), e);
}
public boolean isRequestedFullPage() {
return pageOffset == 0;
}
}

return totalCount;
protected static PageInfo newPageInfo(int pageOffset, int pageSize) {
// Convert Offset-based pagination to Page-based pagination
// Note: ConnId starts offset 1
if (pageOffset == 0) {
// Requested full page
return new PageInfo(pageOffset, 1, 0, -1);

} else if ((pageOffset + pageSize - 1) % pageSize == 0) {
int initPage = (pageOffset + pageSize - 1) / pageSize;
return new PageInfo(pageOffset, initPage, 0, 1);

} else {
int initPage = ((pageOffset + pageSize - 1) / pageSize);
int skipCount = pageOffset - ((initPage - 1) * pageSize) - 1;

return new PageInfo(pageOffset, initPage, skipCount, 2);
}
}

private int getPage(Response response) {
Expand Down

0 comments on commit 89118ce

Please sign in to comment.