Skip to content
Snippets Groups Projects
Commit 1fd631c6 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

- Allow to access image directly

parent ed915c87
No related branches found
No related tags found
No related merge requests found
Showing
with 334 additions and 18 deletions
...@@ -22,6 +22,7 @@ package org.duniter.elasticsearch.rest; ...@@ -22,6 +22,7 @@ package org.duniter.elasticsearch.rest;
* #L% * #L%
*/ */
import org.duniter.elasticsearch.rest.attachment.RestImageAttachmentAction;
import org.duniter.elasticsearch.rest.currency.RestCurrencyIndexAction; import org.duniter.elasticsearch.rest.currency.RestCurrencyIndexAction;
import org.duniter.elasticsearch.rest.security.RestSecurityAuthAction; import org.duniter.elasticsearch.rest.security.RestSecurityAuthAction;
import org.duniter.elasticsearch.rest.security.RestSecurityController; import org.duniter.elasticsearch.rest.security.RestSecurityController;
...@@ -34,6 +35,9 @@ public class RestModule extends AbstractModule implements Module { ...@@ -34,6 +35,9 @@ public class RestModule extends AbstractModule implements Module {
@Override protected void configure() { @Override protected void configure() {
// Attachment
bind(RestImageAttachmentAction.class).asEagerSingleton();
// Currency // Currency
bind(RestCurrencyIndexAction.class).asEagerSingleton(); bind(RestCurrencyIndexAction.class).asEagerSingleton();
......
package org.duniter.elasticsearch.rest.attachment;
/*
* #%L
* duniter4j-elasticsearch-plugin
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.core.util.StringUtils;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Base64;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.internal.Join;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestResponseListener;
import org.elasticsearch.search.fetch.source.FetchSourceContext;
import java.util.Arrays;
import java.util.Map;
import static org.elasticsearch.rest.RestStatus.OK;
public class RestImageAttachmentAction extends BaseRestHandler {
@Inject
public RestImageAttachmentAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);
controller.registerHandler(RestRequest.Method.GET, "/{index}/{type}/{id}/_image/{field}", this);
}
@Override
protected void handleRequest(final RestRequest request, RestChannel channel, Client client) throws Exception {
String index = request.param("index");
String type = request.param("type");
String id = request.param("id");
String paramField = request.param("field");
String[] fieldParts = paramField.split("\\.");
String extension = null;
if (fieldParts.length >= 2) {
extension = fieldParts[fieldParts.length-1];
paramField = Join.join(".", Arrays.copyOf(fieldParts, fieldParts.length-1));
}
final String field = paramField;
final String expectedContentType = "image/" + extension;
GetRequest getRequest = new GetRequest(index, type, id)
.fields(field)
.fetchSourceContext(FetchSourceContext.FETCH_SOURCE)
.realtime(true);
client.get(getRequest, new RestResponseListener<GetResponse>(channel) {
@Override
public RestResponse buildResponse(GetResponse response) throws Exception {
if (response.getSource() == null || !response.getSource().containsKey(field)) {
return new BytesRestResponse(RestStatus.BAD_REQUEST, String.format("Field [%s] not exists.", field));
}
Object value = response.getSource().get(field);
if (!(value instanceof Map)) {
return new BytesRestResponse(RestStatus.BAD_REQUEST, String.format("Field [%s] is not an attachment type.", field));
}
Map<String, String> attachment = (Map<String, String>)value;
String contentType = attachment.get("_content_type");
if (StringUtils.isBlank(contentType)) {
return new BytesRestResponse(RestStatus.BAD_REQUEST, String.format("Field [%s] not contains key [_content_type].", field));
}
if (!expectedContentType.equals(contentType)) {
return new BytesRestResponse(RestStatus.BAD_REQUEST, String.format("File extension not compatible with attachment content type [%s]", contentType));
}
return new BytesRestResponse(OK,
contentType,
new BytesArray(Base64.decode(attachment.get("_content"))));
}
});
}
}
\ No newline at end of file
...@@ -30,7 +30,10 @@ import org.elasticsearch.common.logging.ESLoggerFactory; ...@@ -30,7 +30,10 @@ import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequest;
import java.util.*; import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/** /**
* Created by blavenie on 11/10/16. * Created by blavenie on 11/10/16.
...@@ -54,6 +57,10 @@ public class RestSecurityController extends AbstractLifecycleComponent<RestSecur ...@@ -54,6 +57,10 @@ public class RestSecurityController extends AbstractLifecycleComponent<RestSecur
return allow(method, String.format("/%s/%s(/.*)?", index, type)); return allow(method, String.format("/%s/%s(/.*)?", index, type));
} }
public RestSecurityController allowImageAttachment(String index, String type, String field) {
return allow(RestRequest.Method.GET, String.format("/%s/%s/[^/]+/_image/%s.*", index, type, field));
}
public RestSecurityController allow(RestRequest.Method method, String regexPath) { public RestSecurityController allow(RestRequest.Method method, String regexPath) {
Set<String> allowRules = allowRulesByMethod.get(method); Set<String> allowRules = allowRulesByMethod.get(method);
if (allowRules == null) { if (allowRules == null) {
......
package org.duniter.elasticsearch.gchange.model.market;
import org.duniter.core.client.model.elasticsearch.Record;
import java.util.HashMap;
import java.util.Map;
/**
* Created by blavenie on 01/12/16.
*/
public class MarketRecord extends Record{
public static final String PROPERTY_TITLE="title";
public static final String PROPERTY_DESCRIPTION="description";
public static final String PROPERTY_PRICE="price";
public static final String PROPERTY_UNIT="unit";
public static final String PROPERTY_CURRENCY="currency";
public static final String PROPERTY_THUMBNAIL="thumbnail";
private String title;
private String description;
private Map<String, String> thumbnail = new HashMap<>();
private Double price;
private String unit;
private String currency;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public Map<String, String> getThumbnail() {
return thumbnail;
}
public void setThumbnail(Map<String, String> thumbnail) {
this.thumbnail = thumbnail;
}
}
package org.duniter.elasticsearch.gchange.model; package org.duniter.elasticsearch.gchange.model.registry;
import org.duniter.core.client.model.elasticsearch.Record; import org.duniter.core.client.model.elasticsearch.Record;
import java.util.HashMap;
import java.util.Map;
/** /**
* Created by blavenie on 01/12/16. * Created by blavenie on 01/12/16.
*/ */
public class MarketRecord extends Record{ public class RegistryRecord extends Record{
public static final String PROPERTY_TITLE="title"; public static final String PROPERTY_TITLE="title";
public static final String PROPERTY_DESCRIPTION="description"; public static final String PROPERTY_DESCRIPTION="description";
public static final String PROPERTY_THUMBNAIL="thumbnail";
private String title; private String title;
private String description; private String description;
private Map<String, String> thumbnail = new HashMap<>();
public String getTitle() { public String getTitle() {
return title; return title;
...@@ -28,4 +33,13 @@ public class MarketRecord extends Record{ ...@@ -28,4 +33,13 @@ public class MarketRecord extends Record{
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
public Map<String, String> getThumbnail() {
return thumbnail;
}
public void setThumbnail(Map<String, String> thumbnail) {
this.thumbnail = thumbnail;
}
} }
...@@ -37,12 +37,14 @@ public class RestModule extends AbstractModule implements Module { ...@@ -37,12 +37,14 @@ public class RestModule extends AbstractModule implements Module {
bind(RestMarketCommentIndexAction.class).asEagerSingleton(); bind(RestMarketCommentIndexAction.class).asEagerSingleton();
bind(RestMarketCommentUpdateAction.class).asEagerSingleton(); bind(RestMarketCommentUpdateAction.class).asEagerSingleton();
bind(RestMarketCategoryAction.class).asEagerSingleton(); bind(RestMarketCategoryAction.class).asEagerSingleton();
bind(RestMarketImageAction.class).asEagerSingleton();
// Registry // Registry
bind(RestRegistryRecordIndexAction.class).asEagerSingleton(); bind(RestRegistryRecordIndexAction.class).asEagerSingleton();
bind(RestRegistryRecordUpdateAction.class).asEagerSingleton(); bind(RestRegistryRecordUpdateAction.class).asEagerSingleton();
bind(RestRegistryCommentIndexAction.class).asEagerSingleton(); bind(RestRegistryCommentIndexAction.class).asEagerSingleton();
bind(RestregistryCommentUpdateAction.class).asEagerSingleton(); bind(RestRegistryCommentUpdateAction.class).asEagerSingleton();
bind(RestRegistryCategoryAction.class).asEagerSingleton(); bind(RestRegistryCategoryAction.class).asEagerSingleton();
bind(RestRegistryImageAction.class).asEagerSingleton();
} }
} }
\ No newline at end of file
package org.duniter.elasticsearch.gchange.rest.market;
/*
* #%L
* duniter4j-elasticsearch-plugin
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.elasticsearch.gchange.model.market.MarketRecord;
import org.duniter.elasticsearch.gchange.model.registry.RegistryRecord;
import org.duniter.elasticsearch.gchange.service.MarketService;
import org.duniter.elasticsearch.gchange.service.RegistryService;
import org.duniter.elasticsearch.rest.security.RestSecurityController;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.rest.RestRequest;
public class RestMarketImageAction {
@Inject
public RestMarketImageAction(RestSecurityController securityController) {
// Allow to get thumbnail
securityController.allowImageAttachment(MarketService.INDEX, MarketService.RECORD_TYPE, MarketRecord.PROPERTY_THUMBNAIL);
// TODO : allow to get pictures
}
}
\ No newline at end of file
...@@ -30,10 +30,10 @@ import org.elasticsearch.common.inject.Inject; ...@@ -30,10 +30,10 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestController;
public class RestregistryCommentUpdateAction extends AbstractRestPostUpdateAction { public class RestRegistryCommentUpdateAction extends AbstractRestPostUpdateAction {
@Inject @Inject
public RestregistryCommentUpdateAction(Settings settings, RestController controller, Client client, RestSecurityController securityController, public RestRegistryCommentUpdateAction(Settings settings, RestController controller, Client client, RestSecurityController securityController,
RegistryService service) { RegistryService service) {
super(settings, controller, client, securityController, super(settings, controller, client, securityController,
RegistryService.INDEX, RegistryService.RECORD_COMMENT_TYPE, RegistryService.INDEX, RegistryService.RECORD_COMMENT_TYPE,
......
package org.duniter.elasticsearch.gchange.rest.registry;
/*
* #%L
* duniter4j-elasticsearch-plugin
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.elasticsearch.gchange.model.registry.RegistryRecord;
import org.duniter.elasticsearch.gchange.service.RegistryService;
import org.duniter.elasticsearch.rest.security.RestSecurityController;
import org.duniter.elasticsearch.user.service.UserService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.rest.RestRequest;
public class RestRegistryImageAction {
@Inject
public RestRegistryImageAction(RestSecurityController securityController) {
// Allow to get thumbnail
securityController.allowImageAttachment(RegistryService.INDEX, RegistryService.RECORD_TYPE, RegistryRecord.PROPERTY_THUMBNAIL);
// TODO : allow to get pictures
}
}
\ No newline at end of file
...@@ -32,7 +32,7 @@ import org.duniter.core.service.CryptoService; ...@@ -32,7 +32,7 @@ import org.duniter.core.service.CryptoService;
import org.duniter.elasticsearch.exception.DocumentNotFoundException; import org.duniter.elasticsearch.exception.DocumentNotFoundException;
import org.duniter.elasticsearch.exception.NotFoundException; import org.duniter.elasticsearch.exception.NotFoundException;
import org.duniter.elasticsearch.gchange.PluginSettings; import org.duniter.elasticsearch.gchange.PluginSettings;
import org.duniter.elasticsearch.gchange.model.MarketRecord; import org.duniter.elasticsearch.gchange.model.market.MarketRecord;
import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes; import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes;
import org.duniter.elasticsearch.service.AbstractService; import org.duniter.elasticsearch.service.AbstractService;
import org.duniter.elasticsearch.threadpool.ThreadPool; import org.duniter.elasticsearch.threadpool.ThreadPool;
......
...@@ -33,7 +33,7 @@ import org.duniter.core.exception.TechnicalException; ...@@ -33,7 +33,7 @@ import org.duniter.core.exception.TechnicalException;
import org.duniter.core.service.CryptoService; import org.duniter.core.service.CryptoService;
import org.duniter.core.util.websocket.WebsocketClientEndpoint; import org.duniter.core.util.websocket.WebsocketClientEndpoint;
import org.duniter.elasticsearch.PluginSettings; import org.duniter.elasticsearch.PluginSettings;
import org.duniter.elasticsearch.gchange.model.MarketRecord; import org.duniter.elasticsearch.gchange.model.market.MarketRecord;
import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes; import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes;
import org.duniter.elasticsearch.service.AbstractService; import org.duniter.elasticsearch.service.AbstractService;
import org.duniter.elasticsearch.service.BlockchainService; import org.duniter.elasticsearch.service.BlockchainService;
......
...@@ -24,21 +24,13 @@ package org.duniter.elasticsearch.gchange.service; ...@@ -24,21 +24,13 @@ package org.duniter.elasticsearch.gchange.service;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.collections4.MapUtils;
import org.duniter.core.client.model.elasticsearch.RecordComment;
import org.duniter.core.client.service.bma.WotRemoteService; import org.duniter.core.client.service.bma.WotRemoteService;
import org.duniter.core.exception.TechnicalException; import org.duniter.core.exception.TechnicalException;
import org.duniter.core.service.CryptoService; import org.duniter.core.service.CryptoService;
import org.duniter.elasticsearch.exception.DocumentNotFoundException;
import org.duniter.elasticsearch.gchange.PluginSettings; import org.duniter.elasticsearch.gchange.PluginSettings;
import org.duniter.elasticsearch.gchange.model.MarketRecord;
import org.duniter.elasticsearch.gchange.model.event.GchangeEventCodes;
import org.duniter.elasticsearch.service.AbstractService; import org.duniter.elasticsearch.service.AbstractService;
import org.duniter.elasticsearch.service.ServiceLocator; import org.duniter.elasticsearch.service.ServiceLocator;
import org.duniter.elasticsearch.threadpool.ThreadPool; import org.duniter.elasticsearch.threadpool.ThreadPool;
import org.duniter.elasticsearch.user.model.UserEvent;
import org.duniter.elasticsearch.user.service.UserService;
import org.duniter.elasticsearch.user.service.UserEventService; import org.duniter.elasticsearch.user.service.UserEventService;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder;
...@@ -48,10 +40,8 @@ import org.elasticsearch.common.inject.Inject; ...@@ -48,10 +40,8 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.nuiton.i18n.I18n;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
/** /**
* Created by Benoit on 30/03/2015. * Created by Benoit on 30/03/2015.
......
...@@ -34,6 +34,7 @@ public class UserProfile extends Record { ...@@ -34,6 +34,7 @@ public class UserProfile extends Record {
public static final String PROPERTY_CITY="city"; public static final String PROPERTY_CITY="city";
public static final String PROPERTY_EMAIL="email"; public static final String PROPERTY_EMAIL="email";
public static final String PROPERTY_LOCALE="locale"; public static final String PROPERTY_LOCALE="locale";
public static final String PROPERTY_AVATAR="avatar";
private String title; private String title;
private String description; private String description;
......
...@@ -47,6 +47,7 @@ public class RestModule extends AbstractModule implements Module { ...@@ -47,6 +47,7 @@ public class RestModule extends AbstractModule implements Module {
bind(RestUserSettingsUpdateAction.class).asEagerSingleton(); bind(RestUserSettingsUpdateAction.class).asEagerSingleton();
bind(RestUserEventMarkAsReadAction.class).asEagerSingleton(); bind(RestUserEventMarkAsReadAction.class).asEagerSingleton();
bind(RestUserEventSearchAction.class).asEagerSingleton(); bind(RestUserEventSearchAction.class).asEagerSingleton();
bind(RestUserAvatarAction.class).asEagerSingleton();
// Group // Group
bind(RestGroupIndexAction.class).asEagerSingleton(); bind(RestGroupIndexAction.class).asEagerSingleton();
......
package org.duniter.elasticsearch.user.rest.user;
/*
* #%L
* duniter4j-elasticsearch-plugin
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.elasticsearch.rest.security.RestSecurityController;
import org.duniter.elasticsearch.user.model.UserProfile;
import org.duniter.elasticsearch.user.service.UserService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.rest.RestRequest;
public class RestUserAvatarAction {
@Inject
public RestUserAvatarAction(RestSecurityController securityController) {
// Allow to get avatar as image
securityController.allowImageAttachment(UserService.INDEX, UserService.PROFILE_TYPE, UserProfile.PROPERTY_AVATAR);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment