Java Code Examples for org.apache.commons.httpclient.header#setName()

The following examples show how to use org.apache.commons.httpclient.header#setName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestClient.java    From maven-framework-project with MIT License 7 votes vote down vote up
public Book getBook(String bookName) throws Exception {

        String output = null;
        try{
            String url = "http://localhost:8080/bookservice/getbook/";

            url = url + URLEncoder.encode(bookName, "UTF-8");

            HttpClient client = new HttpClient();
            PostMethod mPost = new PostMethod(url);
            client.executeMethod( mPost );
            Header mtHeader = new Header();
            mtHeader.setName("content-type");
            mtHeader.setValue("application/x-www-form-urlencoded");
            mtHeader.setName("accept");
            mtHeader.setValue("application/xml");
            mPost.addRequestHeader(mtHeader);
            client.executeMethod(mPost);
            output = mPost.getResponseBodyAsString( );
            mPost.releaseConnection( );
            System.out.println("out : " + output);
        }catch(Exception e){
            throw new Exception("Exception in retriving group page info : " + e);
        }
        return null;
    }
 
Example 2
Source File: RestClient.java    From maven-framework-project with MIT License 6 votes vote down vote up
public void addBook(String bookName, String author) throws Exception {

        String output = null;
        try{
            String url = "http://localhost:8080/bookservice/addbook";
            HttpClient client = new HttpClient();
            PostMethod mPost = new PostMethod(url);
            mPost.addParameter("name", "Naked Sun");
            mPost.addParameter("author", "Issac Asimov");
            Header mtHeader = new Header();
            mtHeader.setName("content-type");
            mtHeader.setValue("application/x-www-form-urlencoded");
            mtHeader.setName("accept");
            mtHeader.setValue("application/xml");
            //mtHeader.setValue("application/json");
            mPost.addRequestHeader(mtHeader);
            client.executeMethod(mPost);
            output = mPost.getResponseBodyAsString( );
            mPost.releaseConnection( );
            System.out.println("output : " + output);
        }catch(Exception e){
            throw new Exception("Exception in adding bucket : " + e);
        }

    }
 
Example 3
Source File: ExternalOAuthValidator.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method gets a string accessToken and validates it and generate the OAuth2ClientApplicationDTO
 * containing the validity and user details if valid.
 *
 * @param token which need to be validated.
 * @return OAuthValidationResponse with the validated results.
 */
public OAuthValidationResponse validateToken(String token) throws RemoteException {
    OAuth2TokenValidationRequestDTO validationRequest = new OAuth2TokenValidationRequestDTO();
    OAuth2TokenValidationRequestDTO_OAuth2AccessToken accessToken =
            new OAuth2TokenValidationRequestDTO_OAuth2AccessToken();
    accessToken.setTokenType(OauthAuthenticatorConstants.BEARER_TOKEN_TYPE);
    accessToken.setIdentifier(token);
    validationRequest.setAccessToken(accessToken);
    OAuth2TokenValidationServiceStub tokenValidationService =
            new OAuth2TokenValidationServiceStub(hostURL);
    ServiceClient client = tokenValidationService._getServiceClient();
    Options options = client.getOptions();
    List<Header> headerList = new ArrayList<>();
    Header header = new Header();
    header.setName(HTTPConstants.HEADER_AUTHORIZATION);
    header.setValue(OauthAuthenticatorConstants.AUTHORIZATION_HEADER_PREFIX_BASIC + " " + getBasicAuthCredentials());
    headerList.add(header);
    options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, headerList);
    client.setOptions(options);
    OAuth2TokenValidationResponseDTO tokenValidationResponse = tokenValidationService.
            findOAuthConsumerIfTokenIsValid(validationRequest).getAccessTokenValidationResponse();
    boolean isValid = tokenValidationResponse.getValid();
    String userName = null;
    String tenantDomain = null;
    if (isValid) {
        userName = MultitenantUtils.getTenantAwareUsername(
                tokenValidationResponse.getAuthorizedUser());
        tenantDomain = MultitenantUtils.
                getTenantDomain(tokenValidationResponse.getAuthorizedUser());
    }
    return new OAuthValidationResponse(userName,tenantDomain,isValid);
}
 
Example 4
Source File: Util.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public static void setAuthHeaders(ServiceClient serviceClient, String username) throws Exception {
    // Set authorization header to service client
    List headerList = new ArrayList();
    Header header = new Header();
    header.setName(HTTPConstants.HEADER_AUTHORIZATION);
    header.setValue(getAuthHeader(username));
    headerList.add(header);
    serviceClient.getOptions().setProperty(HTTPConstants.HTTP_HEADERS, headerList);
}
 
Example 5
Source File: Utility.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Set Auth headers to service client. Singed JWT authentication handler expect username
 * as a claim in order to validate the user. This is an alternative to mutual auth.
 *
 * @param serviceClient Service client.
 * @param username      username which is set in header.
 */

public static void setAuthHeaders(ServiceClient serviceClient, String username) {
    List headerList = new ArrayList();
    Header header = new Header();
    header.setName(HTTPConstants.HEADER_AUTHORIZATION);
    header.setValue(getAuthHeader(username));
    headerList.add(header);
    serviceClient.getOptions().setProperty(HTTPConstants.HTTP_HEADERS, headerList);
}