-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced a new elasticsearch exception family that can hold headers
- These heades will be copied as response header on the rest response
- Loading branch information
Showing
5 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.rest; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Classes that carry rest headers should implement this interface. Specifically, exceptions that | ||
* get translated to a rest response, can implement this interface and the headers will be added | ||
* the the response. | ||
* | ||
* @see org.elasticsearch.ElasticsearchException.WithRestHeaders | ||
*/ | ||
public interface HasRestHeaders { | ||
|
||
/** | ||
* @return The rest headers | ||
*/ | ||
Map<String, List<String>> getHeaders(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/java/org/elasticsearch/rest/BytesRestResponseTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.rest; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.test.ElasticsearchTestCase; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
/** | ||
* | ||
*/ | ||
public class BytesRestResponseTests extends ElasticsearchTestCase { | ||
|
||
@Test | ||
public void testWithHeaders() throws Exception { | ||
RestRequest request = new FakeRestRequest(); | ||
RestChannel channel = new RestChannel(request) { | ||
@Override | ||
public void sendResponse(RestResponse response) { | ||
} | ||
}; | ||
BytesRestResponse response = new BytesRestResponse(channel, new ExceptionWithHeaders()); | ||
assertThat(response.getHeaders().get("n1"), notNullValue()); | ||
assertThat(response.getHeaders().get("n1"), contains("v11", "v12")); | ||
assertThat(response.getHeaders().get("n2"), notNullValue()); | ||
assertThat(response.getHeaders().get("n2"), contains("v21", "v22")); | ||
} | ||
|
||
|
||
private static class ExceptionWithHeaders extends ElasticsearchException.WithRestHeaders { | ||
|
||
ExceptionWithHeaders() { | ||
super("", header("n1", "v11", "v12"), header("n2", "v21", "v22")); | ||
} | ||
} | ||
} |