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.impl.client.DefaultHttpClient;
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.LoggerFactory;

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

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

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


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

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.slf4j.Logger;
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
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_DATA_ATTR_NAME, requestBodyEncoded);
requestEntity.put(CommonEPConstans.NEXT_PROTOCOL_ATTR_NAME, nextProtocol);
requestEntity.put(CommonEpConstans.REQUEST_KEY_ATTR_NAME, requestKeyEncoded);
requestEntity.put(CommonEpConstans.REQUEST_DATA_ATTR_NAME, requestBodyEncoded);
requestEntity.put(CommonEpConstans.NEXT_PROTOCOL_ATTR_NAME, nextProtocol);

return requestEntity;
}
Expand Down
Expand Up @@ -52,7 +52,7 @@ public void sync() {
public ProfileSyncRequest createProfileRequest() throws IOException {
if (clientState != null && manager != null && properties != null) {
byte[] serializedProfile = manager.getSerializedProfile();
EndpointObjectHash currentProfileHash = EndpointObjectHash.fromSHA1(serializedProfile);
EndpointObjectHash currentProfileHash = EndpointObjectHash.fromSha1(serializedProfile);
if (isProfileOutDated(currentProfileHash)
|| !clientState.isRegistered()
|| clientState.isNeedProfileResync()) {
Expand Down
Expand Up @@ -161,7 +161,7 @@ protected byte[] getDefaultConfigurationData() {
private class HashContainer implements ConfigurationHashContainer {
@Override
public EndpointObjectHash getConfigurationHash() {
return EndpointObjectHash.fromSHA1(getConfigurationData());
return EndpointObjectHash.fromSha1(getConfigurationData());
}
}
}
Expand Up @@ -410,8 +410,8 @@ private KeyPair getOrInitKeyPair(boolean isAutogeneratedKeys) {
@Override
public EndpointKeyHash getEndpointKeyHash() {
if (keyHash == null) {
EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSHA1(getOrInitKeyPair(isAutogeneratedKeys)
.getPublic().getEncoded());
EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSha1(getOrInitKeyPair(isAutogeneratedKeys)
.getPublic().getEncoded());
keyHash = new EndpointKeyHash(new String(base64.encodeBase64(publicKeyHash.getData())));
}
return keyHash;
Expand Down
Expand Up @@ -31,8 +31,8 @@ public class DefaultMetaDataTransportTest {
public void testCreateMetaDataRequest() {
KaaClientProperties properties = Mockito.mock(KaaClientProperties.class);
KaaClientState state = Mockito.mock(KaaClientState.class);
Mockito.when(state.getProfileHash()).thenReturn(EndpointObjectHash.fromSHA1("123"));
EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSHA1("567");
Mockito.when(state.getProfileHash()).thenReturn(EndpointObjectHash.fromSha1("123"));
EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSha1("567");
MetaDataTransport transport = new DefaultMetaDataTransport();
transport.createMetaDataRequest();
transport.setClientProperties(properties);
Expand Down
Expand Up @@ -92,7 +92,7 @@ public void testUpToDateProfile() throws Exception {
byte[] profile = new byte[]{1, 2, 3};
KaaClientState clientState = Mockito.mock(KaaClientState.class);
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);
ProfileManager profileManager = Mockito.mock(ProfileManager.class);
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
public void testProfileHash() throws IOException {
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);
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.HttpClientBuilder;
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.LoggerFactory;

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

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

if (signatureHeader == null) {
throw new IOException("can't verify message");
Expand Down
Expand Up @@ -21,13 +21,13 @@
/**
* 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_RESOLVE_COMMAND = "Resolve"; //NOSONAR
public static final String BOOTSTRAP_RESOLVE_URI = URI_DELIM + BOOTSTRAP_DOMAIN + URI_DELIM //NOSONAR
+ BOOTSTRAP_RESOLVE_COMMAND;
public static final String BOOTSTRAP_RESOLVE_URI = URI_DELIM + BOOTSTRAP_DOMAIN
+ URI_DELIM + BOOTSTRAP_RESOLVE_COMMAND; //NOSONAR

/**
* The Constant RESPONSE_TYPE.
Expand Down
Expand Up @@ -19,9 +19,9 @@
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.
Expand All @@ -36,8 +36,8 @@ public interface CommonEPConstans extends Constants { //NOSONAR
/**
* The Constant ENDPOINT_REGISTER_URI.
*/
public static final String ENDPOINT_REGISTER_URI = URI_DELIM + ENDPOINT_DOMAIN + URI_DELIM //NOSONAR
+ ENDPOINT_REGISTER_COMMAND;
public static final String ENDPOINT_REGISTER_URI = URI_DELIM + ENDPOINT_DOMAIN + //NOSONAR
URI_DELIM + ENDPOINT_REGISTER_COMMAND;

/**
* The Constant ENDPOINT_UPDATE_COMMAND.
Expand All @@ -64,12 +64,14 @@ public interface CommonEPConstans extends Constants { //NOSONAR
/**
* 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.
*/
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
* @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 publicFile = makeDirs(publicKeyFile);
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
* @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();
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
* @return the file
*/
private static File makeDirs(String privateKeyFile) {
File privateFile = new File(privateKeyFile);
if (privateFile.getParentFile() != null && !privateFile.getParentFile().exists() && !privateFile.getParentFile().mkdirs()) {
LOG.warn("Failed to create required directories: {}", privateFile.getParentFile().getAbsolutePath());
if (privateFile.getParentFile() != null && !privateFile.getParentFile().exists()
&& !privateFile.getParentFile().mkdirs()) {
LOG.warn("Failed to create required directories: {}",
privateFile.getParentFile().getAbsolutePath());
}
return privateFile;
}
Expand All @@ -128,8 +133,8 @@ public static KeyPair generateKeyPair(String privateKeyLocation, String publicKe
KeyPair clientKeyPair = generateKeyPair();
saveKeyPair(clientKeyPair, privateKeyLocation, publicKeyLocation);
return clientKeyPair;
} catch (Exception e) {
LOG.error("Error generating client key pair", e);
} catch (Exception ex) {
LOG.error("Error generating client key pair", ex);
}
return null;
}
Expand All @@ -141,13 +146,14 @@ public static KeyPair generateKeyPair(String privateKeyLocation, String publicKe
* @param publicKeyOutput the public key output stream
* @return the key pair
*/
public static KeyPair generateKeyPair(OutputStream privateKeyOutput, OutputStream publicKeyOutput) {
public static KeyPair generateKeyPair(
OutputStream privateKeyOutput, OutputStream publicKeyOutput) {
try {
KeyPair clientKeyPair = generateKeyPair();
saveKeyPair(clientKeyPair, privateKeyOutput, publicKeyOutput);
return clientKeyPair;
} catch (Exception e) {
LOG.error("Error generating client key pair", e);
} catch (Exception ex) {
LOG.error("Error generating client key pair", ex);
}
return null;
}
Expand All @@ -161,17 +167,17 @@ public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
/**
* Gets the public key from file.
*
* @param f the f
* @param file the file
* @return the public
* @throws IOException the i/o 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;
try {
FileInputStream fis = new FileInputStream(f);
FileInputStream fis = new FileInputStream(file);
dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
byte[] keyBytes = new byte[(int) file.length()];
dis.readFully(keyBytes);
return getPublic(keyBytes);
} 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 {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
int len = 0;
while (-1 != (len = input.read(buffer))) {
output.write(buffer, 0, len);
}
byte[] keyBytes = output.toByteArray();

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

/**
* Gets the private key from file.
*
* @param f the f
* @param file the file
* @return the private
* @throws IOException the i/o 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;
try {
FileInputStream fis = new FileInputStream(f);
FileInputStream fis = new FileInputStream(file);
dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
byte[] keyBytes = new byte[(int) file.length()];
dis.readFully(keyBytes);
return getPrivate(keyBytes);
} 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 {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
int len = 0;
while (-1 != (len = input.read(buffer))) {
output.write(buffer, 0, len);
}
byte[] keyBytes = output.toByteArray();

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

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

0 comments on commit 692ee7a

Please sign in to comment.