If the following dependency is added to your web application and if it is properly configured as described below, the weblibrary provides you with Spring Beans to access UGC. The main ones are S3ResultWriter and VotingReportService.
<dependency>
<groupId>com.subshell.sophora.ugc</groupId>
<artifactId>ugc-weblib</artifactId>
<version>X.X.X</version>
</dependency>
The weblibrary provides you with Spring Beans for you application.
ResultWriter
The S3ResultWriter provides you with the ability to transfer user generated data to the UGC backend without loss of data. The data is written into a folder structure of a S3 bucket where it is discovered and handled by the UGC backend. The data can be one of the following types:
- FormResult
- VotingResult
- QuizResult
- UserComment
- ImageUpload
- UserRating
S3ResultWriter see its javadoc.
It needs a S3Configuration with the following fields:
| Attributes | Description |
|---|---|
| sophora.ugc.weblib.s3.accessKeyId | Access key for your S3 bucket |
| sophora.ugc.weblib.s3.secretAccessKey | Secret key, usually bundled with the previous access key |
| sophora.ugc.weblib.s3.host | Host for the S3 bucket, e.g. "storage.googleapis.com" |
| sophora.ugc.weblib.s3.bucketName | Name of the bucket in which UGC submissions are stored |
A typical usage to the S3ResultWriter would look like this:
public void saveForm(FormResult formResult) {
try {
boolean success = s3ResultWriter.writeResult(formResult);
if (success) {
log.info("Successfully wrote form data to S3");
} else {
log.error("Failed to write form data to S3");
}
} catch (Exception e) {
log.error("Error while writing to S3", e);
}
}
VotingReportService
The VotingReportService can be used to access the RankingVotingReports and SimpleVotingReports of votings. For both, it needs the Sophora IDs of the corresponding votings. Caller of the methods do not need to bother about number of requests. An internal cache ensures that requests only call the UGC backend if the last retrieval is more than the cache time in the past. Call one of the following two methods based on the type of voting represented by the Sophora ID.
RankingVotingReport rankingVotingReport = votingReportService.getRankingReport("vote100");
SimpleVotingReport simpleVotingReport = votingReportService.getSimpleReport("vote102");
The URL to access UGC needs to be configured and ist available by an Spring Bean of type WeblibConfiguration. Additionally, there is an optional configuration option to specify a cache time used by the VotingReportService.
| Attributes | Description |
|---|---|
| sophora.ugc.weblib.ugcBaseUrl | URL that points to the location of UGC, including the HTTP protocol |
| sophora.ugc.weblib.voting.cacheTime | Duration for which a cached report is returned. After the duration is elapsed the next request will call the UGC backend to get the report. Default PT10S |