Skip to content

Commit

Permalink
Issue #1086
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny committed Sep 10, 2024
1 parent 85bf4df commit 66ddf05
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ deploy
**/out
**/nbactions.xml

**/index/*
#**/index/*



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ protected void configureServlets() {
bind(cz.incad.kramerius.rest.apiNew.admin.v70.sync.SDNNTSyncResource.class);
bind(cz.incad.kramerius.rest.apiNew.admin.v70.conf.Configurations.class);
bind(cz.incad.kramerius.rest.apiNew.admin.v70.AdminLockResource.class);
bind(cz.incad.kramerius.rest.apiNew.admin.v70.index.IndexReflectionResource.class);
//bind(cz.incad.kramerius.rest.apiNew.admin.v70.processing.ProcessingSupportResource.class);

// OAI endpoint
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (C) Sep 9, 2024 Pavel Stastny
*
* 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/>.
*/
package cz.incad.kramerius.rest.apiNew.admin.v70.index;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;

import com.google.inject.Inject;
import com.google.inject.Provider;

import cz.incad.kramerius.ObjectPidsPath;
import cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed;
import cz.incad.kramerius.rest.api.exceptions.GenericApplicationException;
import cz.incad.kramerius.rest.apiNew.admin.v70.AdminApiResource;
import cz.incad.kramerius.security.RightsResolver;
import cz.incad.kramerius.security.SecuredActions;
import cz.incad.kramerius.security.SpecialObjects;
import cz.incad.kramerius.security.User;
import cz.incad.kramerius.utils.conf.KConfiguration;
import cz.incad.kramerius.utils.solr.SolrUtils;

//@Path("/admin/v7.0/items")
//public class ItemsResource extends AdminApiResource {

