Skip to content
Snippets Groups Projects
Commit 3625b1b6 authored by strentini's avatar strentini
Browse files

OO-22

extension to the restAPI:  request logfiles
parent eff81bf9
No related branches found
No related tags found
No related merge requests found
......@@ -75,13 +75,19 @@ public class LogFileParser extends LogDelegator {
* @param date the date of the log to retrieve, or null when no date suffix should be appended (= take today's log)
* @return the VFSLeaf of the Logfile given the Date, or null if no such file could be found
*/
public VFSLeaf getLogfilePath(Date date) {
public static VFSLeaf getLogfilePath(Date date) {
String tmpFileName = logfilepathBase;
if (date != null) {
SimpleDateFormat sdb = new SimpleDateFormat("yyyy-MM-dd");
String suffix = sdb.format(date);
filename += "."+suffix;
String today = sdb.format(new Date());
if(suffix.equals(today))
tmpFileName = logfilepathBase;
else
tmpFileName = logfilepathBase + "."+suffix;
}
File logf = new File(filename);
File logf = new File(tmpFileName);
if (!logf.exists()) return null;
return new LocalFileImpl(logf);
}
......
......@@ -45,6 +45,7 @@
<value>org.olat.catalog.restapi.CatalogWebService</value>
<value>org.olat.notifications.restapi.NotificationsWebService</value>
<value>com.frentix.olat.restapi.MoodleImportWebService</value>
<value>org.olat.restapi.log.LogWebService</value>
</list>
</property>
<!-- property name="singletonBeans">
......
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.restapi.log;
import static org.olat.restapi.security.RestSecurityHelper.isAdmin;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang.StringUtils;
import org.olat.core.logging.LogFileParser;
import org.olat.core.util.vfs.VFSLeaf;
/**
* Description:<br>
* This web service returns logFiles
*
* <P>
* Initial Date: 23.12.2011 <br>
* @author strentini, sergio.trentini@frentix.com, www.frentix.com
*/
@Path("log")
public class LogWebService {
private static final String VERSION = "1.0";
public static CacheControl cc = new CacheControl();
static {
cc.setMaxAge(-1);
}
/**
* The version of the Log Web Service
*
* @response.representation.200.mediaType text/plain
* @response.representation.200.doc The version of this specific Web Service
* @response.representation.200.example 1.0
* @return
*/
@GET
@Path("version")
@Produces(MediaType.TEXT_PLAIN)
public Response getVersion() {
return Response.ok(VERSION).build();
}
@GET
@Path("{date}")
@Produces({ "text/plain", MediaType.APPLICATION_OCTET_STREAM })
public Response getLogFileByDate(@PathParam("date") String dateString, @Context HttpServletRequest request) {
// logfile download only allowed for admins!
if (!isAdmin(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
VFSLeaf logFile;
try {
logFile = logFileFromParam(dateString);
} catch (ParseException e) {
return Response.serverError().status(Status.BAD_REQUEST).build();
}
if (logFile == null)
return Response.serverError().status(Status.NOT_FOUND).build();
InputStream is = logFile.getInputStream();
if (is == null)
return Response.serverError().status(Status.NOT_FOUND).build();
return Response.ok(is).cacheControl(cc).build(); // success
}
@GET
@Path("")
@Produces({ "text/plain", MediaType.APPLICATION_OCTET_STREAM })
public Response getCurrentLogFile( @Context HttpServletRequest request) {
return getLogFileByDate(null, request);
}
/**
* returns the correct LogFile as VFSLeaf or null.<br />
*
* dateString can be: <br />
* <ul>
* <li>"today" : will return the current Logfile if it exists</li>
* <li>a two digit number, representing a day of the month : will return the
* logFile of the given day (of the current month)</li>
* <li>A Date-String of the form : yyyy-MM-dd</li>
* </ul>
*
* will return null if the given String is not valid or the resulting
* logfile is not found
*
* @param dateString
* a two digit number (dayOfTheMonth) or "today"
* @return the requested LogFile as VFSLeaf or null
* @throws ParseException
*/
private static VFSLeaf logFileFromParam(String dateString) throws ParseException {
VFSLeaf logFile;
if (StringUtils.isBlank(dateString) || "today".equals(dateString)) {
logFile = LogFileParser.getLogfilePath(null);
} else if(dateString.length() == 2){
DateFormat formatter = new SimpleDateFormat("dd");
Calendar cal_param = Calendar.getInstance();
cal_param.setTime(formatter.parse(dateString));
Calendar cal_file = Calendar.getInstance();
cal_file.set(Calendar.DAY_OF_MONTH, cal_param.get(Calendar.DAY_OF_MONTH));
logFile = LogFileParser.getLogfilePath(cal_file.getTime());
}else{
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
logFile = LogFileParser.getLogfilePath(formatter.parse(dateString));
}
return logFile;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment