Search in sources :

Example 96 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class AbstractVersionSupplier method callVersionService.

/**
 * Call the version service on the given service: dataset, preparation or transformation.
 *
 * @param serviceName the name of the service
 * @return the version of the called service
 */
protected Version callVersionService(String serviceUrl, String serviceName, String entryPoint) {
    HystrixCommand<InputStream> versionCommand = context.getBean(VersionCommand.class, serviceUrl, entryPoint);
    try (InputStream content = versionCommand.execute()) {
        final Version version = mapper.readerFor(Version.class).readValue(content);
        version.setServiceName(serviceName);
        return version;
    } catch (IOException | TDPException e) {
        LOGGER.warn("Unable to get the version of the service {}", serviceName);
        return null;
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Version(org.talend.dataprep.info.Version) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 97 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class DiffMetadata method onExecute.

private HttpRequestBase onExecute(final String dataSetId, final String preparationId, final List<Action> actionsToAdd) {
    // original actions (currently applied on the preparation)
    final List<Action> originalActions;
    try {
        originalActions = objectMapper.readerFor(new TypeReference<List<Action>>() {
        }).readValue(getInput());
    } catch (final IOException e) {
        throw new TDPException(UNABLE_TO_READ_PREPARATION, e, withBuilder().put("id", preparationId).build());
    }
    // prepare the preview parameters out of the preparation actions
    final List<PreviewParameters> previewParameters = IntStream.range(0, actionsToAdd.size()).mapToObj((index) -> {
        try {
            // base actions = original actions + actions to add from 0 to index
            final List<Action> previousActionsToAdd = actionsToAdd.subList(0, index);
            final List<Action> baseActions = new ArrayList<>(originalActions);
            baseActions.addAll(previousActionsToAdd);
            // diff actions actions = base actions + the action to add for diff
            final Action singleActionToAdd = actionsToAdd.get(index);
            final List<Action> diffActions = new ArrayList<>(baseActions);
            diffActions.add(singleActionToAdd);
            return new // 
            PreviewParameters(// 
            serializeActions(baseActions), // 
            serializeActions(diffActions), // 
            dataSetId, // 
            null, // 
            null, HEAD);
        } catch (IOException e) {
            throw new TDPException(UNABLE_TO_READ_PREPARATION, e, withBuilder().put("id", preparationId).build());
        }
    }).collect(toList());
    // create the http action to perform
    try {
        final String uri = transformationServiceUrl + "/transform/diff/metadata";
        final HttpPost transformationCall = new HttpPost(uri);
        transformationCall.setEntity(new StringEntity(objectMapper.writer().writeValueAsString(previewParameters), APPLICATION_JSON));
        return transformationCall;
    } catch (JsonProcessingException e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : IntStream(java.util.stream.IntStream) HttpPost(org.apache.http.client.methods.HttpPost) GenericCommand(org.talend.dataprep.command.GenericCommand) TDPException(org.talend.dataprep.exception.TDPException) PreviewParameters(org.talend.dataprep.transformation.preview.api.PreviewParameters) SCOPE_PROTOTYPE(org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE) Scope(org.springframework.context.annotation.Scope) UNABLE_TO_READ_PREPARATION(org.talend.dataprep.exception.error.PreparationErrorCodes.UNABLE_TO_READ_PREPARATION) ArrayList(java.util.ArrayList) ChainedCommand(org.talend.dataprep.api.service.command.common.ChainedCommand) Action(org.talend.dataprep.api.preparation.Action) TypeReference(com.fasterxml.jackson.core.type.TypeReference) APPLICATION_JSON(org.apache.http.entity.ContentType.APPLICATION_JSON) HEAD(org.talend.dataprep.api.export.ExportParameters.SourceType.HEAD) Defaults.pipeStream(org.talend.dataprep.command.Defaults.pipeStream) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) StringEntity(org.apache.http.entity.StringEntity) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HttpStatus(org.springframework.http.HttpStatus) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Component(org.springframework.stereotype.Component) PreparationGetActions(org.talend.dataprep.command.preparation.PreparationGetActions) CommonErrorCodes(org.talend.dataprep.exception.error.CommonErrorCodes) ExceptionContext.withBuilder(org.talend.daikon.exception.ExceptionContext.withBuilder) InputStream(java.io.InputStream) HttpPost(org.apache.http.client.methods.HttpPost) Action(org.talend.dataprep.api.preparation.Action) IOException(java.io.IOException) TDPException(org.talend.dataprep.exception.TDPException) PreviewParameters(org.talend.dataprep.transformation.preview.api.PreviewParameters) StringEntity(org.apache.http.entity.StringEntity) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 98 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class PreparationCreate method onExecute.

private HttpRequestBase onExecute(Preparation preparation, String folderId) {
    String uri = preparationServiceUrl + "/preparations";
    if (StringUtils.isNotBlank(folderId)) {
        uri += "?folderId=" + folderId;
    }
    HttpPost preparationCreation = new HttpPost(uri);
    // Serialize preparation using configured serialization
    preparationCreation.setHeader("Content-Type", APPLICATION_JSON_VALUE);
    try {
        byte[] preparationJSONValue = objectMapper.writeValueAsBytes(preparation);
        preparationCreation.setEntity(new ByteArrayEntity(preparationJSONValue));
    } catch (IOException e) {
        throw new TDPException(UNABLE_TO_CREATE_PREPARATION, e);
    }
    return preparationCreation;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Defaults.asString(org.talend.dataprep.command.Defaults.asString) IOException(java.io.IOException)

Example 99 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class MailFeedbackSender method send.

@Override
public void send(String subject, String body, String sender) {
    try {
        final String recipientList = StringUtils.join((new HashSet<>(Arrays.asList(recipients))).toArray(), ',');
        subject = subjectPrefix + subject;
        body = bodyPrefix + "<br/>" + body + "<br/>" + bodySuffix;
        InternetAddress from = new InternetAddress(this.sender);
        InternetAddress replyTo = new InternetAddress(sender);
        Properties p = new Properties();
        p.put("mail.smtp.host", smtpHost);
        p.put("mail.smtp.port", smtpPort);
        p.put("mail.smtp.starttls.enable", "true");
        p.put("mail.smtp.auth", "true");
        MailAuthenticator authenticator = new MailAuthenticator(userName, password);
        Session sendMailSession = Session.getInstance(p, authenticator);
        MimeMessage msg = new MimeMessage(sendMailSession);
        msg.setFrom(from);
        msg.setReplyTo(new Address[] { replyTo });
        msg.addRecipients(Message.RecipientType.TO, recipientList);
        msg.setSubject(subject, "UTF-8");
        msg.setSentDate(new Date());
        Multipart mainPart = new MimeMultipart();
        BodyPart html = new MimeBodyPart();
        html.setContent(body, "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        msg.setContent(mainPart);
        Transport.send(msg);
        LOGGER.debug("Sending mail:'{}' to '{}'", subject, recipients);
    } catch (Exception e) {
        throw new TDPException(APIErrorCodes.UNABLE_TO_SEND_MAIL, e);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) TDPException(org.talend.dataprep.exception.TDPException) TDPException(org.talend.dataprep.exception.TDPException) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 100 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class CreateChildFolder method onExecute.

private HttpRequestBase onExecute(final String parentId, final String path) {
    try {
        URIBuilder uriBuilder = new URIBuilder(preparationServiceUrl + "/folders");
        if (parentId != null) {
            uriBuilder.addParameter("parentId", parentId);
        }
        uriBuilder.addParameter("path", path);
        return new HttpPut(uriBuilder.build());
    } catch (URISyntaxException e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) URISyntaxException(java.net.URISyntaxException) HttpPut(org.apache.http.client.methods.HttpPut) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

TDPException (org.talend.dataprep.exception.TDPException)123 IOException (java.io.IOException)43 InputStream (java.io.InputStream)25 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)21 Test (org.junit.Test)17 ApiOperation (io.swagger.annotations.ApiOperation)16 Timed (org.talend.dataprep.metrics.Timed)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 DataSet (org.talend.dataprep.api.dataset.DataSet)13 ServiceBaseTest (org.talend.ServiceBaseTest)11 StringEntity (org.apache.http.entity.StringEntity)10 JsonParser (com.fasterxml.jackson.core.JsonParser)9 URISyntaxException (java.net.URISyntaxException)9 HttpPost (org.apache.http.client.methods.HttpPost)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)9 List (java.util.List)8 URIBuilder (org.apache.http.client.utils.URIBuilder)8 Marker (org.slf4j.Marker)8 ErrorCode (org.talend.daikon.exception.error.ErrorCode)8