001/** 002 * Copyright 2016 Tampere University of Technology, Pori Department 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package service.tut.pori.apilta.alerts.reference; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.util.List; 021import java.util.Set; 022import java.util.UUID; 023 024import org.apache.commons.lang3.ArrayUtils; 025import org.apache.log4j.Logger; 026 027import core.tut.pori.http.parameters.DataGroups; 028import core.tut.pori.http.parameters.DateIntervalParameter.Interval; 029import core.tut.pori.http.parameters.Limits; 030import core.tut.pori.users.UserIdentity; 031import service.tut.pori.apilta.alerts.Definitions; 032import service.tut.pori.apilta.alerts.datatypes.Alert; 033import service.tut.pori.apilta.alerts.datatypes.AlertList; 034import service.tut.pori.apilta.alerts.datatypes.Location; 035import service.tut.pori.apilta.alerts.datatypes.LocationParameter; 036 037/** 038 * The reference implementations for Content Analysis Service. 039 * 040 */ 041public final class AlertsReferenceCore { 042 private static final UserIdentity ALERT_USER_ID = new UserIdentity(22222L); 043 private static final int BUFFER_SIZE = 256; 044 private static final AlertsXMLObjectCreator CREATOR = new AlertsXMLObjectCreator(null); 045 private static final Logger LOGGER = Logger.getLogger(AlertsReferenceCore.class); 046 047 /** 048 * 049 */ 050 private AlertsReferenceCore(){ 051 // nothing needed 052 } 053 054 /** 055 * 056 * @param dataGroups 057 * @param limits 058 * @return random alert list 059 */ 060 public static AlertList generateAlertList(DataGroups dataGroups, Limits limits) { 061 return CREATOR.generateAlertList(null, null, null, dataGroups, limits, null, null); 062 } 063 064 /** 065 * 066 * @param dataGroups 067 * @return random alert 068 */ 069 public static Alert generateAlert(DataGroups dataGroups) { 070 return CREATOR.generateAlert(null, null, null, dataGroups, null, null, null); 071 } 072 073 /** 074 * @param alertGroupIdFilter 075 * @param alertTypeFilter 076 * @param authenticatedUser 077 * @param createdFilter 078 * @param dataGroups 079 * @param location 080 * @param range 081 * @param limits 082 * @return randomly generated list of alerts matching the given terms 083 */ 084 public static AlertList retrieveAlerts(long[] alertGroupIdFilter, List<String> alertTypeFilter, UserIdentity authenticatedUser, Set<Interval> createdFilter, DataGroups dataGroups, Location location, Double range, Limits limits) { 085 if(UserIdentity.isValid(authenticatedUser)){ 086 LOGGER.debug("Authenticated user, id: "+authenticatedUser.getUserId()); // simply log the user id for debug 087 } 088 089 if(!Location.isValid(location)){ 090 if(ArrayUtils.isEmpty(alertGroupIdFilter)){ 091 throw new IllegalArgumentException(LocationParameter.PARAMETER_DEFAULT_NAME+" or "+Definitions.PARAMETER_ALERT_GROUP_ID+" must be given."); 092 }else if(range != null){ 093 throw new IllegalArgumentException(Definitions.PARAMETER_RANGE+" cannot be given without "+LocationParameter.PARAMETER_DEFAULT_NAME); 094 } 095 }else if(range == null){ 096 LOGGER.debug("Using default range: "+Definitions.DEFAULT_RANGE); 097 range = Definitions.DEFAULT_RANGE; 098 }else if(range <= 0){ 099 throw new IllegalArgumentException("Invalid range: "+range); 100 } 101 102 return CREATOR.generateAlertList(alertGroupIdFilter, alertTypeFilter, createdFilter, dataGroups, limits, location, range); 103 } 104 105 /** 106 * 107 * @param alert 108 * @param alertGroupIds 109 * @param authenticatedUser 110 * @return randomly generated id for the alert 111 * @throws IllegalArgumentException on bad input 112 */ 113 public static String addAlert(Alert alert, long[] alertGroupIds, UserIdentity authenticatedUser) throws IllegalArgumentException{ 114 if(ArrayUtils.isEmpty(alertGroupIds)){ 115 throw new IllegalArgumentException("Alert group ids must be given."); 116 } 117 118 if(UserIdentity.isValid(authenticatedUser)){ 119 LOGGER.debug("Authenticated user, id: "+authenticatedUser.getUserId()); // simply log the user id for debug 120 alert.setUserId(authenticatedUser); 121 }else{ 122 LOGGER.debug("No authenticated user, using default user id for validation."); 123 alert.setUserId(ALERT_USER_ID); 124 } 125 126 if(!Alert.isValid(alert)){ 127 throw new IllegalArgumentException("Invalid alert."); 128 } 129 130 return UUID.randomUUID().toString(); 131 } 132 133 /** 134 * 135 * @param authenticatedUser 136 * @param file the contents of the file are simply iterated as raw byte data (all content is discarded) 137 * @return randomly generated guid for the file 138 * @throws IllegalArgumentException on bad data 139 */ 140 public static String createFile(UserIdentity authenticatedUser, InputStream file) throws IllegalArgumentException { 141 try { 142 byte[] buffer = new byte[BUFFER_SIZE]; 143 while(file.read(buffer) > 0){ 144 // simply discard all data 145 } 146 } catch (IOException ex) { 147 LOGGER.error(ex, ex); 148 throw new IllegalArgumentException("Failed to read file."); 149 } 150 if(UserIdentity.isValid(authenticatedUser)){ 151 LOGGER.debug("Authenticated user, id: "+authenticatedUser.getUserId()); // simply log the user id for debug 152 } 153 return CREATOR.createGUID(); // accept any file, return random GUID 154 } 155}