@Path("/admin/v7.0/indexreflection")
public class IndexReflectionResource extends AdminApiResource {

public static final Logger LOGGER = Logger.getLogger(IndexReflectionResource.class.getName());

@Inject
RightsResolver rightsResolver;

@Inject
Provider<User> userProvider;



@GET
@Path("/search/schema")
@Produces(MediaType.APPLICATION_JSON)
public Response getSearchSchema() {
if (permit(this.userProvider.get(), SecuredActions.A_INDEX)) {
try {
String serchHost = KConfiguration.getInstance().getSolrSearchHost();
InputStream schema = SolrUtils.schema(serchHost);
String jsonVal = IOUtils.toString(schema, "UTF-8");
return Response.ok(jsonVal).type(MediaType.APPLICATION_JSON).build();
} catch (IOException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
throw new GenericApplicationException(e.getMessage());
}
} else {
throw new ActionNotAllowed("not allowed");
}
}

@GET
@Path("/search/schema/fields")
public Response getSearchFields() {
if (permit(this.userProvider.get(), SecuredActions.A_INDEX)) {
try {
String serchHost = KConfiguration.getInstance().getSolrSearchHost();
InputStream schema = SolrUtils.fields(serchHost);
String jsonVal = IOUtils.toString(schema, "UTF-8");
return Response.ok(jsonVal).type(MediaType.APPLICATION_JSON).build();
} catch (IOException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
throw new GenericApplicationException(e.getMessage());
}
} else {
throw new ActionNotAllowed("not allowed");
}
}


@GET
@Path("/logs/schema")
@Produces(MediaType.APPLICATION_JSON)
public Response getLogsSchema() {
if (permit(this.userProvider.get(), SecuredActions.A_INDEX)) {
try {
String loggerPoint = KConfiguration.getInstance().getProperty("k7.log.solr.point","http://localhost:8983/solr/logs");
InputStream schema = SolrUtils.schema(loggerPoint);
String jsonVal = IOUtils.toString(schema, "UTF-8");
return Response.ok(jsonVal).type(MediaType.APPLICATION_JSON).build();
} catch (IOException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
throw new GenericApplicationException(e.getMessage());
}
} else {
throw new ActionNotAllowed("not allowed");
}
}


@GET
@Path("/logs/schema/fields")
@Produces(MediaType.APPLICATION_JSON)
public Response getLogsFields() {
if (permit(this.userProvider.get(), SecuredActions.A_INDEX)) {
try {
String loggerPoint = KConfiguration.getInstance().getProperty("k7.log.solr.point","http://localhost:8983/solr/logs");
InputStream schema = SolrUtils.fields(loggerPoint);
String jsonVal = IOUtils.toString(schema, "UTF-8");
return Response.ok(jsonVal).type(MediaType.APPLICATION_JSON).build();
} catch (IOException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
throw new GenericApplicationException(e.getMessage());
}
} else {
throw new ActionNotAllowed("not allowed");
}
}

boolean permit(User user, SecuredActions action) {
if (user != null)
return this.rightsResolver.isActionAllowed(user,
action.getFormalName(),
SpecialObjects.REPOSITORY.getPid(), null,
ObjectPidsPath.REPOSITORY_PATH).flag();
else
return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/
package cz.incad.kramerius.rest.apiNew.admin.v70.statistics;

import static org.apache.http.HttpStatus.SC_BAD_REQUEST;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.text.ParseException;
import java.time.OffsetDateTime;
Expand All @@ -44,31 +47,41 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import com.google.inject.name.Named;
import cz.incad.kramerius.processes.LRProcessManager;
import cz.incad.kramerius.processes.*;

import cz.incad.kramerius.rest.api.exceptions.BadRequestException;
import cz.incad.kramerius.rest.api.processes.LRResource;
import cz.incad.kramerius.rest.apiNew.client.v70.SearchResource;
import cz.incad.kramerius.rest.apiNew.exceptions.InternalErrorException;
import cz.incad.kramerius.statistics.filters.*;
import cz.incad.kramerius.statistics.formatters.report.StatisticsReportFormatter;
import cz.incad.kramerius.users.LoggedUsersSingleton;
import cz.incad.kramerius.utils.IOUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;

import org.apache.http.client.HttpResponseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.terracotta.statistics.Statistic;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.google.inject.Inject;
import com.google.inject.Provider;

import cz.incad.kramerius.ObjectPidsPath;
import cz.incad.kramerius.SolrAccess;
import cz.incad.kramerius.gdpr.AnonymizationSupport;
import cz.incad.kramerius.rest.api.exceptions.ActionNotAllowed;
import cz.incad.kramerius.rest.api.exceptions.GenericApplicationException;
Expand All @@ -82,6 +95,7 @@
import cz.incad.kramerius.statistics.StatisticsReportException;
import cz.incad.kramerius.statistics.filters.VisibilityFilter.VisbilityType;
import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.XMLUtils;
import cz.incad.kramerius.utils.database.Offset;


Expand Down Expand Up @@ -120,6 +134,10 @@ public class StatisticsResource {
@Inject
Provider<HttpServletRequest> requestProvider;

@Inject
@Named("new-index")
private SolrAccess solrAccess;


@DELETE
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
Expand Down Expand Up @@ -524,9 +542,7 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti


public static void validateDateRange(String dateFrom, String dateTo) {
// ISO-8601 formátování pro datum s časovou zónou (např. "2024-09-05T00:00:00Z")
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
// Naparsování řetězců na OffsetDateTime objekty
OffsetDateTime fromDate = OffsetDateTime.parse(dateFrom, formatter);
OffsetDateTime toDate = OffsetDateTime.parse(dateTo, formatter);
long maxHours = 120;
Expand All @@ -536,22 +552,108 @@ public static void validateDateRange(String dateFrom, String dateTo) {
}
}




@GET
@Path("search")
public Response search(@Context UriInfo uriInfo, @Context HttpHeaders headers, @QueryParam("wt") String wt) {
try {
if (permit(SecuredActions.A_STATISTICS)) {
MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
StringBuilder builder = new StringBuilder();
Set<String> keys = queryParameters.keySet();
for (String k : keys) {
for (final String v : queryParameters.get(k)) {
String value = v;
builder.append(k).append("=").append(URLEncoder.encode(value, "UTF-8"));
builder.append("&");
}
}

if ("json".equals(wt)) {
return Response.ok().type(MediaType.APPLICATION_JSON + ";charset=utf-8").entity(buildSearchResponseJson(uriInfo, builder.toString() )).build();
} else if ("xml".equals(wt)) {
return Response.ok().type(MediaType.APPLICATION_XML + ";charset=utf-8").entity(buildSearchResponseXml(uriInfo, builder.toString())).build();
} else { //format not specified in query param "wt"
boolean preferXmlAccordingToHeaderAccept = false;
List<String> headerAcceptValues = headers.getRequestHeader("Accept");
if (headerAcceptValues != null) { //can be null instead of empty list in some implementations
for (String headerValue : headerAcceptValues) {
if ("application/xml".equals(headerValue) || "text/xml".equals(headerValue)) {
preferXmlAccordingToHeaderAccept = true;
break;
}
}
}
if (preferXmlAccordingToHeaderAccept) { //header Accept contains "application/xml" or "text/xml"
return Response.ok().type(MediaType.APPLICATION_XML + ";charset=utf-8").entity(buildSearchResponseXml(uriInfo, builder.toString())).build();
} else { //default format: json
return Response.ok().type(MediaType.APPLICATION_JSON + ";charset=utf-8").entity(buildSearchResponseJson(uriInfo, builder.toString())).build();
}
}
} else {
throw new ActionNotAllowed("not allowed");
}

} catch (WebApplicationException e) {
throw e;
} catch (Throwable e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
}

private String buildSearchResponseJson(UriInfo uriInfo, String solrQuery) {
try {
return cz.incad.kramerius.utils.solr.SolrUtils.requestWithSelectReturningString(logsEndpoint(), solrQuery, "json");

} catch (HttpResponseException e) {
if (e.getStatusCode() == SC_BAD_REQUEST) {
LOGGER.log(Level.INFO, "SOLR Bad Request: " + uriInfo.getRequestUri());
throw new BadRequestException(e.getMessage());
} else {
LOGGER.log(Level.INFO, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
} catch (JSONException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
}


private String buildSearchResponseXml(UriInfo uriInfo,String solrQuery) {
try {
return cz.incad.kramerius.utils.solr.SolrUtils.requestWithSelectReturningString(logsEndpoint(), solrQuery, "xml");
} catch (HttpResponseException e) {
if (e.getStatusCode() == SC_BAD_REQUEST) {
LOGGER.log(Level.INFO, "SOLR Bad Request: " + uriInfo.getRequestUri());
throw new BadRequestException(e.getMessage());
} else {
LOGGER.log(Level.INFO, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
}



@GET
@Path("logs")
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8" })
public Response exportLogs(@QueryParam("dateFrom") String dateFrom, @QueryParam("dateTo") String dateTo, @QueryParam("rows") String rows, @QueryParam("start") String start) {
try {

if (permit(SecuredActions.A_EXPORT_STATISTICS)) {

if (!StringUtils.isAnyString(rows)) {
rows = "10";
}

if (!StringUtils.isAnyString(start)) {
start="0";
}

if (!StringUtils.isAnyString(rows)) { rows = "10"; }
if (!StringUtils.isAnyString(start)) { start="0"; }
String selectEndpint = this.logsEndpoint();
StringBuilder builder = new StringBuilder("q=*");
builder.append(String.format("&rows=%s&start=%s", rows, start));
Expand Down
Loading

0 comments on commit 66ddf05

Please sign in to comment.