|
Apache CXF example source code file (PushBack401.java)
The Apache CXF PushBack401.java source code/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.cxf.systest.http; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.util.Arrays; import java.util.List; import java.util.Map; import org.apache.cxf.common.util.Base64Utility; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.message.Exchange; import org.apache.cxf.message.Message; import org.apache.cxf.message.MessageImpl; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.apache.cxf.transport.Conduit; import org.apache.cxf.ws.addressing.EndpointReferenceType; /* * This interceptor will issue 401s * No Authorization Header --> 401 Realm=Cronus * Username Mary --> 401 Realm=Andromeda * Username Edward --> 401 Realm=Zorantius * Username George --> 401 Realm=Cronus * If the password is not "password" a 401 is issued without * realm. */ public class PushBack401 extends AbstractPhaseInterceptor { PushBack401() { super(Phase.RECEIVE); } /** * This function extracts the user:pass token from * the Authorization:Basic header. It returns a two element * String array, the first being the userid, the second * being the password. It returns null, if it cannot parse. */ private String[] extractUserPass(String token) { try { byte[] userpass = Base64Utility.decode(token); String up = IOUtils.newStringFromBytes(userpass); String user = up.substring(0, up.indexOf(':')); String pass = up.substring(up.indexOf(':') + 1); return new String[] {user, pass}; } catch (Exception e) { return null; } } /** * This function returns the realm which depends on * the user name, as follows: * <pre> * Username Mary --> Andromeda * Username Edward --> Zorantius * Username George --> Cronus * </pre> * However, if the password is not "password" this function * throws an exception, regardless. */ private String checkUserPass( String user, String pass ) throws Exception { //System.out.println("Got user: " + user + " pass: " + pass); if (!"password".equals(pass)) { throw new Exception("bad password"); } if ("Mary".equals(user)) { return "Andromeda"; } if ("Edward".equals(user)) { return "Zorantius"; } if ("George".equals(user)) { return "Cronus"; } return null; } @SuppressWarnings("unchecked") public void handleMessage(Message message) throws Fault { Map<String, List Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF PushBack401.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.