JSONUtil<HashMap<String, String>> util = new JSONUtil<>();
HashMap<String, String> parsedData = util.parseJSON(response,
HashMap.class);
+
+ if (parsedData == null) {
+ Log.d("parsedData is null!");
+ return null;
+ }
+
String userId = parsedData.get(userIdKey);
userInfo.setUserid(userId);
private IResponse handlePostSignUp(IRequest request)
throws ServerException {
- if (request.getPayload() == null) {
- throw new BadRequestException("payload is null");
- }
-
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
if (payloadData == null) {
- throw new BadRequestException("CBOR parsing failed");
+ throw new BadRequestException("payload is null");
}
HashMap<String, Object> responsePayload = null;
private IResponse handlePostSigninout(IRequest request)
throws ServerException {
- // exception handle before getting payload
- if (request.getPayload() == null) {
- throw new BadRequestException("payload is empty");
- }
-
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
if (payloadData == null) {
- throw new BadRequestException("CBOR parsing failed");
+ throw new BadRequestException("payload is null");
}
HashMap<String, Object> responsePayload = null;
import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
import org.iotivity.cloud.base.resource.Resource;
import org.iotivity.cloud.util.Cbor;
-import org.iotivity.cloud.util.Log;
public class TokenRefreshResource extends Resource {
private IResponse handlePostRefreshToken(IRequest request)
throws ServerException {
- if (request.getPayload() == null) {
- throw new BadRequestException("payload is null");
- }
-
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
- // temp code
- Log.v(payloadData.toString());
-
if (payloadData == null) {
- throw new BadRequestException("CBOR parsing failed");
+ throw new BadRequestException("payload is null");
}
HashMap<String, Object> responsePayload = null;
import org.iotivity.cloud.accountserver.db.GroupTable;
import org.iotivity.cloud.accountserver.util.TypeCastingManager;
import org.iotivity.cloud.base.device.Device;
+import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
+import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
import org.iotivity.cloud.base.protocols.IRequest;
import org.iotivity.cloud.base.protocols.MessageBuilder;
public void removeMember(HashSet<String> uuid) {
GroupTable groupTable = getGroupTable();
-
+ if (groupTable.getGmid() == null) {
+ throw new InternalServerErrorException("group master is empty");
+ }
if (uuid.contains(groupTable.getGmid())) {
GroupResource.getInstance().deleteGroup(groupTable.getGmid(), mGid);
notifyToSubscriber(getResponsePayload(false));
} else {
+ if (groupTable.getMidlist() == null) {
+ throw new BadRequestException("midList is invalid in Group");
+ }
groupTable.setMidlist(
removeGroupListSet(groupTable.getMidlist(), uuid));
AccountDBManager.getInstance().updateRecord(Constants.GROUP_TABLE,
public void removeDevice(HashSet<String> di) {
GroupTable groupTable = getGroupTable();
-
+ if (groupTable.getDilist() == null) {
+ throw new BadRequestException("deviceList is invalid in Group");
+ }
groupTable.setDilist(removeGroupListSet(groupTable.getDilist(), di));
AccountDBManager.getInstance().updateRecord(Constants.GROUP_TABLE,
public HashMap<String, Object> getInfo(String mid) {
- GroupTable groupTable = getGroupTable();
-
- HashSet<String> midListSet = new HashSet<String>(
- (Collection<? extends String>) groupTable.getMidlist());
-
- if (!midListSet.contains(mid)) {
-
- throw new UnAuthorizedException(
- mid + " is not Group member in gid=" + mGid);
- }
+ verifyGroupTableMid(mid);
return getResponsePayload(true);
}
public HashMap<String, Object> addSubscriber(String mid, Device subscriber,
IRequest request) {
- GroupTable groupTable = getGroupTable();
-
- HashSet<String> midListSet = new HashSet<String>(
- (Collection<? extends String>) groupTable.getMidlist());
-
- if (!midListSet.contains(mid)) {
-
- throw new UnAuthorizedException(
- mid + " is not Group member in gid=" + mGid);
- }
+ verifyGroupTableMid(mid);
GroupSubscriber newSubscriber = new GroupSubscriber(subscriber,
request);
public HashMap<String, Object> removeSubscriber(String mid) {
- HashMap<String, Object> responsePayload = null;
+ HashMap<String, Object> responsePayload = getResponsePayload(true);
+
if (mSubscribers.containsKey(mid)) {
mSubscribers.remove(mid);
}
return responsePayload;
}
+ private void verifyGroupTableMid(String mid) {
+
+ GroupTable groupTable = getGroupTable();
+
+ if (groupTable.getMidlist() == null) {
+ throw new BadRequestException("midList is invalid in Group");
+ }
+ HashSet<String> midListSet = new HashSet<String>(
+ (Collection<? extends String>) groupTable.getMidlist());
+
+ if (!midListSet.contains(mid)) {
+
+ throw new UnAuthorizedException(
+ mid + " is not Group member in gid=" + mGid);
+ }
+ }
+
private void notifyToSubscriber(
HashMap<String, Object> notifiyBtyePayloadData) {
synchronized (mSubscribers) {
import org.iotivity.cloud.accountserver.util.TypeCastingManager;
import org.iotivity.cloud.base.device.Device;
import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
+import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
import org.iotivity.cloud.base.protocols.IRequest;
public class GroupManager {
getGroupTable = mTypeGroup.convertMaptoObject(element,
getGroupTable);
+ if (getGroupTable.getGtype() == null) {
+ throw new InternalServerErrorException("gtype is empty");
+ }
+
+ if (getGroupTable.getMidlist() == null) {
+ throw new BadRequestException("midList is invalid in Group");
+ }
+
HashSet<String> midListSet = new HashSet<String>(
(Collection<? extends String>) getGroupTable.getMidlist());
HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
+ if (payloadData == null) {
+ throw new BadRequestException("payload is null");
+ }
+
if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
String uuid = payloadData.get(Constants.REQ_GROUP_MASTER_ID)
.toString();
import org.iotivity.cloud.accountserver.db.InviteTable;
import org.iotivity.cloud.accountserver.util.TypeCastingManager;
import org.iotivity.cloud.base.device.Device;
+import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
import org.iotivity.cloud.base.protocols.IRequest;
import org.iotivity.cloud.base.protocols.MessageBuilder;
import org.iotivity.cloud.base.protocols.enums.ContentFormat;
List<InviteTable> inviteList = getInviteTableList(
Constants.KEYFIELD_INVITE_USER, uid);
- if (inviteList != null) {
+ if (!inviteList.isEmpty()) {
invitePayloadData = new ArrayList<>();
for (InviteTable invite : inviteList) {
HashMap<String, String> inviteElement = new HashMap<>();
List<InviteTable> invitedList = getInviteTableList(
Constants.KEYFIELD_INVITED_USER, uid);
- if (invitedList != null) {
+ if (inviteList.isEmpty()) {
invitedPayloadData = new ArrayList<>();
for (InviteTable invited : invitedList) {
HashMap<String, String> invitedElement = new HashMap<>();
private List<InviteTable> getInviteTableList(String property, String uid) {
InviteTable getInviteTable = new InviteTable();
- ArrayList<InviteTable> inviteList = null;
+ ArrayList<InviteTable> inviteList = new ArrayList<>();
HashMap<String, Object> condition = new HashMap<>();
condition.put(property, uid);
ArrayList<HashMap<String, Object>> mapInviteList = AccountDBManager
.getInstance().selectRecord(Constants.INVITE_TABLE, condition);
- if (!mapInviteList.isEmpty()) {
- inviteList = new ArrayList<>();
+ if (mapInviteList == null) {
+ throw new BadRequestException("uid is invalid");
}
-
for (HashMap<String, Object> mapInviteTable : mapInviteList) {
getInviteTable = mTypeInvite.convertMaptoObject(mapInviteTable,
Map<String, Object> payloadData = mCbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
+ if (payloadData == null) {
+ throw new BadRequestException("CBOR parsing failed");
+ }
+
Map<String, Object> responsePayload = null;
if (payloadData.containsKey(Constants.REQ_CSR)) {
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
public class CertificateBuilder {
- private String mIssuer;
- private String mSubjectCN;
- private String mSubjectC;
- private String mSubjectO;
- private String mSubjectOU;
- private String mSubjectAltName;
- private Date mNotBefore;
- private Date mNotAfter;
- private PrivateKey mPrivKey;
- private PublicKey mPubKey;
- private BigInteger mSerial;
- private static final String BC = BouncyCastleProvider.PROVIDER_NAME;
- private static final String SIGNATURE_ALGORITHM = "SHA256withECDSA";
- private static final String CURVE = "secp256r1";
+ private String mIssuer;
+ private String mSubjectCN;
+ private String mSubjectC;
+ private String mSubjectO;
+ private String mSubjectOU;
+ private String mSubjectAltName;
+ private Date mNotBefore;
+ private Date mNotAfter;
+ private PrivateKey mPrivKey;
+ private PublicKey mPubKey;
+ private BigInteger mSerial;
+ private static final String BC = BouncyCastleProvider.PROVIDER_NAME;
+ private static final String SIGNATURE_ALGORITHM = "SHA256withECDSA";
+ private static final String CURVE = "secp256r1";
private static final String KEY_GENERATOR_ALGORITHM = "ECDSA";
- public CertificateBuilder(String subject, Date notBefore, Date notAfter, BigInteger serial) {
+ public CertificateBuilder(String subject, Date notBefore, Date notAfter,
+ BigInteger serial) {
Security.addProvider(new BouncyCastleProvider());
init(subject, null, notBefore, notAfter, null, null, serial);
}
- public CertificateBuilder(String subject, PublicKey pubKey, Date notBefore, Date notAfter,
- BigInteger serial, CertificatePrivateKeyPair root) {
- X500Name x500name = new X500Name( root.getCertificate().getSubjectX500Principal().getName() );
+ public CertificateBuilder(String subject, PublicKey pubKey, Date notBefore,
+ Date notAfter, BigInteger serial, CertificatePrivateKeyPair root) {
+ X500Name x500name = new X500Name(
+ root.getCertificate().getSubjectX500Principal().getName());
RDN cn = x500name.getRDNs(BCStyle.CN)[0];
- init(subject, IETFUtils.valueToString(cn.getFirst().getValue()), notBefore, notAfter, root.getKey(), pubKey, serial);
+ init(subject, IETFUtils.valueToString(cn.getFirst().getValue()),
+ notBefore, notAfter, root.getKey(), pubKey, serial);
}
- public CertificateBuilder(String subject, String issuer, Date notBefore, Date notAfter,
- PrivateKey privKey, PublicKey pubKey, BigInteger serial) {
+ public CertificateBuilder(String subject, String issuer, Date notBefore,
+ Date notAfter, PrivateKey privKey, PublicKey pubKey,
+ BigInteger serial) {
Security.addProvider(new BouncyCastleProvider());
init(subject, issuer, notBefore, notAfter, privKey, pubKey, serial);
}
- private void init(String subject, String issuer, Date notBefore, Date notAfter,
- PrivateKey privKey, PublicKey pubKey, BigInteger serial) {
+ private void init(String subject, String issuer, Date notBefore,
+ Date notAfter, PrivateKey privKey, PublicKey pubKey,
+ BigInteger serial) {
this.mSubjectCN = subject;
this.mIssuer = issuer;
this.mNotBefore = notBefore;
this.mSubjectAltName = subjectAltName;
}
- public CertificatePrivateKeyPair build()
- throws GeneralSecurityException, OperatorCreationException, CertIOException {
- if(null == mPrivKey && null == mPubKey)
- {
+ public CertificatePrivateKeyPair build() throws GeneralSecurityException,
+ OperatorCreationException, CertIOException {
+ if (null == mPrivKey && null == mPubKey) {
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CURVE);
KeyPairGenerator g = null;
subjectNameBld.addRDN(BCStyle.CN, mSubjectCN);
- if(null != mSubjectOU) {
+ if (null != mSubjectOU) {
subjectNameBld.addRDN(BCStyle.OU, mSubjectOU);
}
- if(null != mSubjectO) {
+ if (null != mSubjectO) {
subjectNameBld.addRDN(BCStyle.O, mSubjectO);
}
- if(null != mSubjectC) {
+ if (null != mSubjectC) {
subjectNameBld.addRDN(BCStyle.C, mSubjectC);
}
X500NameBuilder issuerNameBld = new X500NameBuilder(BCStyle.INSTANCE);
issuerNameBld.addRDN(BCStyle.CN, mIssuer);
- ContentSigner sigGen = new JcaContentSignerBuilder(SIGNATURE_ALGORITHM).setProvider(BC).build(mPrivKey);
+ if (mPrivKey == null || mPubKey == null)
+ throw new CertIOException("mPrivKey or mPubKey is null!");
- X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(issuerNameBld.build(),
- mSerial, mNotBefore, mNotAfter ,subjectNameBld.build(), mPubKey);
+ ContentSigner sigGen = new JcaContentSignerBuilder(SIGNATURE_ALGORITHM)
+ .setProvider(BC).build(mPrivKey);
- if(null != mSubjectAltName) {
- certGen.addExtension(Extension.subjectAlternativeName, false, new DEROctetString(mSubjectAltName.getBytes()));
+ X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(
+ issuerNameBld.build(), mSerial, mNotBefore, mNotAfter,
+ subjectNameBld.build(), mPubKey);
+
+ if (null != mSubjectAltName) {
+ certGen.addExtension(Extension.subjectAlternativeName, false,
+ new DEROctetString(mSubjectAltName.getBytes()));
}
- cert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certGen.build(sigGen));
+ cert = new JcaX509CertificateConverter().setProvider("BC")
+ .getCertificate(certGen.build(sigGen));
return new CertificatePrivateKeyPair(cert, mPrivKey);
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.io.InputStream;
public final class CrlStore {
private static final String CRL_FILE_NAME = "crl";
public static void saveCrl(byte[] crl) {
+
+ FileOutputStream out = null;
try {
- FileOutputStream out = new FileOutputStream(CRL_FILE_NAME);
+ out = new FileOutputStream(CRL_FILE_NAME);
out.write(crl);
- out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
+
+ try {
+ if (out != null)
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
public static byte[] loadCrl() {
+ InputStream f = null;
try {
- InputStream f = new FileInputStream(CRL_FILE_NAME);
+ f = new FileInputStream(CRL_FILE_NAME);
int size = f.available();
byte data[] = new byte[size];
- if(f.read(data) != data.length) {
+ if (f.read(data) != data.length) {
System.err.println("couldn't read crl");
}
f.close();
e.printStackTrace();
}
+ try {
+ if (f != null)
+ f.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
return null;
}
}