Commit ec86e60c authored by Matija Obreza's avatar Matija Obreza
Browse files

Removed ElasticsearchHelper, removed parsing client_id from URI

parent e0634ecc
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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 org.genesys2.server.servlet.controller.webapi;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerMapping;
/**
* Relay /api/v0/veryelastic requests to elasticsearch
*/
@Controller
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/webapi")
public class ElasticsearchHelper {
@Value("${elasticsearch.url}")
private String elasticsearchUrl;
public static final Log LOG = LogFactory.getLog(ElasticsearchHelper.class);
@RequestMapping("/{clientId}/es/**")
public void relay2(@PathVariable("clientId") String clientId, HttpServletRequest request, HttpServletResponse response) throws IOException {
String foo = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
foo = foo.substring("/webapi".length() + 1 + clientId.length() + 1 + "es".length() + 1);
String queryString = request.getQueryString();
String method = request.getMethod();
if (LOG.isDebugEnabled()) {
LOG.debug("ES path: " + foo);
LOG.debug("ES qs: " + queryString);
LOG.debug("ES method: " + method);
}
final HttpClient httpclient = new DefaultHttpClient();
HttpRequestBase req = null;
if ("POST".equalsIgnoreCase(method)) {
HttpPost r = new HttpPost(elasticsearchUrl + foo);
HttpEntity entity = new InputStreamEntity(request.getInputStream(), request.getContentLength());
r.setEntity(entity);
req = r;
} else if ("PUT".equalsIgnoreCase(method)) {
HttpPut r = new HttpPut(elasticsearchUrl + foo);
HttpEntity entity = new InputStreamEntity(request.getInputStream(), request.getContentLength());
r.setEntity(entity);
req = r;
} else if ("GET".equalsIgnoreCase(method)) {
req = new HttpGet(elasticsearchUrl + foo);
}
HttpResponse esResponse = null;
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Proxy " + req);
}
esResponse = httpclient.execute(req);
HttpEntity e = esResponse.getEntity();
response.setContentType(e.getContentType().getValue());
response.setContentLengthLong(e.getContentLength());
IOUtils.copy(e.getContent(), response.getOutputStream());
response.flushBuffer();
} catch (final IOException e) {
LOG.error(e.getMessage(), e);
throw e;
}
}
}
......@@ -18,8 +18,6 @@ package org.genesys2.server.servlet.filter;
import java.io.IOException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
......@@ -42,8 +40,6 @@ import org.springframework.web.filter.OncePerRequestFilter;
public class WebApiFilter extends OncePerRequestFilter {
private static final Logger _logger = LoggerFactory.getLogger(WebApiFilter.class);
private Pattern webapiUri = Pattern.compile("/webapi/([^@]+@[^/]+)/.+");
@Autowired
private OAuth2ClientDetailsService clientDetailsService;
......@@ -53,18 +49,6 @@ public class WebApiFilter extends OncePerRequestFilter {
String clientSecret = request.getParameter("client_secret");
String referrer = request.getHeader("Referer");
if (clientId == null) {
String requestURI = request.getRequestURI();
if (_logger.isDebugEnabled()) _logger.debug("PATH=" + requestURI);
Matcher m = webapiUri.matcher(requestURI);
if (m.matches()) {
clientId = m.group(1);
if (_logger.isDebugEnabled())
_logger.debug("client_id=" + clientId);
}
}
try {
if (StringUtils.isBlank(clientId)) {
throw new Exception("client_id not provided");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment