Skip to content

Commit 7259425

Browse files
PLUGINS-229 Adding a whitelist service for receiving xsync sites as well as some front end cleanup
1 parent a44ebcc commit 7259425

18 files changed

Lines changed: 1290 additions & 451 deletions

File tree

src/main/java/org/nrg/xsync/components/XsyncSitePreferencesBean.java

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package org.nrg.xsync.components;
22

3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
36
import org.nrg.framework.configuration.ConfigPaths;
7+
import org.nrg.framework.services.SerializerService;
48
import org.nrg.prefs.annotations.NrgPreference;
59
import org.nrg.prefs.annotations.NrgPreferenceBean;
610
import org.nrg.prefs.beans.AbstractPreferenceBean;
711
import org.nrg.prefs.exceptions.InvalidPreferenceName;
812
import org.nrg.prefs.services.NrgPreferenceService;
913
import org.nrg.xft.exception.InvalidValueException;
14+
import org.nrg.xsync.pojo.WhitelistSitePojo;
1015
import org.nrg.xsync.pojo.XsyncSitePreferencesPojo;
16+
import org.nrg.xsync.services.local.WhitelistXsyncSiteService;
1117
import org.slf4j.Logger;
1218
import org.slf4j.LoggerFactory;
1319
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.core.io.ClassPathResource;
1421
import org.springframework.stereotype.Component;
1522

23+
import java.io.IOException;
24+
import java.util.List;
25+
1626
/**
1727
* The Class XsyncSitePreferencesBean.
1828
* @author Mike Hodge
@@ -21,13 +31,16 @@
2131
@NrgPreferenceBean(toolId = XsyncSitePreferencesBean.XSYNC_TOOL_ID, toolName = "XSync Site Preferences")
2232
public class XsyncSitePreferencesBean extends AbstractPreferenceBean {
2333
@Autowired
24-
public XsyncSitePreferencesBean(final NrgPreferenceService preferenceService, final ConfigPaths configFolderPaths) {
34+
public XsyncSitePreferencesBean(final NrgPreferenceService preferenceService, final ConfigPaths configFolderPaths, SerializerService serializerService, WhitelistXsyncSiteService whitelistXsyncSitesService) {
2535
super(preferenceService, configFolderPaths);
36+
this.serializerService = serializerService;
37+
this.whitelistXsyncSitesService = whitelistXsyncSitesService;
38+
addInitialWhitelistSitesToPreferences();
2639
}
2740

2841
/** The Constant _log. */
2942
private static final Logger _log = LoggerFactory.getLogger(XsyncSitePreferencesBean.class);
30-
43+
3144
/** The Constant XSYNC_TOOL_ID. */
3245
static final String XSYNC_TOOL_ID = "xsync";
3346

