@@ -6,6 +6,7 @@ import ch.derlin.bbdata.common.cassandra.RawValueRepository
66import ch.derlin.bbdata.common.exceptions.ForbiddenException
77import ch.derlin.bbdata.common.exceptions.ItemNotFoundException
88import ch.derlin.bbdata.common.exceptions.WrongParamsException
9+ import ch.derlin.bbdata.common.getIp
910import ch.derlin.bbdata.common.stats.StatsLogic
1011import ch.derlin.bbdata.output.api.types.BaseType
1112import com.fasterxml.jackson.databind.ObjectMapper
@@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.PostMapping
1920import org.springframework.web.bind.annotation.RequestBody
2021import org.springframework.web.bind.annotation.RequestParam
2122import org.springframework.web.bind.annotation.RestController
23+ import javax.servlet.http.HttpServletRequest
2224import javax.validation.Valid
2325import javax.validation.constraints.NotNull
2426
@@ -46,7 +48,6 @@ class InputController(
4648
4749 private val MAX_LAG : Long = 2000 // in millis
4850
49-
5051 data class NewValueAugmented (
5152 val objectId : Long ,
5253 val timestamp : DateTime ,
@@ -78,7 +79,8 @@ class InputController(
7879 " If you omit to provide a timestamp for any measure, it will be added automatically (server time). " +
7980 " This request is *atomic*: either *all* measures are valid and saved, or none. " )
8081 fun postNewMeasures (@Valid @NotNull @RequestBody rawMeasures : ValidatedList <NewValue >,
81- @RequestParam(" simulate" , defaultValue = " false" ) sim : Boolean ): List <NewValueAugmented > {
82+ @RequestParam(" simulate" , defaultValue = " false" ) sim : Boolean ,
83+ request : HttpServletRequest ): List <NewValueAugmented > {
8284
8385 val now = DateTime ()
8486
@@ -89,7 +91,7 @@ class InputController(
8991 val measure = if (rawMeasure.timestamp != null ) {
9092 // check that date is in the past
9193 if (rawMeasure.timestamp.millis > now.millis + MAX_LAG ) {
92- log.info(" REJECTED: wrong timestamp: ${rawMeasure} " )
94+ log.info(" REJECTED: <IP: ${request.getIp()} > wrong timestamp: ${rawMeasure} " )
9395 throw WrongParamsException (" objectId ${rawMeasure.objectId} : date should be in the past. input='${rawMeasure.timestamp} ', now='$now '" )
9496 }
9597 rawMeasure
@@ -100,18 +102,18 @@ class InputController(
100102
101103 // get metadata
102104 val meta = inputRepository.getMeasureMeta(measure.objectId!! , measure.token!! ).orElseThrow {
103- log.info(" REJECTED: wrong token: $measure " )
105+ log.info(" REJECTED: <IP: ${request.getIp()} > wrong token: $measure " )
104106 ItemNotFoundException (msg = " objectId ${measure.objectId} : the pair <objectId, token> does not exist" )
105107 }
106108 // ensure the object is enabled. This is just a double check, as tokens cannot be created on disabled objects
107109 if (meta.disabled) {
108- log.info(" REJECTED: object is disabled: $rawMeasure " )
110+ log.info(" REJECTED: <IP: ${request.getIp()} > object is disabled: $rawMeasure " )
109111 throw ForbiddenException (" objectId ${rawMeasure.objectId} is disabled." )
110112 }
111113 // ensure the measure matches the type declared, and parse it
112114 val parsedValue = BaseType .parseType(measure.value!! , meta.type)
113- if (parsedValue == null ) {
114- log.info(" REJECTED: wrong value given the unit: $measure , $meta " )
115+ if (parsedValue == null ) {
116+ log.info(" REJECTED: <IP: ${request.getIp()} > wrong value given the unit: $measure , $meta " )
115117 throw WrongParamsException (" objectId ${rawMeasure.objectId} : the value '${measure.value} ' does not match " +
116118 " the unit ${meta.unitSymbol} (${meta.type} ) declared in the object definition." )
117119 }
@@ -124,14 +126,14 @@ class InputController(
124126
125127 // ensure no duplicate objectId/timestamp in the data sent
126128 if (valueKeys.contains(key)) {
127- log.info(" REJECTED: duplicate in body: $measure " )
129+ log.info(" REJECTED: <IP: ${request.getIp()} > duplicate in body: $measure " )
128130 throw WrongParamsException (" objectId ${measure.objectId} : two or more values with the same timestamp" )
129131 }
130132 valueKeys.add(key)
131133
132134 // ensure it doesn't already exist in cassandra TODO: find a better way ?
133135 if (rawValueRepository.existsById(rawValue.key)) {
134- log.info(" REJECTED: duplicate in cassandra: $measure " )
136+ log.info(" REJECTED: <IP: ${request.getIp()} > duplicate in cassandra: $measure " )
135137 throw WrongParamsException (" objectId ${rawMeasure.objectId} : " +
136138 " a value with the same timestamp (${measure.timestamp} ) already exists for this object." )
137139 }
0 commit comments