11package 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 ;
36import org .nrg .framework .configuration .ConfigPaths ;
7+ import org .nrg .framework .services .SerializerService ;
48import org .nrg .prefs .annotations .NrgPreference ;
59import org .nrg .prefs .annotations .NrgPreferenceBean ;
610import org .nrg .prefs .beans .AbstractPreferenceBean ;
711import org .nrg .prefs .exceptions .InvalidPreferenceName ;
812import org .nrg .prefs .services .NrgPreferenceService ;
913import org .nrg .xft .exception .InvalidValueException ;
14+ import org .nrg .xsync .pojo .WhitelistSitePojo ;
1015import org .nrg .xsync .pojo .XsyncSitePreferencesPojo ;
16+ import org .nrg .xsync .services .local .WhitelistXsyncSiteService ;
1117import org .slf4j .Logger ;
1218import org .slf4j .LoggerFactory ;
1319import org .springframework .beans .factory .annotation .Autowired ;
20+ import org .springframework .core .io .ClassPathResource ;
1421import 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
2131@ NrgPreferenceBean (toolId = XsyncSitePreferencesBean .XSYNC_TOOL_ID , toolName = "XSync Site Preferences" )
2232public 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
0 commit comments