@@ -42,11 +55,16 @@ public XsyncSitePreferencesBean(final NrgPreferenceService preferenceService, fi
4255
private static final String DEFAULT_SYNC_RETRY_COUNT = "2";
4356

4457
private static final String DEFAULT_SYNC_MAX_UNCOMPRESSED_FILESIZE = "-1" ; //All files no limits
45-
4658

47-
public XsyncSitePreferencesBean(final NrgPreferenceService preferenceService) {
59+
final SerializerService serializerService;
60+
final WhitelistXsyncSiteService whitelistXsyncSitesService;
61+
62+
public XsyncSitePreferencesBean(final NrgPreferenceService preferenceService, SerializerService serializerService, WhitelistXsyncSiteService whitelistXsyncSitesService) {
4863
super(preferenceService);
49-
}
64+
this.serializerService = serializerService;
65+
this.whitelistXsyncSitesService = whitelistXsyncSitesService;
66+
addInitialWhitelistSitesToPreferences();
67+
}
5068

5169
/**
5270
* Gets the token refresh interval.
@@ -73,6 +91,20 @@ public String getSyncMaxUncompressedZipFileSize() {
7391
return getValue("syncMaxUncompressedZipFileSize");
7492
}
7593

94+
@NrgPreference(defaultValue = "false")
95+
public boolean getXsyncWhitelistEnabled() {
96+
return getBooleanValue("xsyncWhitelistEnabled");
97+
}
98+
99+
public void setXsyncWhitelistEnabled(boolean xsyncWhitelistEnabled) {
100+
try {
101+
set(String.valueOf(xsyncWhitelistEnabled), "xsyncWhitelistEnabled");
102+
} catch (InvalidPreferenceName invalidPreferenceName) {
103+
_log.error("Invalid preference name: xsyncWhitelistEnabled");
104+
}
105+
}
106+
107+
76108
/**
77109
* Sets the Max. Total Uncompressed File Size
78110
*
@@ -84,7 +116,7 @@ public void setSyncMaxUncompressedZipFileSize(final String syncMaxUncompressedZi
84116
try {
85117
set(syncMaxUncompressedZipFileSize,"syncMaxUncompressedZipFileSize");
86118
} catch (InvalidPreferenceName invalidPreferenceName) {
87-
_log.error("Invalid preference name");
119+
_log.error("Invalid preference name: syncMaxUncompressedZipFileSize");
88120
}
89121
}
90122

@@ -117,7 +149,7 @@ public void setTokenRefreshInterval(final String tokenRefreshInterval) throws In
117149
calculateIntervalInMillis(tokenRefreshInterval);
118150
set(tokenRefreshInterval,"tokenRefreshInterval");
119151
} catch (InvalidPreferenceName invalidPreferenceName) {
120-
_log.error("Invalid preference name");
152+
_log.error("Invalid preference name: tokenRefreshInterval");
121153
}
122154
}
123155

@@ -150,7 +182,7 @@ public void setSyncRetryInterval(final String syncRetryInterval) throws InvalidV
150182
calculateIntervalInMillis(syncRetryInterval);
151183
set(syncRetryInterval,"syncRetryInterval");
152184
} catch (InvalidPreferenceName invalidPreferenceName) {
153-
_log.error("Invalid preference name");
185+
_log.error("Invalid preference name: syncRetryInterval");
154186
}
155187
}
156188

@@ -182,7 +214,7 @@ public void setSyncRetryCount(final String syncRetryCount) throws InvalidValueEx
182214
try {
183215
set(syncRetryCount, "syncRetryCount");
184216
} catch (InvalidPreferenceName invalidPreferenceName) {
185-
_log.error("Invalid preference name");
217+
_log.error("Invalid preference name: syncRetryCount");
186218
}
187219
}
188220

@@ -212,24 +244,41 @@ public int getSyncRetryCountInt() {
212244
* @throws InvalidValueException the invalid value exception
213245
*/
214246
private static long calculateIntervalInMillis(final String intervalStr) throws InvalidValueException {
215-
final String[] intervalArr = intervalStr.split("[\\s]+");
216-
final long minIntervalMilis = 300000;
247+
final String[] intervalArr = intervalStr.split("[\\s]+");
248+
final long minIntervalMilis = 300000;
249+
250+
if (intervalArr.length==2) {
251+
final long intervalNum = Long.valueOf(intervalArr[0]);
252+
Long interval = null;
253+
if (intervalArr[1].toLowerCase().contains("hour")) {
254+
interval = intervalNum*1000*60*60;
255+
} else if (intervalArr[1].toLowerCase().contains("minute")) {
256+
interval = intervalNum*1000*60;
257+
}
258+
if (interval != null && interval>=minIntervalMilis) {
259+
return interval;
260+
} else {
261+
throw new InvalidValueException("XSync - Interval too short - Specify minimum of " + minIntervalMilis*1000*60 + " minutes.");
262+
}
263+
}else
264+
throw new InvalidValueException("XSync - Invalid interval specified - " + intervalStr);
265+
}
266+
267+
private void addInitialWhitelistSitesToPreferences() {
268+
try {
269+
ClassPathResource resource = new ClassPathResource("META-INF/xnat/xsyncSiteWhitelist.json");
270+
JsonNode rootNode = serializerService.deserializeJson(resource.getInputStream());
217271

218-
if (intervalArr.length==2) {
219-
final long intervalNum = Long.valueOf(intervalArr[0]);
220-
Long interval = null;
221-
if (intervalArr[1].toLowerCase().contains("hour")) {
222-
interval = intervalNum*1000*60*60;
223-
} else if (intervalArr[1].toLowerCase().contains("minute")) {
224-
interval = intervalNum*1000*60;
225-
}
226-
if (interval != null && interval>=minIntervalMilis) {
227-
return interval;
228-
} else {
229-
throw new InvalidValueException("XSync - Interval too short - Specify minimum of " + minIntervalMilis*1000*60 + " minutes.");
230-
}
231-
}else
232-
throw new InvalidValueException("XSync - Invalid interval specified - " + intervalStr);
272+
if (rootNode.has("allowedSites")) {
273+
List<WhitelistSitePojo> whitelistedSites = serializerService.getObjectMapper()
274+
.convertValue(rootNode.get("allowedSites"), new TypeReference<>() {});
275+
whitelistXsyncSitesService.addWhiteListSitesFromJson(whitelistedSites);
276+
} else {
277+
_log.info("Whitelist json does not have proper formatting.");
278+
}
279+
} catch (IOException e) {
280+
_log.info("No xsync whitelist json provided at startup.");
281+
}
233282
}
234283

235284
public void update(final XsyncSitePreferencesPojo xsyncSitePreferencesPojo) throws InvalidValueException {
@@ -245,14 +294,18 @@ public void update(final XsyncSitePreferencesPojo xsyncSitePreferencesPojo) thro
245294
if (null != xsyncSitePreferencesPojo.getSyncMaxUncompressedZipFileSize()) {
246295
this.setSyncMaxUncompressedZipFileSize(xsyncSitePreferencesPojo.getSyncMaxUncompressedZipFileSize());
247296
}
297+
if (null != xsyncSitePreferencesPojo.getXsyncWhitelistEnabled()) {
298+
this.setXsyncWhitelistEnabled(xsyncSitePreferencesPojo.getXsyncWhitelistEnabled());
299+
}
248300
}
249301

250302
public XsyncSitePreferencesPojo toPojo() {
251303
return new XsyncSitePreferencesPojo(
252304
getTokenRefreshInterval(),
253305
getSyncRetryInterval(),
254306
getSyncRetryCount(),
255-
getSyncMaxUncompressedZipFileSize()
307+
getSyncMaxUncompressedZipFileSize(),
308+
getXsyncWhitelistEnabled()
256309
);
257310
}
258311

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.nrg.xsync.manifest.whitelist;
2+
3+
public enum SiteClassification {
4+
PUBLIC, RESEARCH, CLINICAL
5+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.nrg.xsync.manifest.whitelist;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import org.nrg.framework.orm.hibernate.AbstractHibernateEntity;
8+
9+
import javax.persistence.Column;
10+
import javax.persistence.Entity;
11+
12+
@Entity
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
@Getter
16+
@Setter
17+
public class WhitelistSite extends AbstractHibernateEntity {
18+
19+
@Column(name = "siteId", unique = true, nullable = false)
20+
private String siteId;
21+
22+
@Column(name = "siteName", unique = true, nullable = false)
23+
private String siteName;
24+
25+
@Column(name = "siteUrl", unique = true, nullable = false)
26+
private String siteUrl;
27+
28+
private SiteClassification classification;
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.nrg.xsync.manifest.whitelist;
2+
3+
import org.nrg.framework.orm.hibernate.AbstractHibernateDAO;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public class WhitelistSiteRepository extends AbstractHibernateDAO<WhitelistSite> {
8+
9+
public WhitelistSite findWhitelistSiteBySiteId(String siteId) {
10+
return findByUniqueProperty("siteId", siteId);
11+
}
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.nrg.xsync.pojo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
@Getter
11+
@Setter
12+
public class WhitelistSitePojo {
13+
private String siteId;
14+
private String siteName;
15+
private String siteUrl;
16+
private String classification;
17+
}

src/main/java/org/nrg/xsync/pojo/XsyncSitePreferencesPojo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ public XsyncSitePreferencesPojo() {}
1414
public XsyncSitePreferencesPojo(final String tokenRefreshInterval,
1515
final String syncRetryInterval,
1616
final String syncRetryCount,
17-
final String syncMaxUncompressedZipFileSize) {
17+
final String syncMaxUncompressedZipFileSize,
18+
final Boolean xsyncWhitelistEnabled) {
1819
this.tokenRefreshInterval = tokenRefreshInterval;
1920
this.syncRetryInterval = syncRetryInterval;
2021
this.syncRetryCount = syncRetryCount;
2122
this.syncMaxUncompressedZipFileSize = syncMaxUncompressedZipFileSize;
23+
this.xsyncWhitelistEnabled = xsyncWhitelistEnabled;
2224
}
2325

2426
private String tokenRefreshInterval;
2527
private String syncRetryInterval;
2628
private String syncRetryCount;
2729
private String syncMaxUncompressedZipFileSize;
30+
private Boolean xsyncWhitelistEnabled;
2831
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.nrg.xsync.services.local;
2+
3+
import org.nrg.xapi.exceptions.DataFormatException;
4+
import org.nrg.xapi.exceptions.NotFoundException;
5+
import org.nrg.xsync.pojo.WhitelistSitePojo;
6+
7+
import java.util.List;
8+
9+
public interface WhitelistXsyncSiteService {
10+
11+
List<WhitelistSitePojo> getAllWhitelistedSites();
12+
13+
void addWhiteListSitesFromJson(List<WhitelistSitePojo> whitelistSitePojos);
14+
15+
List<WhitelistSitePojo> addOrUpdateWhitelistSiteFromSiteAdmin(WhitelistSitePojo whitelistSitePojo) throws DataFormatException;
16+
17+
List<WhitelistSitePojo> deleteWhitelistSiteFromSiteAdmin(WhitelistSitePojo whitelistSitePojo) throws NotFoundException;
18+
}

0 commit comments

Comments
 (0)