Skip to content

Commit cb306c2

Browse files
kosarkoOyvindLGjesdalkuchtiak-ufal
authored
Synchronize ufal and dataquest main branches (#939)
* remove duplicate dependency element with identical content (warnings in build) (cherry picked from commit 59cee27) * Issue 1165: Handle PaginantionException in Clarin licences search requests (ufal#1184) * Issue 1165: Handle PaginantionException in Clarin licences search requests * resolve MR comments --------- Co-authored-by: Milan Kuchtiak <mkuchtiak@Milans-MacBook-Pro.local> (cherry picked from commit 1778714) * Issue 1055: fixed security issue for downloading file with non anonymous license (ufal#1188) * Issue 1055: fixed security issue for downloading file with a license that requires authenticated user, also change ClarinLicense confirmation type from Integer to Enum (this reqired some refactoring) * fixed failing tests * add unit test for not authenticated user trying to get token for licence not allowed for anonymous user * inmprove unit test * resolve MR comments * typo: backwards compatibility -> backward compatibility --------- Co-authored-by: Milan Kuchtiak <mkuchtiak@Milans-MacBook-Pro.local> (cherry picked from commit 53e88cb) * Issue 1186: catch DSpaceBadRequestException rather than BadRequestException (ufal#1187) * Issue 1186: catch DSpaceBadRequestException rather than BadRequestException * resolve test failures * improve javadoc * resolve MR comment --------- Co-authored-by: Milan Kuchtiak <mkuchtiak@Milans-MacBook-Pro.local> Co-authored-by: Ondřej Košarko <kosarko@ufal.mff.cuni.cz> (cherry picked from commit 77fc4b0) * Issue ufal#1190 - fixed Server error (ufal#1193) * Issue 1190: fixed internal server error, when context.commit() was called multiple times in search request * Issue 1190: fixed internal server error, when context.commit() was called multiple times in search request * unnecessary whitespace * update unit test * small improvements --------- Co-authored-by: Milan Kuchtiak <mkuchtiak@Milans-MacBook-Pro.local> (cherry picked from commit 6638293) * Shortref v7 (ufal#1194) * Anonymous users should be able to use shortener * Update possible only with a valid token * updated handle returned with usable token additional cleanup - magicurl is an implementation detail and should not be serialized * checkstyle * createHandle would throw ConstraintViolation update the tests to use shortener.handle.prefix handle validation - subprefix has a default, should not be required * test blacklist/whitelist checkstyle and license * Address PR review comments doc + default (cherry picked from commit 2e3bd80) * Pull request review comment use a recent version of commons-lang * Shortener blacklist test - provide more guidance in the config file - change the default to match lindat prod --------- Co-authored-by: Øyvind Gjesdal <oyvind.gjesdal@uib.no> Co-authored-by: kuchtiak-ufal <kuchtiak@ufal.mff.cuni.cz>
1 parent 6daa0fd commit cb306c2

31 files changed

Lines changed: 767 additions & 212 deletions

dspace-api/pom.xml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -859,23 +859,6 @@
859859
</exclusions>
860860
</dependency>
861861

862-
<dependency>
863-
<groupId>io.findify</groupId>
864-
<artifactId>s3mock_2.13</artifactId>
865-
<version>0.2.6</version>
866-
<scope>test</scope>
867-
<exclusions>
868-
<exclusion>
869-
<groupId>com.amazonawsl</groupId>
870-
<artifactId>aws-java-sdk-s3</artifactId>
871-
</exclusion>
872-
<exclusion>
873-
<groupId>com.amazonaws</groupId>
874-
<artifactId>aws-java-sdk-s3</artifactId>
875-
</exclusion>
876-
</exclusions>
877-
</dependency>
878-
879862
</dependencies>
880863

881864
<dependencyManagement>

dspace-api/src/main/java/org/dspace/authorize/AuthorizationBitstreamUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
package org.dspace.authorize;
99

10+
import static org.dspace.content.clarin.ClarinLicense.Confirmation;
11+
1012
import java.sql.SQLException;
1113
import java.util.List;
1214
import java.util.Objects;
@@ -117,10 +119,10 @@ public boolean authorizeLicenseWithUser(Context context, UUID bitstreamID) throw
117119

118120
// Bitstream should have only one type of the Clarin license, so we could get first record
119121
ClarinLicense clarinLicense = Objects.requireNonNull(clarinLicenseResourceMappings.get(0)).getLicense();
120-
// 3 - Allow download for anonymous users, but with license confirmation
121-
// 0 - License confirmation is not required
122-
if (Objects.equals(clarinLicense.getConfirmation(), 3) ||
123-
Objects.equals(clarinLicense.getConfirmation(), 0)) {
122+
// ALLOW_ANONYMOUS - Allow download for anonymous users, but with license confirmation
123+
// NOT_REQUIRED - License confirmation is not required
124+
if ((clarinLicense.getConfirmation() == Confirmation.ALLOW_ANONYMOUS) ||
125+
(clarinLicense.getConfirmation() == Confirmation.NOT_REQUIRED)) {
124126
return true;
125127
}
126128
return false;

dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import javax.persistence.CascadeType;
1717
import javax.persistence.Column;
1818
import javax.persistence.Entity;
19+
import javax.persistence.EnumType;
20+
import javax.persistence.Enumerated;
1921
import javax.persistence.FetchType;
2022
import javax.persistence.GeneratedValue;
2123
import javax.persistence.GenerationType;
@@ -83,7 +85,8 @@ public class ClarinLicense implements ReloadableEntity<Integer> {
8385
private String definition = null;
8486

8587
@Column(name = "confirmation")
86-
private Integer confirmation = 0;
88+
@Enumerated(EnumType.ORDINAL)
89+
private Confirmation confirmation = Confirmation.NOT_REQUIRED;
8790

8891
@Column(name = "required_info")
8992
private String requiredInfo = null;
@@ -111,11 +114,11 @@ public void setDefinition(String definition) {
111114
this.definition = definition;
112115
}
113116

114-
public Integer getConfirmation() {
115-
return confirmation;
117+
public Confirmation getConfirmation() {
118+
return confirmation == null ? Confirmation.NOT_REQUIRED : confirmation;
116119
}
117120

118-
public void setConfirmation(Integer confirmation) {
121+
public void setConfirmation(Confirmation confirmation) {
119122
this.confirmation = confirmation;
120123
}
121124

@@ -191,4 +194,29 @@ public ClarinUserRegistration getEperson() {
191194
public void setEperson(ClarinUserRegistration eperson) {
192195
this.eperson = eperson;
193196
}
197+
198+
public enum Confirmation {
199+
200+
// if new Confirmation value is needed, add it to the end of this list, to not break the backward compatibility
201+
NOT_REQUIRED(0), ASK_ONLY_ONCE(1), ASK_ALWAYS(2), ALLOW_ANONYMOUS(3);
202+
203+
private final int value;
204+
205+
Confirmation(int value) {
206+
this.value = value;
207+
}
208+
209+
public int getValue() {
210+
return value;
211+
}
212+
213+
public static Confirmation getConfirmation(int value) {
214+
return Arrays.stream(values())
215+
.filter(v -> (v.getValue() == value))
216+
.findFirst()
217+
.orElseThrow(() ->
218+
new IllegalArgumentException("No license confirmation found for value: " + value));
219+
}
220+
221+
}
194222
}

dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
package org.dspace.content.clarin;
99

10+
import static org.dspace.content.clarin.ClarinLicense.Confirmation;
11+
1012
import java.sql.SQLException;
1113
import java.util.ArrayList;
1214
import java.util.List;
@@ -203,23 +205,23 @@ public ClarinLicense getLicenseToAgree(Context context, UUID userId, UUID resour
203205
}
204206

205207
// Confirmation states:
206-
// 0 - Not required
207-
// 1 - Ask only once
208-
// 2 - Ask always
209-
// 3 - Allow anonymous
210-
if (Objects.equals(clarinLicenseToAgree.getConfirmation(), 0)) {
208+
// NOT_REQUIRED
209+
// ASK_ONLY_ONCE
210+
// ASK_ALWAYS
211+
// ALLOW_ANONYMOUS
212+
if (clarinLicenseToAgree.getConfirmation() == Confirmation.NOT_REQUIRED) {
211213
return null;
212214
}
213215

214216
switch (clarinLicenseToAgree.getConfirmation()) {
215-
case 1:
217+
case ASK_ONLY_ONCE:
216218
// Ask only once - check if the clarin license required info is filled in by the user
217219
if (userFilledInRequiredInfo(context, clarinLicenseResourceMapping, userId)) {
218220
return null;
219221
}
220222
return clarinLicenseToAgree;
221-
case 2:
222-
case 3:
223+
case ASK_ALWAYS:
224+
case ALLOW_ANONYMOUS:
223225
return clarinLicenseToAgree;
224226
default:
225227
return null;

dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ public DSpaceObject resolveToObject(Context context, String handle) throws Illeg
337337
+ Constants.typeText[handleTypeId]);
338338
}
339339

340+
@Override
341+
public int count(Context context) throws SQLException {
342+
return handleDAO.countRows(context);
343+
}
344+
340345
/**
341346
* Create id for handle object.
342347
*
@@ -457,6 +462,21 @@ public Handle createHandle(Context context, String handleStr) throws SQLExceptio
457462
return handle;
458463
}
459464

465+
@Override
466+
public Handle findByHandleAndMagicToken(Context context, String handle, String token) throws SQLException {
467+
Handle h = findByHandle(context, handle);
468+
if (Objects.isNull(h) || Objects.isNull(h.getUrl()) || !h.getUrl().contains(MAGIC_BEAN)) {
469+
return null;
470+
}
471+
org.dspace.handle.external.Handle magicHandle =
472+
new org.dspace.handle.external.Handle(h.getHandle(), h.getUrl());
473+
if (magicHandle.token.equals(token)) {
474+
return h;
475+
} else {
476+
return null;
477+
}
478+
}
479+
460480
/**
461481
* Strips the part identifier from the handle
462482
*

dspace-api/src/main/java/org/dspace/handle/external/Handle.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ public Handle(String handle, String magicURL) {
8787
}
8888

8989
/**
90-
* From the attributes generate the url with `@magicLindat` string
90+
* Generate new token and combine the properties into the url with `@magicLindat` string
9191
* @return url with the `@magicLindat` string
9292
*/
93-
public String getMagicUrl() {
94-
return this.getMagicUrl(this.title, this.submitdate, this.reportemail, this.datasetName, this.datasetVersion,
93+
public String generateMagicUrl() {
94+
return generateMagicUrl(this.title, this.submitdate, this.reportemail, this.datasetName, this.datasetVersion,
9595
this.query, this.url);
9696
}
9797

9898
/**
99-
* From the attributes generate the url with `@magicLindat` string
99+
* Generate new token and combine the params into the url with `@magicLindat` string
100100
* @return url with the `@magicLindat` string
101101
*/
102-
public String getMagicUrl(String title, String submitdate, String reportemail, String datasetName,
103-
String datasetVersion, String query, String url) {
102+
private static String generateMagicUrl(String title, String submitdate, String reportemail, String datasetName,
103+
String datasetVersion, String query, String url) {
104104
String magicURL = "";
105105
String token = UUID.randomUUID().toString();
106106
String[] magicURLProps = new String[] {title, HandlePlugin.getRepositoryName(), submitdate, reportemail,

dspace-api/src/main/java/org/dspace/handle/service/HandleClarinService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ public void update(Context context, Handle handleObject, String newHandle,
156156
*/
157157
public DSpaceObject resolveToObject(Context context, String handle) throws IllegalStateException, SQLException;
158158

159+
/**
160+
* Return the number of entries in the handle table.
161+
* @param context
162+
* @return number of rows in the handle table
163+
* @throws SQLException
164+
*/
165+
int count(Context context) throws SQLException;
166+
159167
/**
160168
* Create the external handles from the list of handles with magic URL
161169
*
@@ -226,4 +234,15 @@ public void update(Context context, Handle handleObject, String newHandle,
226234
* @throws AuthorizeException if authorization error
227235
*/
228236
public Handle createHandle(Context context, String handle) throws SQLException, AuthorizeException;
237+
238+
/**
239+
* Returns a handle entity matching the provided `prefix/suffix` but only when the "magic url"
240+
* contains the provided token.
241+
* @param context
242+
* @param handle prefix/suffix
243+
* @param token the automatically generated part of the magic URL
244+
* @return Handle entity or null (if the handle is not found or the "magic url" does not contain the provided token)
245+
* @throws SQLException
246+
*/
247+
Handle findByHandleAndMagicToken(Context context, String handle, String token) throws SQLException;
229248
}

dspace-api/src/test/java/org/dspace/content/BundleClarinTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package org.dspace.content;
99

10+
import static org.dspace.content.clarin.ClarinLicense.Confirmation;
1011
import static org.dspace.core.Constants.CONTENT_BUNDLE_NAME;
1112
import static org.hamcrest.CoreMatchers.equalTo;
1213
import static org.hamcrest.CoreMatchers.instanceOf;
@@ -119,7 +120,7 @@ public void init() {
119120
this.clarinLicense.setLicenseLabels(cllSet);
120121
this.clarinLicense.setName(LICENSE_NAME);
121122
this.clarinLicense.setDefinition(LICENSE_URI);
122-
this.clarinLicense.setConfirmation(0);
123+
this.clarinLicense.setConfirmation(Confirmation.NOT_REQUIRED);
123124
this.clarinLicenseService.update(context, this.clarinLicense);
124125

125126
// initialize second clarin license and clarin license label
@@ -139,7 +140,7 @@ public void init() {
139140
this.secondClarinLicense.setLicenseLabels(secondCllSet);
140141
this.secondClarinLicense.setName("wrong name");
141142
this.secondClarinLicense.setDefinition("wrong uri");
142-
this.secondClarinLicense.setConfirmation(0);
143+
this.secondClarinLicense.setConfirmation(Confirmation.NOT_REQUIRED);
143144
this.clarinLicenseService.update(context, this.secondClarinLicense);
144145

145146
//we need to commit the changes, so we don't block the table for testing

dspace-server-webapp/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,6 @@
290290
<version>${spring-boot.version}</version>
291291
</dependency>
292292

293-
<dependency>
294-
<groupId>org.springframework.boot</groupId>
295-
<artifactId>spring-boot-starter-actuator</artifactId>
296-
<version>${spring-boot.version}</version>
297-
</dependency>
298-
299293
<dependency>
300294
<groupId>com.flipkart.zjsonpatch</groupId>
301295
<artifactId>zjsonpatch</artifactId>

dspace-server-webapp/src/main/java/org/dspace/app/rest/ItemAddBundleController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
import java.util.UUID;
1717
import javax.servlet.http.HttpServletRequest;
1818
import javax.servlet.http.HttpServletResponse;
19-
import javax.ws.rs.BadRequestException;
2019

2120
import com.fasterxml.jackson.databind.ObjectMapper;
2221
import org.dspace.app.rest.converter.ConverterService;
2322
import org.dspace.app.rest.converter.MetadataConverter;
23+
import org.dspace.app.rest.exception.DSpaceBadRequestException;
2424
import org.dspace.app.rest.exception.UnprocessableEntityException;
2525
import org.dspace.app.rest.model.BundleRest;
2626
import org.dspace.app.rest.model.ItemRest;
@@ -156,7 +156,7 @@ public ItemRest updateLicenseForBundle(@PathVariable UUID uuid,
156156
throws SQLException, AuthorizeException {
157157
Context context = ContextUtil.obtainContext(request);
158158
if (Objects.isNull(context)) {
159-
throw new BadRequestException("No context found for current user");
159+
throw new DSpaceBadRequestException("No context found for current user");
160160
}
161161
Item item = itemService.find(context, uuid);
162162

0 commit comments

Comments
 (0)