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 MPResultType defaultType;
private ReadableInstant lastUsed; private ReadableInstant lastUsed;
private boolean complete;
public MPFileUser(final String fullName) { public MPFileUser(final String fullName) {
this( fullName, null, MPAlgorithm.Version.CURRENT.getAlgorithm() ); this( fullName, null, MPAlgorithm.Version.CURRENT.getAlgorithm() );
@ -131,6 +132,14 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
setChanged(); setChanged();
} }
protected boolean isComplete() {
return complete;
}
protected void setComplete() {
complete = true;
}
public File getFile() { public File getFile() {
return new File( path, getFullName() + getFormat().fileSuffix() ); return new File( path, getFullName() + getFormat().fileSuffix() );
} }
@ -157,6 +166,7 @@ public class MPFileUser extends MPBasicUser<MPFileSite> {
@Override @Override
protected void onChanged() { protected void onChanged() {
try { try {
if (isComplete())
getFormat().marshaller().marshall( this ); getFormat().marshaller().marshall( this );
} }
catch (final MPKeyUnavailableException e) { catch (final MPKeyUnavailableException e) {

View File

@ -41,6 +41,9 @@ public class MPFlatMarshaller implements MPMarshaller {
@Override @Override
public void marshall(final MPFileUser user) public void marshall(final MPFileUser user)
throws IOException, MPKeyUnavailableException, MPMarshalException, MPAlgorithmException { throws IOException, MPKeyUnavailableException, MPMarshalException, MPAlgorithmException {
if (!user.isComplete())
throw new IllegalStateException( "Cannot marshall an incomplete user: " + user );
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
content.append( "# Master Password site export\n" ); content.append( "# Master Password site export\n" );
content.append( "# " ).append( user.getContentMode().description() ).append( '\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." ); throw new MPMarshalException( "No full header found in import file." );
} }
user.setComplete();
user.endChanges(); user.endChanges();
} }
} }

View File

@ -155,8 +155,8 @@ public class MPJSONFile extends MPJSONAnyObject {
String siteName = siteEntry.getKey(); String siteName = siteEntry.getKey();
Site fileSite = siteEntry.getValue(); Site fileSite = siteEntry.getValue();
MPFileSite site = new MPFileSite( MPFileSite site = new MPFileSite(
user, siteName, fileSite.algorithm.getAlgorithm(), UnsignedInteger.valueOf( fileSite.counter ), fileSite.type, user, siteName, fileSite.algorithm.getAlgorithm(), UnsignedInteger.valueOf( fileSite.counter ),
export.redacted? fileSite.password: null, fileSite.type, export.redacted? fileSite.password: null,
fileSite.login_type, export.redacted? fileSite.login_name: null, fileSite.login_type, export.redacted? fileSite.login_name: null,
(fileSite._ext_mpw != null)? fileSite._ext_mpw.url: null, fileSite.uses, (fileSite._ext_mpw != null)? fileSite._ext_mpw.url: null, fileSite.uses,
(fileSite.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( fileSite.last_used ): new Instant() ); (fileSite.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( fileSite.last_used ): new Instant() );
@ -169,9 +169,23 @@ public class MPJSONFile extends MPJSONAnyObject {
fileSite.login_name ); 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.addSite( site );
} }
user.setComplete();
user.endChanges(); user.endChanges();
} }

View File

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