Skip to content

Commit

Permalink
Merge pull request #498 from shimono/feature-483
Browse files Browse the repository at this point in the history
Fix #483 #484 If-Match when resource does not exists and If-None-Match when it exists.
  • Loading branch information
tochi-y authored Nov 1, 2019
2 parents 8681808 + 97b6f40 commit 0f092f0
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
<version>1.19</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/personium/core/PersoniumCoreException.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ public static class Dav {
* When "F" is specified in the Overwrite header but the resource of the destination already exists.
*/
public static final PersoniumCoreException DESTINATION_ALREADY_EXISTS = create("PR412-DV-0002");
/**
* No Entity is matching.
*/
public static final PersoniumCoreException NO_ENTITY_MATCH = create("PR412-DV-0003");
/**
* The Etag of the corresponding resource match.
*/
public static final PersoniumCoreException ETAG_MATCH = create("PR412-DV-0004");
/**
* Range header specification error.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/personium/core/rs/box/DavFileResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public DavFileResource(final DavRsCmp parent, final DavCmp davCmp) {
@PUT
public Response put(@HeaderParam(HttpHeaders.CONTENT_TYPE) final String contentType,
@HeaderParam(HttpHeaders.IF_MATCH) final String ifMatch,
@HeaderParam(HttpHeaders.IF_NONE_MATCH) final String ifNoneMatch,
final InputStream inputStream) {
if ("*".equals(ifNoneMatch)) {
throw PersoniumCoreException.Dav.ETAG_MATCH;
}
// Access Control
this.davRsCmp.checkAccessContext(BoxPrivilege.WRITE_CONTENT);

Expand Down
22 changes: 21 additions & 1 deletion src/main/java/io/personium/core/rs/box/NullResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public final Response get() {
@PUT
public final Response put(
@HeaderParam(HttpHeaders.CONTENT_TYPE) final String contentType,
@HeaderParam(HttpHeaders.IF_MATCH) final String ifMatch,
final InputStream inputStream) {

//Access control
Expand All @@ -130,6 +131,16 @@ public final Response put(
throw PersoniumCoreException.Dav.HAS_NOT_PARENT.params(this.davRsCmp.getParent().getUrl());
}

// If If-Match: * is specified, then should return 412
// https://tools.ietf.org/html/rfc7232#section-3.1
// If the field-value is "*", the condition is false
// if the origin server does not have a current representation
// for the target resource.
// Interpretation: if any value is specified then should evaluated as false
if (ifMatch != null) {
throw PersoniumCoreException.Dav.NO_ENTITY_MATCH;
}

Response response = this.davRsCmp.getDavCmp().putForCreate(contentType, inputStream).build();

// post event to EventBus
Expand Down Expand Up @@ -281,12 +292,21 @@ public Object nextPath(@PathParam("nextPath") final String nextPath,
* @return Jax-RS response object
*/
@DELETE
public final Response delete() {
public final Response delete(@HeaderParam(HttpHeaders.IF_MATCH) final String ifMatch) {
//Access control
if (!this.isParentNull) {
this.davRsCmp.getParent().checkAccessContext(BoxPrivilege.UNBIND);
}

// If If-Match: * is specified, then should return 412
// https://tools.ietf.org/html/rfc7232#section-3.1
// If the field-value is "*", the condition is false
// if the origin server does not have a current representation
// for the target resource.
if (ifMatch != null) {
throw PersoniumCoreException.Dav.NO_ENTITY_MATCH;
}

throw PersoniumCoreException.Dav.RESOURCE_NOT_FOUND.params(this.davRsCmp.getUrl());
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/personium/core/utils/HttpClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public class HttpClientFactory {
/** Connection timeout value.*/
private static final int TIMEOUT = 60000; // 20000;

/** Addr local. */
public static final String IP_ADDR_LOCAL = "127.0.0.1";

/** Constructor. */
private HttpClientFactory() {
}
Expand Down Expand Up @@ -100,7 +103,7 @@ private static CloseableHttpClient createAlwaysLocal(RequestConfig config) {
@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
// Always 127.0.0.1
return new InetAddress[] { InetAddress.getByName("127.0.0.1") };
return new InetAddress[] { InetAddress.getByName(IP_ADDR_LOCAL) };
}
};
HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry,
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/personium-messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ io.personium.core.msg.PR400-DV-0002=XML content error.
io.personium.core.msg.PR400-DV-0003=Invalid depth header value:[{0}].
io.personium.core.msg.PR400-DV-0004=Role not found.
io.personium.core.msg.PR400-DV-0005=Box not found url:[{0}].
io.personium.core.msg.PR400-DV-0006=XML validate error. Cause:[{0}].
io.personium.core.msg.PR400-DV-0006=XML validation error. Cause:[{0}].
io.personium.core.msg.PR400-DV-0007=Cannot add any more child resources.
io.personium.core.msg.PR400-DV-0008=Hierarchy of the collection is too deep.
io.personium.core.msg.PR400-DV-0009=Request header {0} value is invalid [{1}].
Expand Down Expand Up @@ -135,6 +135,9 @@ io.personium.core.msg.PR409-DV-0002=File [{0}] already exists.
# PR412-DV
io.personium.core.msg.PR412-DV-0001=ETag does not match.
io.personium.core.msg.PR412-DV-0002=Overwrite header is "F" and the destination URL is already mapped to a resource.
io.personium.core.msg.PR412-DV-0003=No entity matching.
io.personium.core.msg.PR412-DV-0004=An entity matches with the given ETag.


# PR416-DV
io.personium.core.msg.PR416-DV-0001=Requested range not satisfiable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import io.personium.common.utils.CommonUtils;
import io.personium.core.PersoniumCoreException;
Expand All @@ -42,8 +39,6 @@
/**
* DavCollectionResource unit test classs.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ AccessContext.class })
@Category({ Unit.class })
public class DavCollectionResourceTest {

Expand Down Expand Up @@ -116,4 +111,5 @@ public void delete_Error_recursiveHeader_is_false_davCmp_not_empty() {
assertThat(e.getCode(), is(PersoniumCoreException.Dav.HAS_CHILDREN.getCode()));
}
}

}
62 changes: 62 additions & 0 deletions src/test/java/io/personium/core/rs/box/DavFileResourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Personium
* Copyright 2019 Personium Project
* - FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.personium.core.rs.box;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.base.Charsets;

import io.personium.core.PersoniumCoreException;
import io.personium.test.categories.Unit;

/**
* DavFileResource unit test class.
*/
@Category({ Unit.class })
public class DavFileResourceTest {

/** Target class of unit test. */
private DavFileResource davFileResource;

/**
* put with If-None-Match header value * should fail with 412.
*/
@Test
public void put_IfNoneMatchWildcard_ShouldFail() {
// Prepare test target object
davFileResource = new DavFileResource(null, null);

// Run method
try {
InputStream is = new ByteArrayInputStream("{test:1}".getBytes(Charsets.UTF_8));
davFileResource.put(org.apache.http.entity.ContentType.APPLICATION_JSON.toString(), null, "*", is);
fail("Not throws exception.");
} catch (PersoniumCoreException e) {
// Confirm result
PersoniumCoreException expected = PersoniumCoreException.Dav.ETAG_MATCH;
assertEquals(expected.getCode(), e.getCode());
}
}
}
105 changes: 105 additions & 0 deletions src/test/java/io/personium/core/rs/box/NullResourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Personium
* Copyright 2017-2019 Personium Project
* - FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.personium.core.rs.box;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import io.personium.core.PersoniumCoreException;
import io.personium.core.model.Cell;
import io.personium.core.model.DavCmp;
import io.personium.core.model.DavRsCmp;
import io.personium.test.categories.Unit;

/**
* ODataSvcCollectionResource unit test classs.
*/
@Category({ Unit.class })
public class NullResourceTest {

/** Target class of unit test. */
private NullResource nullResource;

@Before
public void before() {
// --------------------
// Test method args
// --------------------
String url = "https://personium/cell/box";
String name = "col";
String cellUrl = "https://personium/cell/";

// --------------------
// Mock settings
// --------------------
DavRsCmp davRsCmp = mock(DavRsCmp.class);
DavCmp davCmp = mock(DavCmp.class);
Cell cell = mock(Cell.class);
doReturn(null).when(davRsCmp).getAccessContext();
doReturn(url).when(davRsCmp).getUrl();
doReturn(cell).when(davRsCmp).getCell();
doReturn(cellUrl).when(cell).getUrl();
doReturn(name).when(davCmp).getName();
nullResource = new NullResource(davRsCmp, davCmp, false);

}

/**
*
*/
@Test
public void put_With_IfMatchWildCard_ShouldReturn_412() {
try {
nullResource.put("text/html", "*", null);
fail();
}catch (PersoniumCoreException e) {
assertEquals(PersoniumCoreException.Dav.NO_ENTITY_MATCH.getCode(), e.getCode());
}
}

/**
* delete_ShouldReturn_404
*/
@Test
public void delete_ShouldReturn_404() {
try {
nullResource.delete(null);
fail();
}catch (PersoniumCoreException e) {
assertEquals(PersoniumCoreException.Dav.RESOURCE_NOT_FOUND.getCode(), e.getCode());
}
}
/**
* delete_With_IfMatchWildCard_ShouldReturn_412
*/
@Test
public void delete_With_IfMatchWildCard_ShouldReturn_412() {
try {
nullResource.delete("*");
fail();
}catch (PersoniumCoreException e) {
assertEquals(PersoniumCoreException.Dav.NO_ENTITY_MATCH.getCode(), e.getCode());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* personium.io
* Copyright 2014 FUJITSU LIMITED
* Personium
* Copyright 2014-2019 Personium Project
* - FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +55,7 @@ public class UserDataDeleteTest extends AbstractUserDataTest {
String userDataId = "userdata001";

/**
* コンストラクタ.
* Constructor.
*/
public UserDataDeleteTest() {
super(new PersoniumCoreApplication());
Expand Down Expand Up @@ -115,7 +116,7 @@ public UserDataDeleteTest() {

// DELETEを実行
deleteUserData(cellName, boxName, "colhoge", entityTypeName,
userDataId, AbstractCase.MASTER_TOKEN_NAME, HttpStatus.SC_NOT_FOUND);
userDataId, AbstractCase.MASTER_TOKEN_NAME, HttpStatus.SC_PRECONDITION_FAILED);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* personium.io
* Copyright 2014 FUJITSU LIMITED
* Personium
* Copyright 2014-2019 Personium Project
* - FUJITSU LIMITED
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,7 +49,7 @@ public class AssociationEndDeleteTest extends AbstractCase {
private static final String ENTITY_TYPE_NAME = "Product";

/**
* コンストラクタ.
* Constructor.
*/
public AssociationEndDeleteTest() {
super(new PersoniumCoreApplication());
Expand Down Expand Up @@ -147,7 +148,7 @@ public AssociationEndDeleteTest() {
.with("entityTypeName", ENTITY_TYPE_NAME)
.with("ifMatch", "*")
.returns()
.statusCode(HttpStatus.SC_NOT_FOUND)
.statusCode(HttpStatus.SC_PRECONDITION_FAILED)
.debug();

// レスポンスヘッダーのチェック
Expand Down

0 comments on commit 0f092f0

Please sign in to comment.