Search in sources :

Example 6 with AuthManager

use of org.apache.jmeter.protocol.http.control.AuthManager in project jmeter by apache.

the class HTTPHC4Impl method executeRequest.

/**
     * Execute request either as is or under PrivilegedAction 
     * if a Subject is available for url
     * @param httpClient the {@link CloseableHttpClient} to be used to execute the httpRequest
     * @param httpRequest the {@link HttpRequest} to be executed
     * @param localContext th {@link HttpContext} to be used for execution
     * @param url the target url (will be used to look up a possible subject for the execution)
     * @return the result of the execution of the httpRequest
     * @throws IOException
     * @throws ClientProtocolException
     */
private CloseableHttpResponse executeRequest(final CloseableHttpClient httpClient, final HttpRequestBase httpRequest, final HttpContext localContext, final URL url) throws IOException, ClientProtocolException {
    AuthManager authManager = getAuthManager();
    if (authManager != null) {
        Subject subject = authManager.getSubjectForUrl(url);
        if (subject != null) {
            try {
                return Subject.doAs(subject, (PrivilegedExceptionAction<CloseableHttpResponse>) () -> httpClient.execute(httpRequest, localContext));
            } catch (PrivilegedActionException e) {
                log.error("Can't execute httpRequest with subject: {}", subject, e);
                throw new RuntimeException("Can't execute httpRequest with subject:" + subject, e);
            }
        }
        if (BASIC_AUTH_PREEMPTIVE) {
            Authorization authorization = authManager.getAuthForURL(url);
            if (authorization != null && Mechanism.BASIC_DIGEST.equals(authorization.getMechanism())) {
                HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
                // Create AuthCache instance
                AuthCache authCache = new BasicAuthCache();
                // Generate BASIC scheme object and 
                // add it to the local auth cache
                BasicScheme basicAuth = new BasicScheme();
                authCache.put(target, basicAuth);
                // Add AuthCache to the execution context
                localContext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
            }
        }
    }
    return httpClient.execute(httpRequest, localContext);
}
Also used : Authorization(org.apache.jmeter.protocol.http.control.Authorization) BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) PrivilegedActionException(java.security.PrivilegedActionException) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) Subject(javax.security.auth.Subject)

Example 7 with AuthManager

use of org.apache.jmeter.protocol.http.control.AuthManager in project jmeter by apache.

the class AjpSampler method setConnectionHeaders.

private String setConnectionHeaders(URL url, String host, String method) throws IOException {
    HeaderManager headers = getHeaderManager();
    AuthManager auth = getAuthManager();
    StringBuilder hbuf = new StringBuilder();
    // Allow Headers to override Host setting
    //$NON-NLS-1$
    hbuf.append("Host").append(COLON_SPACE).append(host).append(NEWLINE);
    //Host
    setInt(0xA00b);
    setString(host);
    if (headers != null) {
        for (JMeterProperty jMeterProperty : headers.getHeaders()) {
            Header header = (Header) jMeterProperty.getObjectValue();
            String n = header.getName();
            String v = header.getValue();
            hbuf.append(n).append(COLON_SPACE).append(v).append(NEWLINE);
            int hc = translateHeader(n);
            if (hc > 0) {
                setInt(hc + AJP_HEADER_BASE);
            } else {
                setString(n);
            }
            setString(v);
        }
    }
    if (method.equals(HTTPConstants.POST)) {
        int cl = -1;
        HTTPFileArg[] hfa = getHTTPFiles();
        if (hfa.length > 0) {
            HTTPFileArg fa = hfa[0];
            String fn = fa.getPath();
            File input = new File(fn);
            cl = (int) input.length();
            if (body != null) {
                JOrphanUtils.closeQuietly(body);
                body = null;
            }
            body = new BufferedInputStream(new FileInputStream(input));
            setString(HTTPConstants.HEADER_CONTENT_DISPOSITION);
            setString("form-data; name=\"" + encode(fa.getParamName()) + "\"; filename=\"" + encode(fn) + //$NON-NLS-1$ //$NON-NLS-2$
            "\"");
            String mt = fa.getMimeType();
            hbuf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(COLON_SPACE).append(mt).append(NEWLINE);
            // content-type
            setInt(0xA007);
            setString(mt);
        } else {
            hbuf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(COLON_SPACE).append(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED).append(NEWLINE);
            // content-type
            setInt(0xA007);
            setString(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
            StringBuilder sb = new StringBuilder();
            boolean first = true;
            for (JMeterProperty arg : getArguments()) {
                if (first) {
                    first = false;
                } else {
                    sb.append('&');
                }
                sb.append(arg.getStringValue());
            }
            stringBody = sb.toString();
            // TODO - charset?
            byte[] sbody = stringBody.getBytes();
            cl = sbody.length;
            body = new ByteArrayInputStream(sbody);
        }
        hbuf.append(HTTPConstants.HEADER_CONTENT_LENGTH).append(COLON_SPACE).append(String.valueOf(cl)).append(NEWLINE);
        // Content-length
        setInt(0xA008);
        setString(String.valueOf(cl));
    }
    if (auth != null) {
        String authHeader = auth.getAuthHeaderForURL(url);
        if (authHeader != null) {
            // Authorization
            setInt(0xA005);
            setString(authHeader);
            hbuf.append(HTTPConstants.HEADER_AUTHORIZATION).append(COLON_SPACE).append(authHeader).append(NEWLINE);
        }
    }
    return hbuf.toString();
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) FileInputStream(java.io.FileInputStream) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager) Header(org.apache.jmeter.protocol.http.control.Header) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File)

Example 8 with AuthManager

use of org.apache.jmeter.protocol.http.control.AuthManager in project jmeter by apache.

the class AjpSampler method getHeaderSize.

private int getHeaderSize(String method, URL url) {
    HeaderManager headers = getHeaderManager();
    CookieManager cookies = getCookieManager();
    AuthManager auth = getAuthManager();
    // Host always
    int hsz = 1;
    if (method.equals(HTTPConstants.POST)) {
        HTTPFileArg[] hfa = getHTTPFiles();
        if (hfa.length > 0) {
            hsz += 3;
        } else {
            hsz += 2;
        }
    }
    if (headers != null) {
        hsz += headers.size();
    }
    if (cookies != null) {
        hsz += cookies.getCookieCount();
    }
    if (auth != null) {
        String authHeader = auth.getAuthHeaderForURL(url);
        if (authHeader != null) {
            ++hsz;
        }
    }
    return hsz;
}
Also used : AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) CookieManager(org.apache.jmeter.protocol.http.control.CookieManager) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager)

Aggregations

AuthManager (org.apache.jmeter.protocol.http.control.AuthManager)8 HeaderManager (org.apache.jmeter.protocol.http.control.HeaderManager)2 HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 PrivilegedActionException (java.security.PrivilegedActionException)1 Subject (javax.security.auth.Subject)1 HttpHost (org.apache.http.HttpHost)1 AuthCache (org.apache.http.client.AuthCache)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 BasicScheme (org.apache.http.impl.auth.BasicScheme)1 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)1 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)1 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)1 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)1 Authorization (org.apache.jmeter.protocol.http.control.Authorization)1 CookieManager (org.apache.jmeter.protocol.http.control.CookieManager)1 Header (org.apache.jmeter.protocol.http.control.Header)1