List of usage examples for org.apache.commons.httpclient HttpException getReasonCode
public int getReasonCode()
From source file:fr.jayasoft.ivy.url.HttpClientHandler.java
public URLInfo getURLInfo(URL url, int timeout) { HeadMethod head = null;/* w w w.j av a2 s.com*/ try { head = doHead(url, timeout); int status = head.getStatusCode(); head.releaseConnection(); if (status == HttpStatus.SC_OK) { return new URLInfo(true, getResponseContentLength(head), getLastModified(head)); } if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { Message.error("Your proxy requires authentication."); } else if (String.valueOf(status).startsWith("4")) { Message.verbose("CLIENT ERROR: " + head.getStatusText() + " url=" + url); } else if (String.valueOf(status).startsWith("5")) { Message.warn("SERVER ERROR: " + head.getStatusText() + " url=" + url); } Message.debug("HTTP response status: " + status + "=" + head.getStatusText() + " url=" + url); } catch (HttpException e) { Message.error("HttpClientHandler: " + e.getMessage() + ":" + e.getReasonCode() + "=" + e.getReason() + " url=" + url); } catch (UnknownHostException e) { Message.warn("Host " + e.getMessage() + " not found. url=" + url); Message.info( "You probably access the destination server through a proxy server that is not well configured."); } catch (IOException e) { Message.error("HttpClientHandler: " + e.getMessage() + " url=" + url); } finally { if (head != null) { head.releaseConnection(); } } return UNAVAILABLE; }
From source file:com.idega.slide.authentication.IWSlideAuthenticator.java
public void doAuthentication(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; HttpSession session = request.getSession(); LoginBusinessBean loginBusiness = getLoginBusiness(request); try {/*w ww . j a v a2s. c o m*/ if (loginBusiness.isLoggedOn(request)) { LoggedOnInfo lInfo = loginBusiness.getLoggedOnInfo(session); if (lInfo == null) { setAsUnauthenticatedInSlide(session); } else { request = setAsAuthenticatedInSlide(request, lInfo.getLogin(), lInfo); } } else { String[] loginAndPassword = loginBusiness .getLoginNameAndPasswordFromBasicAuthenticationRequest(request); String loggedInUser = getUserAuthenticatedBySlide(session); if (loginAndPassword != null) { String username = loginAndPassword[0]; String password = loginAndPassword[1]; LoggedOnInfo lInfo = loginBusiness.getLoggedOnInfo(session, username); if (loggedInUser == null) { if (isAuthenticated(request, lInfo, username, password)) { request = setAsAuthenticatedInSlide(request, username, lInfo); } else { setAsUnauthenticatedInSlide(session); } } else if (!username.equals(loggedInUser)) { if (isAuthenticated(request, lInfo, username, password)) { request = setAsAuthenticatedInSlide(request, username, lInfo); } else { setAsUnauthenticatedInSlide(session); } } } else if (loggedInUser != null) { setAsUnauthenticatedInSlide(session); } } } catch (HttpException e) { e.printStackTrace(); response.sendError(e.getReasonCode(), e.getReason()); return; } // the slide token is set so that business methods can get it from IWSlideSession. // The WebdavUtils#getSlideToken(request) can be expensive since it copies pointers to all attributes from session to the token. // This is used e.g. to check for permissions(i.e. to calculate permissions using the ACLSecurityImpl) IWSlideSession slideSession = IBOLookup.getSessionInstance(session, IWSlideSession.class); slideSession.setSlideToken(WebdavUtils.getSlideToken(request)); arg2.doFilter(request, response); //2005.05.27 - Gummi //Workaround to ensure that the response is fully flushed. //Needed because of troubles with jakarta-slide. //iwc.getWriter().flush(); }
From source file:com.idega.slide.business.IWSlideSessionBean.java
/** * Differs from the method in the service bean a little. The service bean uses the root credentials but this uses the current users credentials. *///from w w w.j a va2 s . com public boolean getExistence(String path) throws HttpException, IOException { if (path == null) { return false; } try { String pathToCheck = ((path.startsWith(getWebdavServerURI())) ? path : getURI(path)); Enumeration prop = getWebdavRootResource().propfindMethod(pathToCheck, WebdavResource.DISPLAYNAME); return !(prop == null || !prop.hasMoreElements()); } catch (HttpException e) { if (e.getReasonCode() == WebdavStatus.SC_NOT_FOUND) { return false; } else { throw e; } } // return getWebdavRootResource().headMethod(((path.startsWith(getWebdavServerURI()))?path:getURI(path))); }
From source file:com.idega.slide.business.FileSystemCopyServiceBean.java
private void connect() { /*/*ww w. j ava 2 s . com*/ if (!uri.endsWith("/") && !uri.endsWith("\\")) { // append / to the path uri+="/"; } */ //System.out.println("connect " + uri); try { //httpURL = uriToHttpURL(uri); // httpURL.setUserinfo("root","root"); if (this.webdavResource == null) { this.webdavResource = getService().getWebdavExternalResourceAuthenticatedAsRoot(); //webdavResource.setDebug(Integer.MAX_VALUE); // is not a collection? if (!this.webdavResource.getResourceType().isCollection()) { this.webdavResource = null; this.httpURL = null; //System.out.println("Error: " + uri + " is not a collection! Use open/connect only for collections!"); } } else { this.webdavResource.close(); // webdavResource.setHttpURL(httpURL); this.webdavResource = getService().getWebdavExternalResourceAuthenticatedAsRoot(); } setPath(this.webdavResource.getPath() + CoreConstants.PATH_FILES_ROOT); } catch (HttpException we) { System.out.print("HttpException.getReasonCode(): " + we.getReasonCode()); if (we.getReasonCode() == HttpStatus.SC_UNAUTHORIZED) { try { String userName = "root"; if ((userName == null) || (userName.length() == 0)) { disconnect(); return; } userName = userName.trim(); String password = "root"; if (password != null) { password = password.trim(); } try { if (this.webdavResource != null) { this.webdavResource.close(); } } catch (IOException e) { } finally { this.httpURL = null; this.webdavResource = null; } //httpURL = uriToHttpURL(uri); // It should be used like this way. this.httpURL.setUserinfo(userName, password); this.webdavResource = new WebdavResource(this.httpURL); //webdavResource.setDebug(Integer.MAX_VALUE); setPath(this.webdavResource.getPath()); if (!this.webdavResource.getResourceType().isCollection()) { this.webdavResource = null; this.httpURL = null; System.out.println("Error: " + this.httpURL.getURI() + " is not a collection! Use open/connect only for collections!"); } } catch (Exception ex) { handleException(ex); this.httpURL = null; this.webdavResource = null; } } else { handleException(we); this.httpURL = null; this.webdavResource = null; } } catch (Exception ex) { handleException(ex); this.webdavResource = null; this.httpURL = null; } }
From source file:org.apache.ambari.funtest.server.AmbariHttpWebRequest.java
@SuppressWarnings("deprecation") private RuntimeException createRuntimeException(HttpException httpException) { String message = httpException.getMessage(); try {//from w w w . j a v a2s . com JsonElement jsonElement = new JsonParser() .parse(new JsonReader(new StringReader(httpException.getMessage()))); if (jsonElement != null && jsonElement.getAsJsonObject().has("message")) { message = jsonElement.getAsJsonObject().get("message").getAsString(); } } catch (Throwable t) { } if (httpException.getReasonCode() != HttpStatus.SC_OK) { message = httpException.getReasonCode() + " " + httpException.getReason() + ": " + message; } return new RuntimeException(message, httpException); }
From source file:org.apache.cocoon.components.source.impl.WebDAVSource.java
/** * Initialize the SWCL WebdavResource./*from w w w . ja v a 2s. c o m*/ * <p> * The action argument specifies a set of properties to load during initialization. * Its value is one of WebdavResource.NOACTION, WebdavResource.NAME, * WebdavResource.BASIC, WebdavResource.DEFAULT, WebdavResource.ALL. * Similarly the depth argument specifies the depth header of the PROPFIND * method that is executed upon initialization. * </p> * <p> * The different methods of this Source implementation call this method to * initialize the resource using their minimal action and depth requirements. * For instance the WebDAVSource.getMimeType() method requires WebdavResource.BASIC * properties and a search depth of 0 is sufficient. * </p> * <p> * However it may be that a later call (eg. WebDAVSource.getChildren()) requires more * information. In that case the WebdavResource would have to make another call to the Server. * It would be more efficient if previous initialization had been done using depth 1 instead. * In order give the user more control over this the WebDAVSource can be passed a minimal * action and depth using cocoon:webdav-depth and cocoon:webdav-action query string parameters. * By default the mimimum action is WebdavResource.BASIC (which loads all the following basic * webdav properties: DAV:displayname, DAV:getcontentlength, DAV:getcontenttype DAV:resourcetype, * DAV:getlastmodified and DAV:lockdiscovery). The default minimum depth is 1. * </p> * * @param action the set of propterties the WebdavResource should load. * @param depth the webdav depth. * @throws SourceException * @throws SourceNotFoundException */ private void initResource(int action, int depth) throws SourceException, SourceNotFoundException { try { boolean update = false; if (action != WebdavResource.NOACTION) { if (action > this.action) { this.action = action; update = true; } else { action = this.action; } } if (depth > this.depth) { this.depth = depth; update = true; } else { depth = this.depth; } if (this.resource == null) { this.resource = new WebdavResource(this.url, action, depth); } else if (update) { this.resource.setProperties(action, depth); } if (this.action > WebdavResource.NOACTION) { if (this.resource.isCollection()) { String path = this.url.getPath(); if (path.charAt(path.length() - 1) != '/') { this.url.setPath(path + "/"); } } } } catch (HttpException e) { if (e.getReasonCode() == HttpStatus.SC_NOT_FOUND) { throw new SourceNotFoundException("Not found: " + getSecureURI(), e); } if (e.getReasonCode() == HttpStatus.SC_BAD_REQUEST) { throw new SourceException("Server doesn't appear to understand WebDAV: " + getSecureURI(), e); } final String msg = "Could not initialize webdav resource at " + getSecureURI() + ". Server responded " + e.getReasonCode() + " (" + e.getReason() + ") - " + e.getMessage(); throw new SourceException(msg, e); } catch (IOException e) { throw new SourceException("Could not initialize webdav resource", e); } }
From source file:org.apache.cocoon.components.source.impl.WebDAVSource.java
private void resourcesToSax(WebdavResource[] resources, ContentHandler handler) throws SAXException { for (int i = 0; i < resources.length; i++) { if (getLogger().isDebugEnabled()) { final String message = "RESOURCE: " + resources[i].getDisplayName(); getLogger().debug(message);//from www.j a v a 2s. c o m } if (resources[i].isCollection()) { try { WebdavResource[] childs = resources[i].listWebdavResources(); AttributesImpl attrs = new AttributesImpl(); attrs.addAttribute(NAMESPACE, COLLECTION_NAME, PREFIX + ":name", "CDATA", resources[i].getDisplayName()); handler.startElement(NAMESPACE, COLLECTION_NAME, PREFIX + ":" + COLLECTION_NAME, attrs); this.resourcesToSax(childs, handler); handler.endElement(NAMESPACE, COLLECTION_NAME, PREFIX + ":" + COLLECTION_NAME); } catch (HttpException e) { if (getLogger().isDebugEnabled()) { final String message = "Unable to get WebDAV children. Server responded " + e.getReasonCode() + " (" + e.getReason() + ") - " + e.getMessage(); getLogger().debug(message); } } catch (SAXException e) { if (getLogger().isDebugEnabled()) { final String message = "Unable to get WebDAV children: " + e.getMessage(); getLogger().debug(message, e); } } catch (IOException e) { if (getLogger().isDebugEnabled()) { final String message = "Unable to get WebDAV children: " + e.getMessage(); getLogger().debug(message, e); } } catch (Exception e) { if (getLogger().isDebugEnabled()) { final String message = "Unable to get WebDAV children: " + e.getMessage(); getLogger().debug(message, e); } } } else { AttributesImpl attrs = new AttributesImpl(); attrs.addAttribute(NAMESPACE, "name", PREFIX + ":name", "CDATA", resources[i].getDisplayName()); handler.startElement(NAMESPACE, RESOURCE_NAME, PREFIX + ":" + RESOURCE_NAME, attrs); handler.endElement(NAMESPACE, RESOURCE_NAME, PREFIX + ":" + RESOURCE_NAME); } } }
From source file:org.apache.cocoon.components.source.impl.WebDAVSource.java
/** * Get the collection children./*from w w w . j ava 2 s .c o m*/ * * @see org.apache.excalibur.source.TraversableSource#getChildren() */ public Collection getChildren() throws SourceException { initResource(WebdavResource.BASIC, DepthSupport.DEPTH_1); ArrayList children = new ArrayList(); try { WebdavResource[] resources = this.resource.listWebdavResources(); for (int i = 0; i < resources.length; i++) { HttpURL childURL; if (this.url instanceof HttpsURL) { childURL = new HttpsURL((HttpsURL) this.url, resources[i].getName()); } else { childURL = new HttpURL(this.url, resources[i].getName()); } WebDAVSource src = WebDAVSource.newWebDAVSource(resources[i], childURL, this.protocol, getLogger(), this.eventfactory); src.enableLogging(getLogger()); children.add(src); } } catch (HttpException e) { if (getLogger().isDebugEnabled()) { final String message = "Unable to get WebDAV children. Server responded " + e.getReasonCode() + " (" + e.getReason() + ") - " + e.getMessage(); getLogger().debug(message); } throw new SourceException("Failed to get WebDAV collection children.", e); } catch (SourceException e) { throw e; } catch (IOException e) { throw new SourceException("Failed to get WebDAV collection children.", e); } return children; }
From source file:org.apache.cocoon.components.source.impl.WebDAVSource.java
/** * Sets a property for a source.// w ww.ja v a2 s . com * * @param sourceproperty Property of the source * * @throws SourceException If an exception occurs during this operation */ public void setSourceProperty(SourceProperty sourceproperty) throws SourceException { initResource(WebdavResource.NOACTION, DepthSupport.DEPTH_0); try { Node node = null; NodeList list = sourceproperty.getValue().getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if ((list.item(i) instanceof Text && !"".equals(list.item(i).getNodeValue())) || list.item(i) instanceof Element) { node = list.item(i); break; } } Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); format.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); String prop = XMLUtils.serializeNode(node, format); this.resource.proppatchMethod(new PropertyName(sourceproperty.getNamespace(), sourceproperty.getName()), prop, true); } catch (HttpException e) { final String message = "Unable to set property. Server responded " + e.getReasonCode() + " (" + e.getReason() + ") - " + e.getMessage(); getLogger().debug(message); throw new SourceException("Could not set property ", e); } catch (Exception e) { throw new SourceException("Could not set property ", e); } }
From source file:org.apache.cocoon.transformation.WebDAVTransformer.java
private void executeRequest(WebDAVRequestMethod method) throws SAXException { try {/* www . ja va2s.c o m*/ client.executeMethod(method.getHostConfiguration(), method, m_state); super.contentHandler.startPrefixMapping("webdav", NS_URI); // start <response> AttributesImpl atts = new AttributesImpl(); atts.addCDATAAttribute(TARGET_ATTR, m_target); atts.addCDATAAttribute(METHOD_ATTR, m_method); super.contentHandler.startElement(NS_URI, RESPONSE_TAG, NS_PREFIX + RESPONSE_TAG, atts); atts.clear(); // <status> atts.addCDATAAttribute(CODE_ATTR, String.valueOf(method.getStatusCode())); atts.addCDATAAttribute(MSG_ATTR, method.getStatusText()); super.contentHandler.startElement(NS_URI, STATUS_TAG, NS_PREFIX + STATUS_TAG, atts); atts.clear(); super.contentHandler.endElement(NS_URI, STATUS_TAG, NS_PREFIX + STATUS_TAG); // <header>s Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { atts.addCDATAAttribute(NAME_ATTR, headers[i].getName()); atts.addCDATAAttribute(VALUE_ATTR, headers[i].getValue()); super.contentHandler.startElement(NS_URI, HEADER_TAG, NS_PREFIX + HEADER_TAG, atts); atts.clear(); super.contentHandler.endElement(NS_URI, HEADER_TAG, NS_PREFIX + HEADER_TAG); } // response <body> final InputStream in = method.getResponseBodyAsStream(); if (in != null) { String mimeType = null; Header header = method.getResponseHeader("Content-Type"); if (header != null) { mimeType = header.getValue(); int pos = mimeType.indexOf(';'); if (pos != -1) { mimeType = mimeType.substring(0, pos); } } if (mimeType != null && mimeType.equals("text/xml")) { super.contentHandler.startElement(NS_URI, BODY_TAG, NS_PREFIX + BODY_TAG, atts); IncludeXMLConsumer consumer = new IncludeXMLConsumer(super.contentHandler); XMLizer xmlizer = null; try { xmlizer = (XMLizer) manager.lookup(XMLizer.ROLE); xmlizer.toSAX(in, mimeType, m_target, consumer); } catch (ServiceException ce) { throw new SAXException("Missing service dependency: " + XMLizer.ROLE, ce); } finally { manager.release(xmlizer); } super.contentHandler.endElement(NS_URI, BODY_TAG, NS_PREFIX + BODY_TAG); } } // end <response> super.contentHandler.endElement(NS_URI, RESPONSE_TAG, NS_PREFIX + RESPONSE_TAG); super.contentHandler.endPrefixMapping(NS_URI); } catch (HttpException e) { throw new SAXException("Error executing WebDAV request." + " Server responded " + e.getReasonCode() + " (" + e.getReason() + ") - " + e.getMessage(), e); } catch (IOException e) { throw new SAXException("Error executing WebDAV request", e); } }