Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
feat(glossary export): glossary-pull to download file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed May 17, 2016
1 parent fd6d430 commit 2deb45c
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 10 deletions.
@@ -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();
}
}
}
}
}
@@ -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();
}
Expand Up @@ -29,8 +29,6 @@
import org.zanata.rest.dto.stats.ContainerTranslationStatistics;
import org.zanata.rest.dto.stats.TranslationStatistics;

import org.apache.commons.csv.CSVPrinter;

import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand Down
Expand Up @@ -32,9 +32,8 @@
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
**/
public class GlossaryDeleteMojo extends
ConfigurableProjectMojo<GlossaryDeleteOptions> implements
GlossaryDeleteOptions {
public class GlossaryDeleteMojo extends ConfigurableMojo<GlossaryDeleteOptions>
implements GlossaryDeleteOptions {

/**
* id of glossary to delete
Expand Down
@@ -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";
}
}
Expand Up @@ -38,9 +38,8 @@
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
**/
public class GlossaryPushMojo extends
ConfigurableProjectMojo<GlossaryPushOptions> implements
GlossaryPushOptions {
public class GlossaryPushMojo extends ConfigurableMojo<GlossaryPushOptions>
implements GlossaryPushOptions {

/**
* Source language of document
Expand Down
Expand Up @@ -32,6 +32,8 @@
import org.zanata.rest.dto.Project;
import org.zanata.rest.service.GlossaryResource;

import com.google.common.base.Joiner;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;

Expand Down Expand Up @@ -60,6 +62,15 @@ public void post(List<GlossaryEntry> glossaryEntries) {
.post(entity);
}

public ClientResponse downloadFile(String fileType, String[] transLang) {
WebResource webResource =
webResource().path("file").queryParam("fileType", fileType);
if (transLang != null && transLang.length > 0) {
webResource.queryParam("locales", Joiner.on(",").join(transLang));
}
return webResource.get(ClientResponse.class);
}

public void delete(String id) {
webResource().path("entries/" + id)
.delete();
Expand Down
Expand Up @@ -50,9 +50,9 @@ public class InvalidContentTypeFilter extends ClientFilter {
"Please check the server URL is correct (in zanata.ini and in zanata.xml) and make sure you use the correct address.";


// we assume only xml or json are the valid types (wildcard type is also considered compatible)
// we assume only xml, json and streaming output are the valid types (wildcard type is also considered compatible)
private static final Pattern VALID_TYPES_REGEX =
Pattern.compile("application/.*\\+?(\\*|xml|json)(;.*)?");
Pattern.compile("application/.*\\+?(\\*|xml|json|octet-stream)(;.*)?");

@Override
public ClientResponse handle(ClientRequest clientRequest)
Expand Down

0 comments on commit 2deb45c

Please sign in to comment.