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.sensors; 017 018import core.tut.pori.http.Response; 019import core.tut.pori.http.Response.Status; 020import core.tut.pori.http.annotations.HTTPAuthenticationParameter; 021import core.tut.pori.http.annotations.HTTPMethodParameter; 022import core.tut.pori.http.annotations.HTTPService; 023import core.tut.pori.http.annotations.HTTPServiceMethod; 024import core.tut.pori.http.parameters.AuthenticationParameter; 025import core.tut.pori.http.parameters.DataGroups; 026import core.tut.pori.http.parameters.DateIntervalParameter; 027import core.tut.pori.http.parameters.InputStreamParameter; 028import core.tut.pori.http.parameters.Limits; 029import core.tut.pori.http.parameters.LongParameter; 030import core.tut.pori.http.parameters.StringParameter; 031import core.tut.pori.utils.XMLFormatter; 032import service.tut.pori.apilta.files.datatypes.FileDetails; 033import service.tut.pori.apilta.files.datatypes.FileDetailsList; 034import service.tut.pori.apilta.sensors.datatypes.SensorTask; 035import service.tut.pori.tasks.TasksCore; 036import service.tut.pori.tasks.datatypes.Task; 037import service.tut.pori.tasks.datatypes.TaskList; 038 039/** 040 * Service declaration for the sensor service 041 * 042 */ 043@HTTPService(name = Definitions.SERVICE_SENSORS) 044public class SensorService { 045 private XMLFormatter _formatter = new XMLFormatter(); 046 047 /** 048 * 049 * @param authenticatedUser 050 * @param xml Only the result data should be in the body. See {@link service.tut.pori.apilta.sensors.datatypes.SensorTask} 051 */ 052 @HTTPServiceMethod(name = service.tut.pori.tasks.Definitions.METHOD_TASK_FINISHED, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST}) 053 public void taskFinished ( 054 @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser, 055 @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml 056 ) 057 { 058 SensorsCore.taskFinished(authenticatedUser.getUserIdentity(), _formatter.toObject(xml.getValue(), SensorTask.class)); 059 } 060 061 /** 062 * 063 * @param authenticatedUser 064 * @param taskId 065 * @param backendId 066 * @param dataGroups 067 * @param limits paging limits 068 * @return response See {@link service.tut.pori.apilta.sensors.datatypes.SensorTask} 069 */ 070 @HTTPServiceMethod(name = service.tut.pori.tasks.Definitions.METHOD_QUERY_TASK_DETAILS, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET}) 071 public Response queryTaskDetails ( 072 @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser, 073 @HTTPMethodParameter(name = service.tut.pori.tasks.Definitions.PARAMETER_BACKEND_ID) LongParameter backendId, 074 @HTTPMethodParameter(name = service.tut.pori.tasks.Definitions.PARAMETER_TASK_ID) StringParameter taskId, 075 @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups, 076 @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits 077 ) 078 { 079 return new Response(TasksCore.retrieveTask(authenticatedUser.getUserIdentity(), backendId.getValue(), dataGroups, limits, taskId.getValue())); 080 } 081 082 /** 083 * Implementation of create task 084 * @param authenticatedUser 085 * @param xml Only the workload data should be in the body. See {@link service.tut.pori.apilta.sensors.datatypes.SensorTask} 086 * @return {@link service.tut.pori.tasks.datatypes.TaskList} with identifier for the generated task 087 */ 088 @HTTPServiceMethod(name = Definitions.METHOD_CREATE_TASK, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST}) 089 public Response createTask ( 090 @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser, 091 @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml 092 ) 093 { 094 String taskId = SensorsCore.createTask(authenticatedUser.getUserIdentity(), _formatter.toObject(xml.getValue(), SensorTask.class)); 095 if(taskId == null){ 096 return new Response(Status.FORBIDDEN); 097 }else{ 098 TaskList taskList = new TaskList(); 099 Task task = new SensorTask(); 100 task.addTaskId(taskId); 101 taskList.addTask(task); 102 return new Response(taskList); 103 } 104 } 105 106 /** 107 * Implementation of modify task 108 * @param authenticatedUser 109 * @param xml Only the workload data should be in the body. See {@link service.tut.pori.apilta.sensors.datatypes.SensorTask} 110 * @return {@link service.tut.pori.tasks.datatypes.TaskList} with identifier for the generated task 111 */ 112 @HTTPServiceMethod(name = Definitions.METHOD_MODIFY_TASK, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST}) 113 public Response modifyTask ( 114 @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser, 115 @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml 116 ) 117 { 118 String taskId = SensorsCore.modifyTask(authenticatedUser.getUserIdentity(), _formatter.toObject(xml.getValue(), SensorTask.class)); 119 if(taskId == null){ 120 return new Response(Status.FORBIDDEN); 121 }else{ 122 TaskList taskList = new TaskList(); 123 Task task = new SensorTask(); 124 task.addTaskId(taskId); 125 taskList.addTask(task); 126 return new Response(taskList); 127 } 128 } 129 130 /** 131 * Implementation of create file 132 * @param authenticatedUser 133 * @param backendId 134 * @param file only the file contents should be in the body. 135 * @return {@link service.tut.pori.apilta.files.datatypes.FileDetailsList} with identifier for the generated file 136 */ 137 @HTTPServiceMethod(name = Definitions.METHOD_CREATE_FILE, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST}) 138 public Response createFile ( 139 @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser, 140 @HTTPMethodParameter(name = service.tut.pori.tasks.Definitions.PARAMETER_BACKEND_ID) LongParameter backendId, 141 @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter file 142 ) 143 { 144 FileDetails details = SensorsCore.createFile(authenticatedUser.getUserIdentity(), backendId.getValue(), file.getValue()); 145 if(details == null){ 146 return new Response(Status.FORBIDDEN); 147 }else{ 148 FileDetailsList list = new FileDetailsList(); 149 list.addFile(details); 150 return new Response(list); 151 } 152 } 153 154 /** 155 * 156 * @param authenticatedUser 157 * @param taskId 158 * @param backendIdFilter 159 * @param createdFilter 160 * @param dataGroups for valid data groups see {@link service.tut.pori.apilta.sensors.SensorsCore#getMeasurements(core.tut.pori.users.UserIdentity, long[], java.util.Set, DataGroups, Limits, java.util.List, java.util.List)} 161 * @param limits 162 * @param measurementIdFilter 163 * @return see {@link service.tut.pori.apilta.sensors.datatypes.MeasurementList} 164 */ 165 @HTTPServiceMethod(name = Definitions.METHOD_GET_MEASUREMENTS, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET}) 166 public Response getMeasurements( 167 @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser, 168 @HTTPMethodParameter(name = service.tut.pori.tasks.Definitions.PARAMETER_TASK_ID) StringParameter taskId, 169 @HTTPMethodParameter(name = service.tut.pori.tasks.Definitions.PARAMETER_BACKEND_ID, required = false) LongParameter backendIdFilter, 170 @HTTPMethodParameter(name = service.tut.pori.tasks.Definitions.PARAMETER_CREATED, required = false) DateIntervalParameter createdFilter, 171 @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups, 172 @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits, 173 @HTTPMethodParameter(name = Definitions.PARAMETER_MEASUREMENT_ID, required = false) StringParameter measurementIdFilter 174 ) 175 { 176 return new Response(SensorsCore.getMeasurements(authenticatedUser.getUserIdentity(), backendIdFilter.getValues(), createdFilter.getValues(), dataGroups, limits, measurementIdFilter.getValues(), taskId.getValues())); 177 } 178}