2
0

Read in site questions from json & don't serialize incomplete MPFileUser

This commit is contained in:
Maarten Billemont 2018-07-23 23:59:11 -04:00
parent 8f7faa9e4e
commit 513840e2c4
5 changed files with 34 additions and 3 deletions

View File

@ -42,6 +42,7 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
private MPResultType defaultType;
private ReadableInstant lastUsed;
private boolean complete;
public MPFileUser(final String fullName) {
this( fullName, null, MPAlgorithm.Version.CURRENT.getAlgorithm() );
@ -131,6 +132,14 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
setChanged();
}
protected boolean isComplete() {
return complete;
}
protected void setComplete() {
complete = true;
}
public File getFile() {
return new File( path, getFullName() + getFormat().fileSuffix() );
}
@ -157,7 +166,8 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
@Override
protected void onChanged() {
try {
getFormat().marshaller().marshall( this );
if (isComplete())
getFormat().marshaller().marshall( this );
}
catch (final MPKeyUnavailableException e) {
logger.wrn( e, "Cannot write out changes for unauthenticated user: %s.", this );

View File

@ -41,6 +41,9 @@ public class MPFlatMarshaller implements MPMarshaller {
@Override
public void marshall(final MPFileUser user)
throws IOException, MPKeyUnavailableException, MPMarshalException, MPAlgorithmException {
if (!user.isComplete())
throw new IllegalStateException( "Cannot marshall an incomplete user: " + user );
StringBuilder content = new StringBuilder();
content.append( "# Master Password site export\n" );
content.append( "# " ).append( user.getContentMode().description() ).append( '\n' );

View File

@ -188,6 +188,7 @@ public class MPFlatUnmarshaller implements MPUnmarshaller {
throw new MPMarshalException( "No full header found in import file." );
}
user.setComplete();
user.endChanges();
}
}

View File

@ -155,8 +155,8 @@ public class MPJSONFile extends MPJSONAnyObject {
String siteName = siteEntry.getKey();
Site fileSite = siteEntry.getValue();
MPFileSite site = new MPFileSite(
user, siteName, fileSite.algorithm.getAlgorithm(), UnsignedInteger.valueOf( fileSite.counter ), fileSite.type,
export.redacted? fileSite.password: null,
user, siteName, fileSite.algorithm.getAlgorithm(), UnsignedInteger.valueOf( fileSite.counter ),
fileSite.type, export.redacted? fileSite.password: null,
fileSite.login_type, export.redacted? fileSite.login_name: null,
(fileSite._ext_mpw != null)? fileSite._ext_mpw.url: null, fileSite.uses,
(fileSite.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( fileSite.last_used ): new Instant() );
@ -169,9 +169,23 @@ public class MPJSONFile extends MPJSONAnyObject {
fileSite.login_name );
}
if (fileSite.questions != null)
for (final Map.Entry<String, Site.Question> questionEntry : fileSite.questions.entrySet()) {
Site.Question fileQuestion = questionEntry.getValue();
MPFileQuestion question = new MPFileQuestion( site, questionEntry.getKey(),
fileQuestion.type, export.redacted? fileQuestion.answer: null );
if (!export.redacted && (fileQuestion.answer != null))
question.setAnswer( (fileQuestion.type != null)? fileQuestion.type: MPResultType.StoredPersonal,
fileQuestion.answer );
site.addQuestion( question );
}
user.addSite( site );
}
user.setComplete();
user.endChanges();
}

View File

@ -37,6 +37,9 @@ public class MPJSONMarshaller implements MPMarshaller {
public void marshall(final MPFileUser user)
throws IOException, MPKeyUnavailableException, MPMarshalException, MPAlgorithmException {
if (!user.isComplete())
throw new IllegalStateException( "Cannot marshall an incomplete user: " + user );
try {
objectMapper.writerWithDefaultPrettyPrinter().writeValue( user.getFile(), new MPJSONFile( user ) );
}