Search in sources :

Example 36 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project ORCID-Source by ORCID.

the class MailGunManager method sendEmail.

public boolean sendEmail(String from, String to, String subject, String text, String html, boolean custom) {
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api", getApiKey()));
    // determine correct api based off domain.
    WebResource webResource = null;
    String fromEmail = getFromEmail(from);
    if (custom)
        webResource = client.resource(getNotifyApiUrl());
    else if (fromEmail.endsWith("@verify.orcid.org"))
        webResource = client.resource(getVerifyApiUrl());
    else if (fromEmail.endsWith("@notify.orcid.org"))
        webResource = client.resource(getNotifyApiUrl());
    else
        webResource = client.resource(getApiUrl());
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    formData.add("from", from);
    formData.add("to", to);
    formData.add("subject", subject);
    formData.add("text", text);
    if (html != null) {
        formData.add("html", html);
    }
    formData.add("o:testmode", testmode);
    if (testmode.equals("yes"))
        LOGGER.error("Email form data: \n" + formData.toString());
    // sandbox
    if (to.matches(filter)) {
        ClientResponse cr = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);
        if (cr.getStatus() != 200) {
            LOGGER.error("Post MailGunManager.sendEmail not accepted: " + formData.toString());
            return false;
        }
    }
    return true;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 37 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project java-docs-samples by GoogleCloudPlatform.

the class MailgunServlet method sendSimpleMessage.

// CHECKSTYLE OFF: VariableDeclarationUsageDistance
// [START simple]
private ClientResponse sendSimpleMessage(String recipient) {
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
    WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    formData.add("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
    formData.add("to", recipient);
    formData.add("subject", "Simple Mailgun Example");
    formData.add("text", "Plaintext content");
    return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);
}
Also used : WebResource(com.sun.jersey.api.client.WebResource) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 38 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project java-docs-samples by GoogleCloudPlatform.

the class MailgunServlet method sendComplexMessage.

// [END simple]
// [START complex]
private ClientResponse sendComplexMessage(String recipient) {
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
    FormDataMultiPart formData = new FormDataMultiPart();
    formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
    formData.field("to", recipient);
    formData.field("subject", "Complex Mailgun Example");
    formData.field("html", "<html>HTML <strong>content</strong></html>");
    ClassLoader classLoader = getClass().getClassLoader();
    File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
    formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
    WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
    return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, formData);
}
Also used : FormDataMultiPart(com.sun.jersey.multipart.FormDataMultiPart) WebResource(com.sun.jersey.api.client.WebResource) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) File(java.io.File) FileDataBodyPart(com.sun.jersey.multipart.file.FileDataBodyPart)

Example 39 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project atlas by apache.

the class AtlasBaseClient method initializeState.

void initializeState(Configuration configuration, String[] baseUrls, UserGroupInformation ugi, String doAsUser) {
    this.configuration = configuration;
    Client client = getClient(configuration, ugi, doAsUser);
    if ((!AuthenticationUtil.isKerberosAuthenticationEnabled()) && basicAuthUser != null && basicAuthPassword != null) {
        final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(basicAuthUser, basicAuthPassword);
        client.addFilter(authFilter);
    }
    String activeServiceUrl = determineActiveServiceURL(baseUrls, client);
    atlasClientContext = new AtlasClientContext(baseUrls, client, ugi, doAsUser);
    service = client.resource(UriBuilder.fromUri(activeServiceUrl).build());
}
Also used : Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 40 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project activityinfo by bedatadriven.

the class OdkApplicationDriver method queryFormList.

public List<String> queryFormList() {
    Client client = Client.create();
    if (currentUser != null) {
        client.addFilter(new HTTPBasicAuthFilter(currentUser.getEmail(), currentUser.getPassword()));
    }
    Document formList = client.resource(server.getRootUrl()).path("formList").get(Document.class);
    NodeList formNodes = formList.getElementsByTagName("form");
    List<String> forms = new ArrayList<>();
    for (int i = 0; i < formNodes.getLength(); ++i) {
        forms.add(parseFormName(formNodes.item(i)));
    }
    return forms;
}
Also used : NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) Client(com.sun.jersey.api.client.Client) Document(org.w3c.dom.Document) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Aggregations

HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)45 Client (com.sun.jersey.api.client.Client)31 WebResource (com.sun.jersey.api.client.WebResource)19 ClientResponse (com.sun.jersey.api.client.ClientResponse)15 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)14 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)14 LoggingFilter (com.sun.jersey.api.client.filter.LoggingFilter)7 ArrayList (java.util.ArrayList)7 ClientFilter (com.sun.jersey.api.client.filter.ClientFilter)6 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)5 ApacheHttpClient (com.sun.jersey.client.apache.ApacheHttpClient)4 HostnameVerifier (javax.net.ssl.HostnameVerifier)4 SSLSession (javax.net.ssl.SSLSession)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 ClientRequest (com.sun.jersey.api.client.ClientRequest)3 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)3 FormDataMultiPart (com.sun.jersey.multipart.FormDataMultiPart)3 FileInputStream (java.io.FileInputStream)3 URI (java.net.URI)3 KeyStore (java.security.KeyStore)3