Skip to content

Commit

Permalink
Replace deprecated classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Mar 10, 2018
1 parent 4d7c36c commit a58b40a
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 36 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/BaseProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.traccar.database.ActiveDevice;
import org.traccar.helper.DataConverter;
import org.traccar.model.Command;

import javax.xml.bind.DatatypeConverter;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -79,7 +79,7 @@ public void sendDataCommand(ActiveDevice activeDevice, Command command) {
if (activeDevice.getChannel().getPipeline().get(StringEncoder.class) != null) {
activeDevice.write(data);
} else {
activeDevice.write(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary(data)));
activeDevice.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex(data)));
}
} else {
throw new RuntimeException("Command " + command.getType() + " is not supported in protocol " + getName());
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/ExtendedObjectDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.traccar.helper.DataConverter;
import org.traccar.model.Position;

import javax.xml.bind.DatatypeConverter;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
Expand All @@ -39,7 +39,7 @@ private void saveOriginal(Object decodedMessage, Object originalMessage) {
ChannelBuffer buf = (ChannelBuffer) originalMessage;
position.set(Position.KEY_ORIGINAL, ChannelBuffers.hexDump(buf, 0, buf.writerIndex()));
} else if (originalMessage instanceof String) {
position.set(Position.KEY_ORIGINAL, DatatypeConverter.printHexBinary(
position.set(Position.KEY_ORIGINAL, DataConverter.printHex(
((String) originalMessage).getBytes(StandardCharsets.US_ASCII)));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/api/SecurityRequestFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.traccar.Context;
import org.traccar.api.resource.SessionResource;
import org.traccar.helper.DataConverter;
import org.traccar.helper.Log;
import org.traccar.model.User;

Expand All @@ -28,7 +29,6 @@
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.xml.bind.DatatypeConverter;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
Expand All @@ -43,7 +43,7 @@ public class SecurityRequestFilter implements ContainerRequestFilter {

public static String[] decodeBasicAuth(String auth) {
auth = auth.replaceFirst("[B|b]asic ", "");
byte[] decodedBytes = DatatypeConverter.parseBase64Binary(auth);
byte[] decodedBytes = DataConverter.parseBase64(auth);
if (decodedBytes != null && decodedBytes.length > 0) {
return new String(decodedBytes, StandardCharsets.US_ASCII).split(":", 2);
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/api/resource/SessionResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.traccar.Context;
import org.traccar.api.BaseResource;
import org.traccar.helper.DataConverter;
import org.traccar.helper.LogAction;
import org.traccar.model.User;

Expand All @@ -34,7 +35,6 @@
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.DatatypeConverter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
Expand Down Expand Up @@ -63,11 +63,11 @@ public User get(@QueryParam("token") String token) throws SQLException, Unsuppor
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(USER_COOKIE_KEY)) {
byte[] emailBytes = DatatypeConverter.parseBase64Binary(
byte[] emailBytes = DataConverter.parseBase64(
URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name()));
email = new String(emailBytes, StandardCharsets.UTF_8);
} else if (cookie.getName().equals(PASS_COOKIE_KEY)) {
byte[] passwordBytes = DatatypeConverter.parseBase64Binary(
byte[] passwordBytes = DataConverter.parseBase64(
URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name()));
password = new String(passwordBytes, StandardCharsets.UTF_8);
}
Expand Down
47 changes: 47 additions & 0 deletions src/org/traccar/helper/DataConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2018 Anton Tananaev ([email protected])
*
* 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.traccar.helper;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public final class DataConverter {

private DataConverter() {
}

public static byte[] parseHex(String string) {
try {
return Hex.decodeHex(string);
} catch (DecoderException e) {
throw new RuntimeException(e);
}
}

public static String printHex(byte[] data) {
return Hex.encodeHexString(data);
}

public static byte[] parseBase64(String string) {
return Base64.decodeBase64(string);
}

public static String printBase64(byte[] data) {
return Base64.encodeBase64String(data);
}

}
9 changes: 4 additions & 5 deletions src/org/traccar/helper/Hashing.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
Expand Down Expand Up @@ -75,13 +74,13 @@ public static HashingResult createHash(String password) {
RANDOM.nextBytes(salt);
byte[] hash = function(password.toCharArray(), salt);
return new HashingResult(
DatatypeConverter.printHexBinary(hash),
DatatypeConverter.printHexBinary(salt));
DataConverter.printHex(hash),
DataConverter.printHex(salt));
}

public static boolean validatePassword(String password, String hashHex, String saltHex) {
byte[] hash = DatatypeConverter.parseHexBinary(hashHex);
byte[] salt = DatatypeConverter.parseHexBinary(saltHex);
byte[] hash = DataConverter.parseHex(hashHex);
byte[] salt = DataConverter.parseHex(saltHex);
return slowEquals(hash, function(password.toCharArray(), salt));
}

Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/At2000ProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.helper.DataConverter;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.net.SocketAddress;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -84,7 +84,7 @@ protected Object decode(
IvParameterSpec ivSpec = new IvParameterSpec(iv);

SecretKeySpec keySpec = new SecretKeySpec(
DatatypeConverter.parseHexBinary("000102030405060708090a0b0c0d0e0f"), "AES");
DataConverter.parseHex("000102030405060708090a0b0c0d0e0f"), "AES");

cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/EelinkProtocolEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.traccar.BaseProtocolEncoder;
import org.traccar.helper.DataConverter;
import org.traccar.helper.Log;
import org.traccar.model.Command;

import javax.xml.bind.DatatypeConverter;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

Expand All @@ -47,7 +47,7 @@ public static ChannelBuffer encodeContent(
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();

if (connectionless) {
buf.writeBytes(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary('0' + uniqueId)));
buf.writeBytes(ChannelBuffers.wrappedBuffer(DataConverter.parseHex('0' + uniqueId)));
}

buf.writeByte(0x67);
Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/protocol/HuabaoProtocolEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.traccar.BaseProtocolEncoder;
import org.traccar.helper.DataConverter;
import org.traccar.helper.Log;
import org.traccar.model.Command;

import javax.xml.bind.DatatypeConverter;
import java.text.SimpleDateFormat;
import java.util.Date;

Expand All @@ -31,10 +31,10 @@ public class HuabaoProtocolEncoder extends BaseProtocolEncoder {
protected Object encodeCommand(Command command) {

ChannelBuffer id = ChannelBuffers.wrappedBuffer(
DatatypeConverter.parseHexBinary(getUniqueId(command.getDeviceId())));
DataConverter.parseHex(getUniqueId(command.getDeviceId())));

ChannelBuffer data = ChannelBuffers.dynamicBuffer();
byte[] time = DatatypeConverter.parseHexBinary(new SimpleDateFormat("yyMMddHHmmss").format(new Date()));
byte[] time = DataConverter.parseHex(new SimpleDateFormat("yyMMddHHmmss").format(new Date()));

switch (command.getType()) {
case Command.TYPE_ENGINE_STOP:
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/MeiligaoProtocolEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.jboss.netty.buffer.ChannelBuffers;
import org.traccar.BaseProtocolEncoder;
import org.traccar.helper.Checksum;
import org.traccar.helper.DataConverter;
import org.traccar.helper.Log;
import org.traccar.model.Command;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.util.TimeZone;

Expand All @@ -44,7 +44,7 @@ private ChannelBuffer encodeContent(long deviceId, int type, ChannelBuffer conte

buf.writeShort(2 + 2 + 7 + 2 + content.readableBytes() + 2 + 2); // message length

buf.writeBytes(DatatypeConverter.parseHexBinary((getUniqueId(deviceId) + "FFFFFFFFFFFFFF").substring(0, 14)));
buf.writeBytes(DataConverter.parseHex((getUniqueId(deviceId) + "FFFFFFFFFFFFFF").substring(0, 14)));

buf.writeShort(type);

Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/protocol/RuptelaProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.helper.DataConverter;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;

import javax.xml.bind.DatatypeConverter;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Date;
Expand Down Expand Up @@ -196,7 +196,7 @@ protected Object decode(
}

if (channel != null) {
channel.write(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary("0002640113bc")));
channel.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex("0002640113bc")));
}

return positions;
Expand Down Expand Up @@ -229,7 +229,7 @@ protected Object decode(
}

if (channel != null) {
channel.write(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary("00026d01c4a4")));
channel.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex("00026d01c4a4")));
}

return positions;
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/SigfoxProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.traccar.BaseHttpProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.helper.DataConverter;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;

import javax.json.Json;
import javax.json.JsonObject;
import javax.xml.bind.DatatypeConverter;
import java.io.StringReader;
import java.net.SocketAddress;
import java.net.URLDecoder;
Expand Down Expand Up @@ -61,7 +61,7 @@ protected Object decode(
position.setTime(new Date(json.getInt("time") * 1000L));

ChannelBuffer buf = ChannelBuffers.wrappedBuffer(
ByteOrder.LITTLE_ENDIAN, DatatypeConverter.parseHexBinary(json.getString("data")));
ByteOrder.LITTLE_ENDIAN, DataConverter.parseHex(json.getString("data")));

int type = buf.readUnsignedByte() >> 4;
if (type == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/T800xProtocolEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.traccar.BaseProtocolEncoder;
import org.traccar.helper.DataConverter;
import org.traccar.helper.Log;
import org.traccar.model.Command;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;

public class T800xProtocolEncoder extends BaseProtocolEncoder {
Expand All @@ -39,7 +39,7 @@ private ChannelBuffer encodeContent(Command command, String content) {
buf.writeByte(T800xProtocolDecoder.MSG_COMMAND);
buf.writeShort(7 + 8 + 1 + content.length());
buf.writeShort(1); // serial number
buf.writeBytes(DatatypeConverter.parseHexBinary("0" + getUniqueId(command.getDeviceId())));
buf.writeBytes(DataConverter.parseHex("0" + getUniqueId(command.getDeviceId())));
buf.writeByte(MODE_SETTING);
buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII));

Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/WatchProtocolEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package org.traccar.protocol;

import org.traccar.StringProtocolEncoder;
import org.traccar.helper.DataConverter;
import org.traccar.helper.Log;
import org.traccar.model.Command;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
Expand Down Expand Up @@ -68,7 +68,7 @@ private int getEnableFlag(Command command) {
}

private String getBinaryData(Command command) {
byte[] data = DatatypeConverter.parseHexBinary(command.getString(Command.KEY_DATA));
byte[] data = DataConverter.parseHex(command.getString(Command.KEY_DATA));

int encodedLength = data.length;
for (byte b : data) {
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/Xt2400ProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import org.traccar.BaseProtocolDecoder;
import org.traccar.Context;
import org.traccar.DeviceSession;
import org.traccar.helper.DataConverter;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;

import javax.xml.bind.DatatypeConverter;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Date;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void setConfig(String configString) {
Pattern pattern = Pattern.compile(":wycfg pcr\\[\\d+\\] ([0-9a-fA-F]{2})[0-9a-fA-F]{2}([0-9a-fA-F]+)");
Matcher matcher = pattern.matcher(configString);
while (matcher.find()) {
formats.put(Short.parseShort(matcher.group(1), 16), DatatypeConverter.parseHexBinary(matcher.group(2)));
formats.put(Short.parseShort(matcher.group(1), 16), DataConverter.parseHex(matcher.group(2)));
}
}

Expand Down
Loading

0 comments on commit a58b40a

Please sign in to comment.