Browse files

Rest API: Add an `X-Opaque-Id` header, to be returned in the response…

… if exists in the request, closes #1202.
  • Loading branch information...
1 parent 318bdb9 commit 95031261558529376976d0bc0798673b6f1e0614 @kimchy kimchy committed Aug 3, 2011
View
38 modules/elasticsearch/src/main/java/org/elasticsearch/http/HttpHelper.java
@@ -1,38 +0,0 @@
-/*
- * Licensed to Elastic Search and Shay Banon under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. Elastic Search 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.http;
-
-import org.elasticsearch.common.Nullable;
-
-/**
- */
-public class HttpHelper {
-
- public static boolean isBrowser(@Nullable String userAgent) {
- if (userAgent == null) {
- return false;
- }
- // chrome, safari, firefox, ie
- if (userAgent.startsWith("Mozilla")) {
- return true;
- }
- return false;
- }
-}
View
9 modules/elasticsearch/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java
@@ -36,10 +36,10 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.http.HttpChannel;
import org.elasticsearch.http.HttpException;
-import org.elasticsearch.http.HttpHelper;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.XContentRestResponse;
+import org.elasticsearch.rest.support.RestUtils;
import org.elasticsearch.transport.netty.NettyTransport;
import java.io.IOException;
@@ -78,7 +78,7 @@ public NettyHttpChannel(NettyHttpServerTransport transport, Channel channel, org
} else {
resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
}
- if (HttpHelper.isBrowser(request.getHeader(HttpHeaders.Names.USER_AGENT))) {
+ if (RestUtils.isBrowser(request.getHeader(HttpHeaders.Names.USER_AGENT))) {
// add support for cross origin
resp.addHeader("Access-Control-Allow-Origin", "*");
if (request.getMethod() == HttpMethod.OPTIONS) {
@@ -89,6 +89,11 @@ public NettyHttpChannel(NettyHttpServerTransport transport, Channel channel, org
}
}
+ String opaque = request.getHeader("X-Opaque-Id");
+ if (opaque != null) {
+ resp.addHeader("X-Opaque-Id", opaque);
+ }
+
// Convert the response content to a ChannelBuffer.
ChannelFutureListener releaseContentListener = null;
ChannelBuffer buf;
View
12 modules/elasticsearch/src/main/java/org/elasticsearch/rest/support/RestUtils.java
@@ -19,6 +19,7 @@
package org.elasticsearch.rest.support;
+import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.base.Charsets;
import org.elasticsearch.common.path.PathTrie;
@@ -36,6 +37,17 @@
}
};
+ public static boolean isBrowser(@Nullable String userAgent) {
+ if (userAgent == null) {
+ return false;
+ }
+ // chrome, safari, firefox, ie
+ if (userAgent.startsWith("Mozilla")) {
+ return true;
+ }
+ return false;
+ }
+
public static void decodeQueryString(String s, int fromIndex, Map<String, String> params) {
if (fromIndex < 0) {
return;
View
19 plugins/transport/wares/src/main/java/org/elasticsearch/wares/NodeServlet.java
@@ -27,6 +27,7 @@
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
+import org.elasticsearch.rest.support.RestUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
@@ -128,12 +129,18 @@
@Override public void sendResponse(RestResponse response) {
resp.setContentType(response.contentType());
- resp.addHeader("Access-Control-Allow-Origin", "*");
- if (restRequest.method() == RestRequest.Method.OPTIONS) {
- // also add more access control parameters
- resp.addHeader("Access-Control-Max-Age", "1728000");
- resp.addHeader("Access-Control-Allow-Methods", "PUT, DELETE");
- resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With");
+ if (RestUtils.isBrowser(restRequest.header("User-Agent"))) {
+ resp.addHeader("Access-Control-Allow-Origin", "*");
+ if (restRequest.method() == RestRequest.Method.OPTIONS) {
+ // also add more access control parameters
+ resp.addHeader("Access-Control-Max-Age", "1728000");
+ resp.addHeader("Access-Control-Allow-Methods", "PUT, DELETE");
+ resp.addHeader("Access-Control-Allow-Headers", "X-Requested-With");
+ }
+ }
+ String opaque = restRequest.header("X-Opaque-Id");
+ if (opaque != null) {
+ resp.addHeader("X-Opaque-Id", opaque);
}
try {
int contentLength = response.contentLength() + response.prefixContentLength() + response.suffixContentLength();

0 comments on commit 9503126

Please sign in to comment.