Skip to content

Commit

Permalink
SHIBUI-2571
Browse files Browse the repository at this point in the history
Beacon gathering and sending
  • Loading branch information
chasegawa committed Jun 2, 2023
1 parent ed2d206 commit e63884f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
import org.springframework.web.util.UrlPathHelper;

import javax.sql.DataSource;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@SpringBootConfiguration
Expand Down Expand Up @@ -277,4 +282,13 @@ public IBeaconDataService getBeaconDataService(@Value("${shibui.beacon.productNa
BeaconDataServiceImpl result = new BeaconDataServiceImpl(productName, info, tierVersion, entityDescriptorRepository, metadataResolverRepository, filterRepository, groupsRepository, roleRepository, beaconConfigurationRepository, userService);
return result;
}

@Bean
public List<URL> beaconEndpointUrl(@Value("${shibui.beacon.url}") String urls) throws MalformedURLException {
List<URL> result = new ArrayList<>();
for (String url : Arrays.asList(urls.split(","))) {
result.add(new URL(url));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package edu.internet2.tier.shibboleth.admin.ui.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import edu.internet2.tier.shibboleth.admin.ui.scheduled.BeaconReportingTask;
import edu.internet2.tier.shibboleth.admin.ui.service.IBeaconDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Profile({"dev", "very-dangerous"})
@RestController
@RequestMapping(value = "/api/beacon")
public class BeaconController {
@Autowired
BeaconReportingTask beaconReporter;

@Autowired
private IBeaconDataService service;


@GetMapping(value = "/detail")
public ResponseEntity<?> getDetail() throws JsonProcessingException {
return ResponseEntity.ok(service.getBeaconData());
}

@PostMapping("/send")
public ResponseEntity<?> forceSendBeaconData() {
beaconReporter.sendBeaconData();
return ResponseEntity.ok("Manual push of beacon data completed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,48 @@
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

@Configuration
@ConditionalOnProperty(name = "shibui.beacon.enabled", matchIfMissing=true)
@EnableSchedulerLock(defaultLockAtMostFor = "${shibui.maxTask.lockTime:30m}")
public class BeaconReportingTask {
@Autowired
IBeaconDataService dataService;

@Autowired
@Qualifier("beaconEndpointUrl")
private List<URL> endpointUrls;

@Scheduled(cron="#{@getBeaconCronValue}")
@SchedulerLock(name = "generateEntityDescriptorFiles")
@SchedulerLock(name = "sendBeaconData")
@Transactional(readOnly = true)
public void generateEntityDescriptorFiles() {
public void sendBeaconData() {
endpointUrls.forEach(url -> {
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
try(OutputStream os = con.getOutputStream()){
byte[] input = dataService.getBeaconData().getBytes("utf-8");
os.write(input, 0, input.length);
}
} catch (IOException e) {
e.printStackTrace();
}
});

}
}
1 change: 1 addition & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ shibui.pac4j-enabled=false
shibui.beacon.enabled=true
shibui.beacon.productName=ShibUi
shibui.beacon.installationID=UNICON-SHIBUI-TESTING
shibui.beacon.url=http://collector.testbed.tier.internet2.edu:5001

### Swagger/Springdoc patterns
springdoc.use-management-port=true
Expand Down

0 comments on commit e63884f

Please sign in to comment.