18 changed files with 222 additions and 32 deletions
@ -0,0 +1,68 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.springframework.http.converter.xml; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
||||
|
import org.springframework.http.MediaType; |
||||
|
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; |
||||
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
||||
|
import org.springframework.util.Assert; |
||||
|
|
||||
|
import java.lang.reflect.Type; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* RestTemplate firstly uses MappingJackson2XmlHttpMessageConverter converter instead of MappingJackson2HttpMessageConverter. |
||||
|
* It produces error UnsupportedMediaType, so this converter had to be shadowed for read and write operations to use the correct converter |
||||
|
*/ |
||||
|
public class MappingJackson2XmlHttpMessageConverter extends AbstractJackson2HttpMessageConverter { |
||||
|
private static final List<MediaType> problemDetailMediaTypes; |
||||
|
|
||||
|
public MappingJackson2XmlHttpMessageConverter() { |
||||
|
this(Jackson2ObjectMapperBuilder.xml().build()); |
||||
|
} |
||||
|
|
||||
|
public MappingJackson2XmlHttpMessageConverter(ObjectMapper objectMapper) { |
||||
|
super(objectMapper, new MediaType[]{new MediaType("application", "xml", StandardCharsets.UTF_8), new MediaType("text", "xml", StandardCharsets.UTF_8), new MediaType("application", "*+xml", StandardCharsets.UTF_8)}); |
||||
|
Assert.isInstanceOf(XmlMapper.class, objectMapper, "XmlMapper required"); |
||||
|
} |
||||
|
|
||||
|
public void setObjectMapper(ObjectMapper objectMapper) { |
||||
|
Assert.isInstanceOf(XmlMapper.class, objectMapper, "XmlMapper required"); |
||||
|
super.setObjectMapper(objectMapper); |
||||
|
} |
||||
|
|
||||
|
protected List<MediaType> getMediaTypesForProblemDetail() { |
||||
|
return problemDetailMediaTypes; |
||||
|
} |
||||
|
|
||||
|
static { |
||||
|
problemDetailMediaTypes = Collections.singletonList(MediaType.APPLICATION_PROBLEM_XML); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean canWrite(Class<?> clazz, MediaType mediaType) { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.common.util; |
||||
|
|
||||
|
import org.springframework.util.ConcurrentReferenceHashMap; |
||||
|
|
||||
|
import java.util.concurrent.ConcurrentMap; |
||||
|
import java.util.concurrent.atomic.AtomicBoolean; |
||||
|
|
||||
|
import static org.springframework.util.ConcurrentReferenceHashMap.ReferenceType.SOFT; |
||||
|
|
||||
|
public class DeduplicationUtil { |
||||
|
|
||||
|
private static final ConcurrentMap<Object, Long> cache = new ConcurrentReferenceHashMap<>(16, SOFT); |
||||
|
|
||||
|
public static boolean alreadyProcessed(Object deduplicationKey, long deduplicationDuration) { |
||||
|
AtomicBoolean alreadyProcessed = new AtomicBoolean(false); |
||||
|
cache.compute(deduplicationKey, (key, lastProcessedTs) -> { |
||||
|
if (lastProcessedTs != null) { |
||||
|
long passed = System.currentTimeMillis() - lastProcessedTs; |
||||
|
if (passed <= deduplicationDuration) { |
||||
|
alreadyProcessed.set(true); |
||||
|
return lastProcessedTs; |
||||
|
} |
||||
|
} |
||||
|
return System.currentTimeMillis(); |
||||
|
}); |
||||
|
return alreadyProcessed.get(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue