package de.cinovo.cloudconductor.agent.jobs;

import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import de.cinovo.cloudconductor.agent.AgentState;
import de.cinovo.cloudconductor.agent.helper.JWTHelper;
import de.cinovo.cloudconductor.agent.helper.ServerCom;
import de.cinovo.cloudconductor.agent.tasks.SchedulerService;
import de.cinovo.cloudconductor.api.lib.exceptions.CloudConductorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

/**
 * 
 * Copyright 2017 Cinovo AG<br>
 * <br>
 * 
 * @author mweise
 *
 */
public class RefreshJWTJob implements AgentJob {
	
	/**
	 * the job name, used by the scheduler
	 */
	public static final String JOB_NAME = "REFRESH_JWT_JOB";
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RefreshJWTJob.class);
	
	
	@Override
	public void run() {
		RefreshJWTJob.LOGGER.debug("Start RefreshJWTJob");
		
		String newJWT = null;
		long period = 1000 * 60;
		try {
			newJWT = ServerCom.getJWT();
			
			if (newJWT != null) {
				JWTClaimsSet claimsSet = SignedJWT.parse(newJWT).getJWTClaimsSet();
				AgentState.info().setJWT(newJWT);
				
				period = JWTHelper.calcNextRefreshInMillis(claimsSet);
				RefreshJWTJob.LOGGER.debug("Authentication successful!");
			} else {
				RefreshJWTJob.LOGGER.error("Authentication failed: Missing JWT!");
				throw new CloudConductorException("Missing JWT!");
			}
		} catch (CloudConductorException e) {
			RefreshJWTJob.LOGGER.error("Error refreshing JWT: ", e);
			AgentState.info().setJWT(null);
		} catch (ParseException e) {
			RefreshJWTJob.LOGGER.error("Error parsing new JWT '" + newJWT + "': ", e);
			AgentState.info().setJWT(null);
		} finally {
			SchedulerService.instance.executeOnce(new RefreshJWTJob(), period, TimeUnit.MILLISECONDS);
			RefreshJWTJob.LOGGER.debug("Scheduled next refresh of JWT in " + period + " ms");
			RefreshJWTJob.LOGGER.debug("Finished RefreshJWTJob");
		}
	}
	
	@Override
	public String getJobIdentifier() {
		return RefreshJWTJob.JOB_NAME;
	}
	
	@Override
	public boolean isDefaultStart() {
		return false;
	}
	
	@Override
	public long defaultStartTimer() {
		return 30;
	}
	
	@Override
	public TimeUnit defaultStartTimerUnit() {
		return TimeUnit.MINUTES;
	}
	
}

@@ -546,20 +546,16 @@
 t;%0A%0A/**%0A
- * %0A
  * Copyr
@@ -588,17 +588,16 @@
  %3Cbr%3E%0A *
- 
 %0A * @aut
@@ -607,19 +607,16 @@
  mweise%0A
- *%0A
  */%0Apubl
@@ -660,17 +660,16 @@
 ntJob %7B%0A
-%09
 %0A%09/**%0A%09 
@@ -769,17 +769,16 @@
 T_JOB%22;%0A
-%09
 %0A%09privat
@@ -855,18 +855,103 @@
 lass);%0A%09
+private final long defaultPeriod = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);
 %0A
-%09
 %0A%09@Overr
@@ -1032,60 +1032,77 @@
 %22);%0A
-%09%09
 %0A%09%09
-String newJWT = null;%0A%09%09long period = 1000 * 60
+long period = this.defaultPeriod;%0A%09%09AgentState.info().setJWT(null)
 ;%0A%09%09
@@ -1110,16 +1110,23 @@
 ry %7B%0A%09%09%09
+String 
 newJWT =
@@ -1150,18 +1150,13 @@
 ();%0A
-%09%09%09%0A
 %09%09%09if
- 
 (new
@@ -1284,13 +1284,8 @@
 T);%0A
-%09%09%09%09%0A
 %09%09%09%09
@@ -1553,17 +1553,16 @@
 %09%7D catch
- 
 (CloudCo
@@ -1647,43 +1647,8 @@
 e);%0A
-%09%09%09AgentState.info().setJWT(null);%0A
 %09%09%7D 
@@ -1652,17 +1652,16 @@
 %09%7D catch
- 
 (ParseEx
@@ -1729,66 +1729,14 @@
  JWT
- '%22 + newJWT + %22': %22, e);%0A%09%09%09AgentState.info().setJWT(null
+: %22, e
 );%0A%09
@@ -1842,16 +1842,168 @@
 CONDS);%0A
+%09%09%09if(period == this.defaultPeriod) %7B%0A%09%09%09%09RefreshJWTJob.LOGGER.warn(%22Scheduled next refresh of JWT in default period time! Something is wrong. %22);%0A%09%09%09%7D%0A
 %09%09%09Refre
@@ -2139,25 +2139,24 @@
 b%22);%0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09@Override%0A
@@ -2223,25 +2223,24 @@
 OB_NAME;%0A%09%7D%0A
-%09
 %0A%09@Override%0A
@@ -2289,25 +2289,24 @@
 n false;%0A%09%7D%0A
-%09
 %0A%09@Override%0A
@@ -2356,17 +2356,16 @@
  30;%0A%09%7D%0A
-%09
 %0A%09@Overr
@@ -2441,12 +2441,11 @@
 TES;%0A%09%7D%0A
-%09
 %0A%7D%0A

Fix JWT refresh job.