Skip to content

Commit

Permalink
KAA-1279: Fixed about 50 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sashadidukh committed Sep 29, 2016
1 parent fc69589 commit 692ee7a
Show file tree
Hide file tree
Showing 38 changed files with 210 additions and 193 deletions.
Expand Up @@ -24,7 +24,7 @@
import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.ByteArrayBuffer; import org.apache.http.util.ByteArrayBuffer;
import org.kaaproject.kaa.common.endpoint.CommonEPConstans; import org.kaaproject.kaa.common.endpoint.CommonEpConstans;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


Expand Down Expand Up @@ -135,7 +135,7 @@ private byte[] getResponseBody(HttpResponse response, boolean verifyResponse) th


if (verifyResponse) { if (verifyResponse) {
Header signatureHeader = response Header signatureHeader = response
.getFirstHeader(CommonEPConstans.SIGNATURE_HEADER_NAME); .getFirstHeader(CommonEpConstans.SIGNATURE_HEADER_NAME);


if (signatureHeader == null) { if (signatureHeader == null) {
throw new IOException("can't verify message"); throw new IOException("can't verify message");
Expand Down
Expand Up @@ -595,7 +595,7 @@ protected TransportContext buildTransportContext(KaaClientProperties properties,
LogTransport logTransport = buildLogTransport(); LogTransport logTransport = buildLogTransport();




EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSHA1(kaaClientState.getPublicKey().getEncoded()); EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSha1(kaaClientState.getPublicKey().getEncoded());
MetaDataTransport mdTransport = new DefaultMetaDataTransport(); MetaDataTransport mdTransport = new DefaultMetaDataTransport();
mdTransport.setClientProperties(properties); mdTransport.setClientProperties(properties);
mdTransport.setClientState(kaaClientState); mdTransport.setClientState(kaaClientState);
Expand Down
Expand Up @@ -17,7 +17,7 @@
package org.kaaproject.kaa.client.channel.impl.channels; package org.kaaproject.kaa.client.channel.impl.channels;


import org.kaaproject.kaa.common.Constants; import org.kaaproject.kaa.common.Constants;
import org.kaaproject.kaa.common.endpoint.CommonEPConstans; import org.kaaproject.kaa.common.endpoint.CommonEpConstans;
import org.kaaproject.kaa.common.endpoint.security.MessageEncoderDecoder; import org.kaaproject.kaa.common.endpoint.security.MessageEncoderDecoder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -64,11 +64,11 @@ static LinkedHashMap<String, byte[]> createHttpRequest(byte[] body, MessageEncod
} }
LinkedHashMap<String, byte[]> requestEntity = new LinkedHashMap<String, byte[]>(); //NOSONAR LinkedHashMap<String, byte[]> requestEntity = new LinkedHashMap<String, byte[]>(); //NOSONAR
if (sign) { if (sign) {
requestEntity.put(CommonEPConstans.REQUEST_SIGNATURE_ATTR_NAME, signature); requestEntity.put(CommonEpConstans.REQUEST_SIGNATURE_ATTR_NAME, signature);
} }
requestEntity.put(CommonEPConstans.REQUEST_KEY_ATTR_NAME, requestKeyEncoded); requestEntity.put(CommonEpConstans.REQUEST_KEY_ATTR_NAME, requestKeyEncoded);
requestEntity.put(CommonEPConstans.REQUEST_DATA_ATTR_NAME, requestBodyEncoded); requestEntity.put(CommonEpConstans.REQUEST_DATA_ATTR_NAME, requestBodyEncoded);
requestEntity.put(CommonEPConstans.NEXT_PROTOCOL_ATTR_NAME, nextProtocol); requestEntity.put(CommonEpConstans.NEXT_PROTOCOL_ATTR_NAME, nextProtocol);


return requestEntity; return requestEntity;
} }
Expand Down
Expand Up @@ -52,7 +52,7 @@ public void sync() {
public ProfileSyncRequest createProfileRequest() throws IOException { public ProfileSyncRequest createProfileRequest() throws IOException {
if (clientState != null && manager != null && properties != null) { if (clientState != null && manager != null && properties != null) {
byte[] serializedProfile = manager.getSerializedProfile(); byte[] serializedProfile = manager.getSerializedProfile();
EndpointObjectHash currentProfileHash = EndpointObjectHash.fromSHA1(serializedProfile); EndpointObjectHash currentProfileHash = EndpointObjectHash.fromSha1(serializedProfile);
if (isProfileOutDated(currentProfileHash) if (isProfileOutDated(currentProfileHash)
|| !clientState.isRegistered() || !clientState.isRegistered()
|| clientState.isNeedProfileResync()) { || clientState.isNeedProfileResync()) {
Expand Down
Expand Up @@ -161,7 +161,7 @@ protected byte[] getDefaultConfigurationData() {
private class HashContainer implements ConfigurationHashContainer { private class HashContainer implements ConfigurationHashContainer {
@Override @Override
public EndpointObjectHash getConfigurationHash() { public EndpointObjectHash getConfigurationHash() {
return EndpointObjectHash.fromSHA1(getConfigurationData()); return EndpointObjectHash.fromSha1(getConfigurationData());
} }
} }
} }
Expand Up @@ -410,8 +410,8 @@ private KeyPair getOrInitKeyPair(boolean isAutogeneratedKeys) {
@Override @Override
public EndpointKeyHash getEndpointKeyHash() { public EndpointKeyHash getEndpointKeyHash() {
if (keyHash == null) { if (keyHash == null) {
EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSHA1(getOrInitKeyPair(isAutogeneratedKeys) EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSha1(getOrInitKeyPair(isAutogeneratedKeys)
.getPublic().getEncoded()); .getPublic().getEncoded());
keyHash = new EndpointKeyHash(new String(base64.encodeBase64(publicKeyHash.getData()))); keyHash = new EndpointKeyHash(new String(base64.encodeBase64(publicKeyHash.getData())));
} }
return keyHash; return keyHash;
Expand Down
Expand Up @@ -31,8 +31,8 @@ public class DefaultMetaDataTransportTest {
public void testCreateMetaDataRequest() { public void testCreateMetaDataRequest() {
KaaClientProperties properties = Mockito.mock(KaaClientProperties.class); KaaClientProperties properties = Mockito.mock(KaaClientProperties.class);
KaaClientState state = Mockito.mock(KaaClientState.class); KaaClientState state = Mockito.mock(KaaClientState.class);
Mockito.when(state.getProfileHash()).thenReturn(EndpointObjectHash.fromSHA1("123")); Mockito.when(state.getProfileHash()).thenReturn(EndpointObjectHash.fromSha1("123"));
EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSHA1("567"); EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSha1("567");
MetaDataTransport transport = new DefaultMetaDataTransport(); MetaDataTransport transport = new DefaultMetaDataTransport();
transport.createMetaDataRequest(); transport.createMetaDataRequest();
transport.setClientProperties(properties); transport.setClientProperties(properties);
Expand Down
Expand Up @@ -92,7 +92,7 @@ public void testUpToDateProfile() throws Exception {
byte[] profile = new byte[]{1, 2, 3}; byte[] profile = new byte[]{1, 2, 3};
KaaClientState clientState = Mockito.mock(KaaClientState.class); KaaClientState clientState = Mockito.mock(KaaClientState.class);
Mockito.when(clientState.isRegistered()).thenReturn(true); Mockito.when(clientState.isRegistered()).thenReturn(true);
Mockito.when(clientState.getProfileHash()).thenReturn(EndpointObjectHash.fromSHA1(profile)); Mockito.when(clientState.getProfileHash()).thenReturn(EndpointObjectHash.fromSha1(profile));
KaaClientProperties properties = Mockito.mock(KaaClientProperties.class); KaaClientProperties properties = Mockito.mock(KaaClientProperties.class);
ProfileManager profileManager = Mockito.mock(ProfileManager.class); ProfileManager profileManager = Mockito.mock(ProfileManager.class);
Mockito.when(profileManager.getSerializedProfile()).thenReturn(new byte[]{1, 2, 3}); Mockito.when(profileManager.getSerializedProfile()).thenReturn(new byte[]{1, 2, 3});
Expand Down
Expand Up @@ -178,7 +178,7 @@ public void testRecreateKeys() throws IOException, InvalidKeyException {
@Test @Test
public void testProfileHash() throws IOException { public void testProfileHash() throws IOException {
KaaClientState state = new KaaClientPropertiesState(new FilePersistentStorage(), CommonsBase64.getInstance(), getProperties()); KaaClientState state = new KaaClientPropertiesState(new FilePersistentStorage(), CommonsBase64.getInstance(), getProperties());
EndpointObjectHash hash = EndpointObjectHash.fromSHA1(new byte[]{1, 2, 3}); EndpointObjectHash hash = EndpointObjectHash.fromSha1(new byte[]{1, 2, 3});
state.setProfileHash(hash); state.setProfileHash(hash);
assertEquals(hash, state.getProfileHash()); assertEquals(hash, state.getProfileHash());
} }
Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.kaaproject.kaa.common.endpoint.CommonEPConstans; import org.kaaproject.kaa.common.endpoint.CommonEpConstans;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


Expand Down Expand Up @@ -99,7 +99,7 @@ private byte[] getResponseBody(HttpResponse response, boolean verifyResponse) th


if (verifyResponse) { if (verifyResponse) {
Header signatureHeader = response Header signatureHeader = response
.getFirstHeader(CommonEPConstans.SIGNATURE_HEADER_NAME); .getFirstHeader(CommonEpConstans.SIGNATURE_HEADER_NAME);


if (signatureHeader == null) { if (signatureHeader == null) {
throw new IOException("can't verify message"); throw new IOException("can't verify message");
Expand Down
Expand Up @@ -21,13 +21,13 @@
/** /**
* Common Bootstrap Constants. * Common Bootstrap Constants.
*/ */
public interface CommonBSConstants extends Constants { //NOSONAR public interface CommonBsConstants extends Constants { //NOSONAR


public static final String BOOTSTRAP_DOMAIN = "BS"; //NOSONAR public static final String BOOTSTRAP_DOMAIN = "BS"; //NOSONAR


public static final String BOOTSTRAP_RESOLVE_COMMAND = "Resolve"; //NOSONAR public static final String BOOTSTRAP_RESOLVE_COMMAND = "Resolve"; //NOSONAR
public static final String BOOTSTRAP_RESOLVE_URI = URI_DELIM + BOOTSTRAP_DOMAIN + URI_DELIM //NOSONAR public static final String BOOTSTRAP_RESOLVE_URI = URI_DELIM + BOOTSTRAP_DOMAIN
+ BOOTSTRAP_RESOLVE_COMMAND; + URI_DELIM + BOOTSTRAP_RESOLVE_COMMAND; //NOSONAR


/** /**
* The Constant RESPONSE_TYPE. * The Constant RESPONSE_TYPE.
Expand Down
Expand Up @@ -19,9 +19,9 @@
import org.kaaproject.kaa.common.Constants; import org.kaaproject.kaa.common.Constants;


/** /**
* The Interface CommonEPConstans. * The Interface CommonEpConstans.
*/ */
public interface CommonEPConstans extends Constants { //NOSONAR public interface CommonEpConstans extends Constants { //NOSONAR


/** /**
* The Constant ENDPOINT_DOMAIN. * The Constant ENDPOINT_DOMAIN.
Expand All @@ -36,8 +36,8 @@ public interface CommonEPConstans extends Constants { //NOSONAR
/** /**
* The Constant ENDPOINT_REGISTER_URI. * The Constant ENDPOINT_REGISTER_URI.
*/ */
public static final String ENDPOINT_REGISTER_URI = URI_DELIM + ENDPOINT_DOMAIN + URI_DELIM //NOSONAR public static final String ENDPOINT_REGISTER_URI = URI_DELIM + ENDPOINT_DOMAIN + //NOSONAR
+ ENDPOINT_REGISTER_COMMAND; URI_DELIM + ENDPOINT_REGISTER_COMMAND;


/** /**
* The Constant ENDPOINT_UPDATE_COMMAND. * The Constant ENDPOINT_UPDATE_COMMAND.
Expand All @@ -64,12 +64,14 @@ public interface CommonEPConstans extends Constants { //NOSONAR
/** /**
* The Constant SYNC_URI. * The Constant SYNC_URI.
*/ */
public static final String SYNC_URI = URI_DELIM + ENDPOINT_DOMAIN + URI_DELIM + SYNC_COMMAND; //NOSONAR public static final String SYNC_URI = URI_DELIM + ENDPOINT_DOMAIN + URI_DELIM
+ SYNC_COMMAND; //NOSONAR


/** /**
* The Constant SYNC_URI. * The Constant SYNC_URI.
*/ */
public static final String LONG_SYNC_URI = URI_DELIM + ENDPOINT_DOMAIN + URI_DELIM + LONG_SYNC_COMMAND; //NOSONAR public static final String LONG_SYNC_URI = URI_DELIM + ENDPOINT_DOMAIN + URI_DELIM
+ LONG_SYNC_COMMAND; //NOSONAR




/** /**
Expand Down
Expand Up @@ -64,7 +64,8 @@ private KeyUtil() {
* @param publicKeyFile the public key file * @param publicKeyFile the public key file
* @throws IOException Signals that an I/O exception has occurred. * @throws IOException Signals that an I/O exception has occurred.
*/ */
public static void saveKeyPair(KeyPair keyPair, String privateKeyFile, String publicKeyFile) throws IOException { public static void saveKeyPair(KeyPair keyPair, String privateKeyFile, String publicKeyFile)
throws IOException {
File privateFile = makeDirs(privateKeyFile); File privateFile = makeDirs(privateKeyFile);
File publicFile = makeDirs(publicKeyFile); File publicFile = makeDirs(publicKeyFile);
OutputStream privateKeyOutput = null; OutputStream privateKeyOutput = null;
Expand All @@ -87,7 +88,9 @@ public static void saveKeyPair(KeyPair keyPair, String privateKeyFile, String pu
* @param publicKeyOutput the public key output stream * @param publicKeyOutput the public key output stream
* @throws IOException Signals that an I/O exception has occurred. * @throws IOException Signals that an I/O exception has occurred.
*/ */
public static void saveKeyPair(KeyPair keyPair, OutputStream privateKeyOutput, OutputStream publicKeyOutput) throws IOException { public static void saveKeyPair(
KeyPair keyPair, OutputStream privateKeyOutput, OutputStream publicKeyOutput)
throws IOException {
PrivateKey privateKey = keyPair.getPrivate(); PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic(); PublicKey publicKey = keyPair.getPublic();


Expand All @@ -103,15 +106,17 @@ public static void saveKeyPair(KeyPair keyPair, OutputStream privateKeyOutput, O
} }


/** /**
* Create all required directories * Create all required directories.
* *
* @param privateKeyFile the private key file * @param privateKeyFile the private key file
* @return the file * @return the file
*/ */
private static File makeDirs(String privateKeyFile) { private static File makeDirs(String privateKeyFile) {
File privateFile = new File(privateKeyFile); File privateFile = new File(privateKeyFile);
if (privateFile.getParentFile() != null && !privateFile.getParentFile().exists() && !privateFile.getParentFile().mkdirs()) { if (privateFile.getParentFile() != null && !privateFile.getParentFile().exists()
LOG.warn("Failed to create required directories: {}", privateFile.getParentFile().getAbsolutePath()); && !privateFile.getParentFile().mkdirs()) {
LOG.warn("Failed to create required directories: {}",
privateFile.getParentFile().getAbsolutePath());
} }
return privateFile; return privateFile;
} }
Expand All @@ -128,8 +133,8 @@ public static KeyPair generateKeyPair(String privateKeyLocation, String publicKe
KeyPair clientKeyPair = generateKeyPair(); KeyPair clientKeyPair = generateKeyPair();
saveKeyPair(clientKeyPair, privateKeyLocation, publicKeyLocation); saveKeyPair(clientKeyPair, privateKeyLocation, publicKeyLocation);
return clientKeyPair; return clientKeyPair;
} catch (Exception e) { } catch (Exception ex) {
LOG.error("Error generating client key pair", e); LOG.error("Error generating client key pair", ex);
} }
return null; return null;
} }
Expand All @@ -141,13 +146,14 @@ public static KeyPair generateKeyPair(String privateKeyLocation, String publicKe
* @param publicKeyOutput the public key output stream * @param publicKeyOutput the public key output stream
* @return the key pair * @return the key pair
*/ */
public static KeyPair generateKeyPair(OutputStream privateKeyOutput, OutputStream publicKeyOutput) { public static KeyPair generateKeyPair(
OutputStream privateKeyOutput, OutputStream publicKeyOutput) {
try { try {
KeyPair clientKeyPair = generateKeyPair(); KeyPair clientKeyPair = generateKeyPair();
saveKeyPair(clientKeyPair, privateKeyOutput, publicKeyOutput); saveKeyPair(clientKeyPair, privateKeyOutput, publicKeyOutput);
return clientKeyPair; return clientKeyPair;
} catch (Exception e) { } catch (Exception ex) {
LOG.error("Error generating client key pair", e); LOG.error("Error generating client key pair", ex);
} }
return null; return null;
} }
Expand All @@ -161,17 +167,17 @@ public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
/** /**
* Gets the public key from file. * Gets the public key from file.
* *
* @param f the f * @param file the file
* @return the public * @return the public
* @throws IOException the i/o exception * @throws IOException the i/o exception
* @throws InvalidKeyException invalid key exception * @throws InvalidKeyException invalid key exception
*/ */
public static PublicKey getPublic(File f) throws IOException, InvalidKeyException { public static PublicKey getPublic(File file) throws IOException, InvalidKeyException {
DataInputStream dis = null; DataInputStream dis = null;
try { try {
FileInputStream fis = new FileInputStream(f); FileInputStream fis = new FileInputStream(file);
dis = new DataInputStream(fis); dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()]; byte[] keyBytes = new byte[(int) file.length()];
dis.readFully(keyBytes); dis.readFully(keyBytes);
return getPublic(keyBytes); return getPublic(keyBytes);
} finally { } finally {
Expand All @@ -190,9 +196,9 @@ public static PublicKey getPublic(File f) throws IOException, InvalidKeyExceptio
public static PublicKey getPublic(InputStream input) throws IOException, InvalidKeyException { public static PublicKey getPublic(InputStream input) throws IOException, InvalidKeyException {
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int n = 0; int len = 0;
while (-1 != (n = input.read(buffer))) { while (-1 != (len = input.read(buffer))) {
output.write(buffer, 0, n); output.write(buffer, 0, len);
} }
byte[] keyBytes = output.toByteArray(); byte[] keyBytes = output.toByteArray();


Expand All @@ -211,25 +217,25 @@ public static PublicKey getPublic(byte[] keyBytes) throws InvalidKeyException {
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance(RSA); KeyFactory kf = KeyFactory.getInstance(RSA);
return kf.generatePublic(spec); return kf.generatePublic(spec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
throw new InvalidKeyException(e); throw new InvalidKeyException(ex);
} }
} }


/** /**
* Gets the private key from file. * Gets the private key from file.
* *
* @param f the f * @param file the file
* @return the private * @return the private
* @throws IOException the i/o exception * @throws IOException the i/o exception
* @throws InvalidKeyException invalid key exception * @throws InvalidKeyException invalid key exception
*/ */
public static PrivateKey getPrivate(File f) throws IOException, InvalidKeyException { public static PrivateKey getPrivate(File file) throws IOException, InvalidKeyException {
DataInputStream dis = null; DataInputStream dis = null;
try { try {
FileInputStream fis = new FileInputStream(f); FileInputStream fis = new FileInputStream(file);
dis = new DataInputStream(fis); dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()]; byte[] keyBytes = new byte[(int) file.length()];
dis.readFully(keyBytes); dis.readFully(keyBytes);
return getPrivate(keyBytes); return getPrivate(keyBytes);
} finally { } finally {
Expand All @@ -248,9 +254,9 @@ public static PrivateKey getPrivate(File f) throws IOException, InvalidKeyExcept
public static PrivateKey getPrivate(InputStream input) throws IOException, InvalidKeyException { public static PrivateKey getPrivate(InputStream input) throws IOException, InvalidKeyException {
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int n = 0; int len = 0;
while (-1 != (n = input.read(buffer))) { while (-1 != (len = input.read(buffer))) {
output.write(buffer, 0, n); output.write(buffer, 0, len);
} }
byte[] keyBytes = output.toByteArray(); byte[] keyBytes = output.toByteArray();


Expand All @@ -269,13 +275,13 @@ public static PrivateKey getPrivate(byte[] keyBytes) throws InvalidKeyException
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance(RSA); KeyFactory kf = KeyFactory.getInstance(RSA);
return kf.generatePrivate(spec); return kf.generatePrivate(spec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
throw new InvalidKeyException(e); throw new InvalidKeyException(ex);
} }
} }


/** /**
* Validates RSA public and private key * Validates RSA public and private key.
* *
* @param keyPair the keypair * @param keyPair the keypair
* @return true if keys matches * @return true if keys matches
Expand All @@ -298,8 +304,8 @@ public static boolean validateKeyPair(KeyPair keyPair) {
try { try {
encodedPayload = encDec.encodeData(rawPayload); encodedPayload = encDec.encodeData(rawPayload);
decodedPayload = encDec.decodeData(encodedPayload); decodedPayload = encDec.decodeData(encodedPayload);
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException ex) {
LOG.error("Validation keypair error ", e); LOG.error("Validation keypair error ", ex);
return false; return false;
} }
return Arrays.equals(rawPayload, decodedPayload); return Arrays.equals(rawPayload, decodedPayload);
Expand Down

0 comments on commit 692ee7a

Please sign in to comment.