This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(glossary export): glossary-pull to download file
- Loading branch information
Alex Eng
committed
May 17, 2016
1 parent
fd6d430
commit 2deb45c
Showing
8 changed files
with
213 additions
and
10 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
...-commands/src/main/java/org/zanata/client/commands/glossary/pull/GlossaryPullCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2011, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.zanata.client.commands.glossary.pull; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.zanata.client.commands.ConfigurableCommand; | ||
import org.zanata.client.commands.OptionsUtil; | ||
import org.zanata.rest.client.ClientUtil; | ||
import org.zanata.rest.client.GlossaryClient; | ||
import org.zanata.rest.client.RestClientFactory; | ||
import org.zanata.util.PathUtil; | ||
|
||
import com.sun.jersey.api.client.ClientResponse; | ||
|
||
/** | ||
* | ||
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a> | ||
* | ||
**/ | ||
public class GlossaryPullCommand extends | ||
ConfigurableCommand<GlossaryPullOptions> { | ||
private static final Logger log = | ||
LoggerFactory.getLogger(GlossaryPullCommand.class); | ||
|
||
private final GlossaryClient client; | ||
|
||
public GlossaryPullCommand(GlossaryPullOptions opts, | ||
RestClientFactory clientFactory) { | ||
super(opts, clientFactory); | ||
client = getClientFactory().getGlossaryClient(); | ||
} | ||
|
||
public GlossaryPullCommand(GlossaryPullOptions opts) { | ||
this(opts, OptionsUtil.createClientFactory(opts)); | ||
} | ||
|
||
@Override | ||
public void run() throws Exception { | ||
String fileType = StringUtils.isEmpty(getOpts().getFileType()) ? "csv" | ||
: getOpts().getFileType(); | ||
if (!fileType.equalsIgnoreCase("po") | ||
&& !fileType.equalsIgnoreCase("csv")) { | ||
throw new RuntimeException( | ||
"Option 'zanata.fileType' is not valid. Please use 'csv' or 'pot'"); | ||
} | ||
|
||
log.info("Server: {}", getOpts().getUrl()); | ||
log.info("Username: {}", getOpts().getUsername()); | ||
log.info("File type: {}", fileType); | ||
if (getOpts().getTransLang() != null | ||
&& getOpts().getTransLang().length > 0) { | ||
log.info("Translation language: {}", getOpts().getTransLang()); | ||
} | ||
|
||
log.info("pulling glossary from server"); | ||
ClientResponse response = | ||
client.downloadFile(fileType, getOpts().getTransLang()); | ||
|
||
if (response.getClientResponseStatus() == ClientResponse.Status.NOT_FOUND) { | ||
log.info("No glossary file in server"); | ||
} else { | ||
ClientUtil.checkResult(response); | ||
InputStream glossaryFile = response.getEntity(InputStream.class); | ||
if (glossaryFile != null) { | ||
try { | ||
String fileName = | ||
ClientUtil.getFileNameFromHeader(response.getHeaders()); | ||
File file = new File(fileName); | ||
PathUtil.makeDirs(file.getParentFile()); | ||
OutputStream out = new FileOutputStream(file); | ||
int read; | ||
byte[] buffer = new byte[1024]; | ||
while ((read = glossaryFile.read(buffer)) != -1) { | ||
out.write(buffer, 0, read); | ||
} | ||
out.flush(); | ||
} finally { | ||
glossaryFile.close(); | ||
} | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...-commands/src/main/java/org/zanata/client/commands/glossary/pull/GlossaryPullOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.zanata.client.commands.glossary.pull; | ||
|
||
import org.zanata.client.commands.ConfigurableOptions; | ||
|
||
public interface GlossaryPullOptions extends ConfigurableOptions { | ||
public String getFileType(); | ||
|
||
public String[] getTransLang(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
zanata-maven-plugin/src/main/java/org/zanata/maven/GlossaryPullMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2011, Red Hat, Inc. and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.zanata.maven; | ||
|
||
import org.zanata.client.commands.glossary.pull.GlossaryPullCommand; | ||
import org.zanata.client.commands.glossary.pull.GlossaryPullOptions; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
/** | ||
* Pull glossary file from Zanata. | ||
* | ||
* @goal glossary-pull | ||
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a> | ||
* | ||
**/ | ||
public class GlossaryPullMojo extends ConfigurableMojo<GlossaryPullOptions> | ||
implements GlossaryPullOptions { | ||
|
||
/** | ||
* File type to be downloaded. | ||
* csv - csv file format with comma separated | ||
* po - a zip file of po files on available locales | ||
* | ||
* @parameter expression="${zanata.fileType}" default-value="csv" | ||
*/ | ||
private String fileType = "csv"; | ||
|
||
/** | ||
* Optional translation languages to pull. Leave empty for all available locales | ||
* | ||
* @parameter expression="${zanata.transLang}" | ||
*/ | ||
private String[] transLang; | ||
|
||
public GlossaryPullMojo() throws Exception { | ||
super(); | ||
} | ||
|
||
@Override | ||
public String getFileType() { | ||
return fileType; | ||
} | ||
|
||
@Override | ||
public GlossaryPullCommand initCommand() { | ||
return new GlossaryPullCommand(this); | ||
} | ||
|
||
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", | ||
justification = "Injected by Maven") | ||
@Override | ||
public String[] getTransLang() { | ||
return transLang; | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return "glossary-pull"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters