private void logHttpPostRequest(PostMethod method) { try { if (logger.isDebugEnabled()) { logger.debug("/n/n============= HTTP Request Start ============="); logger.debug("HTTP Post Request URL ==>/n" + method.getURI().toString()); logger.debug("HTTP Post Request Headers ==>/n" + getHttpRequestHeader(method)); logger.debug("HTTP Post Request Cookies ==>/n" + getHttpCookie()); logger.debug("HTTP Post Request QueryString ==>/n" + method.getQueryString()); logger.debug("HTTP Post Request Body ==>/n" + getHttpRequestBody(method)); logger.debug("============= HTTP Request End =============/n/n"); } } catch (URIException e) { logger.error("URIException", e); } }
public WebResponse doPost(String url, NameValuePair[] params, String referer) throws WebBrowserException, WebServerException { try { PostMethod postMethod = new PostMethod(url); setHttpRequestHeader(postMethod); if (referer != null && referer.trim().length() != 0) { postMethod.setRequestHeader("Referer", referer); } postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); postMethod.setRequestBody(params); logHttpPostRequest(postMethod); int status = httpClient.executeMethod(postMethod); String strResp = postMethod.getResponseBodyAsString(); byte[] byteResp = postMethod.getResponseBody(); String respEnc = getResponseEncoding(postMethod); logHttpResponse(postMethod, strResp); postMethod.releaseConnection(); // http:301,302,303,307 if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_SEE_OTHER || status == HttpStatus.SC_TEMPORARY_REDIRECT) { Header locationHeader = postMethod.getResponseHeader("Location"); String location = locationHeader.getValue(); if (logger.isDebugEnabled()) { logger.debug("Redirect To Location = " + location); } if (location.startsWith("http")) { return doGet(location); } else { return doGet("http://" + getResponseHost(postMethod) + location); } } else if (status == HttpStatus.SC_OK) { // http:200 return new WebResponse(postMethod.getURI().toString(), byteResp, respEnc); } else { throw new WebServerException("Server Exception[code=" + status + "]"); } } catch (HttpException e) { throw new WebBrowserException(e); } catch (IOException e) { throw new WebBrowserException(e); } }
@Override public void deployItemsToTarget( String site, List<PublishingSyncItem> filteredItems, PublishingTargetItem target) throws ContentNotFoundForPublishingException, UploadFailedException { LOGGER.debug( "Start deploying items for site \"{0}\", target \"{1}\", number of items \"{2}\"", site, target.getName(), filteredItems.size()); URL requestUrl = null; try { requestUrl = new URL(target.getServerUrl()); } catch (MalformedURLException e) { LOGGER.error("Invalid server URL for target {0}", target.getName()); throw new UploadFailedException(site, target.getName(), target.getServerUrl(), e); } ByteArrayPartSource baps = null; PartSource metadataPart = null; StringPart stringPart = null; FilePart filePart = null; int numberOfBuckets = filteredItems.size() / target.getBucketSize() + 1; Iterator<PublishingSyncItem> iter = filteredItems.iterator(); LOGGER.debug( "Divide all deployment items into {0} bucket(s) for target {1}", numberOfBuckets, target.getName()); List<DeploymentEventItem> eventItems = new ArrayList<DeploymentEventItem>(); for (int bucketIndex = 0; bucketIndex < numberOfBuckets; bucketIndex++) { int cntFiles = 0; StringBuilder sbDeletedFiles = new StringBuilder(); List<Part> formParts = new ArrayList<Part>(); formParts.add(new StringPart(PASSWORD_REQUEST_PARAMETER, target.getPassword())); formParts.add(new StringPart(TARGET_REQUEST_PARAMETER, target.getTarget())); String siteId = target.getSiteId(); if (StringUtils.isEmpty(siteId)) { siteId = site; } formParts.add(new StringPart(SITE_REQUEST_PARAMETER, siteId)); LOGGER.debug( "Preparing deployment items (bucket {0}) for target {1}", bucketIndex + 1, target.getName()); int loopSize = (filteredItems.size() - (bucketIndex * target.getBucketSize()) > target.getBucketSize()) ? target.getBucketSize() : filteredItems.size() - bucketIndex * target.getBucketSize(); for (int j = 0; j < loopSize; j++) { if (iter.hasNext()) { PublishingSyncItem item = iter.next(); LOGGER.debug( "Parsing \"{0}\" , site \"{1}\"; for publishing on target \"{2}\"", item.getPath(), item.getSite(), target.getName()); DeploymentEventItem eventItem = new DeploymentEventItem(); eventItem.setSite(item.getSite()); eventItem.setPath(item.getPath()); eventItem.setUser(item.getUser()); eventItem.setDateTime(new Date()); if (item.getAction() == PublishingSyncItem.Action.DELETE) { eventItem.setState(DeploymentEventItem.STATE_DELETED); if (sbDeletedFiles.length() > 0) { sbDeletedFiles.append(FILES_SEPARATOR).append(item.getPath()); } else { sbDeletedFiles.append(item.getPath()); } if (item.getPath().endsWith("/" + _indexFile)) { sbDeletedFiles .append(FILES_SEPARATOR) .append(item.getPath().replace("/" + _indexFile, "")); } } else { if (item.getAction() == PublishingSyncItem.Action.NEW) { eventItem.setState(DeploymentEventItem.STATE_NEW); } else if (item.getAction() == PublishingSyncItem.Action.MOVE) { eventItem.setState(DeploymentEventItem.STATE_MOVED); } else { eventItem.setState(DeploymentEventItem.STATE_UPDATED); } LOGGER.debug("Get content for \"{0}\" , site \"{1}\"", item.getPath(), item.getSite()); InputStream input = _contentRepository.getContent(site, null, item.getEnvironment(), item.getPath()); try { if (input == null || input.available() < 0) { if (!_contentRepository.isFolder(site, item.getPath()) && _contentRepository.contentExists(site, item.getPath())) { baps = null; stringPart = null; filePart = null; formParts = null; throw new ContentNotFoundForPublishingException( site, target.getName(), item.getPath()); } else { // Content does not exist - skip deploying file continue; } } } catch (IOException err) { LOGGER.error( "Error reading input stream for content at path: " + item.getPath() + " site: " + item.getSite()); if (_contentRepository.contentExists(site, item.getPath())) { baps = null; stringPart = null; filePart = null; formParts = null; throw new ContentNotFoundForPublishingException( site, target.getName(), item.getPath()); } else { // Content does not exist - skip deploying file continue; } } String fileName = _contentRepository.getFilename(site, item.getPath()); byte[] byteArray = null; try { byteArray = IOUtils.toByteArray(input); } catch (IOException e) { LOGGER.error("Error while converting input stream to byte array", e); baps = null; stringPart = null; filePart = null; formParts = null; if (_contentRepository.contentExists(site, item.getPath())) { throw new ContentNotFoundForPublishingException( site, target.getName(), item.getPath()); } else { // Content does not exist - skip deploying file continue; } } finally { IOUtils.closeQuietly(input); input = null; } baps = new ByteArrayPartSource(fileName, byteArray); LOGGER.debug( "Create http request parameters for \"{0}\" , site \"{1}\"; publishing on target \"{2}\"", item.getPath(), item.getSite(), target.getName()); int idx = item.getPath().lastIndexOf("/"); String relativePath = item.getPath().substring(0, idx + 1) + fileName; stringPart = new StringPart(CONTENT_LOCATION_REQUEST_PARAMETER + cntFiles, relativePath); formParts.add(stringPart); filePart = new FilePart(CONTENT_FILE_REQUEST_PARAMETER + cntFiles, baps); formParts.add(filePart); if (item.getAction() == PublishingSyncItem.Action.MOVE) { if (item.getOldPath() != null && !item.getOldPath().equalsIgnoreCase(item.getPath())) { LOGGER.debug( "Add old path to be deleted for MOVE action (\"{0}\")", item.getOldPath()); eventItem.setOldPath(item.getOldPath()); if (sbDeletedFiles.length() > 0) { sbDeletedFiles.append(",").append(item.getOldPath()); } else { sbDeletedFiles.append(item.getOldPath()); } if (item.getOldPath().endsWith("/" + _indexFile)) { sbDeletedFiles .append(FILES_SEPARATOR) .append(item.getOldPath().replace("/" + _indexFile, "")); } } } if (target.isSendMetadata()) { LOGGER.debug( "Adding meta data for content \"{0}\" site \"{0}\"", item.getPath(), item.getSite()); InputStream metadataStream = null; try { metadataStream = _contentRepository.getMetadataStream(site, item.getPath()); metadataPart = new ByteArrayPartSource( fileName + ".meta", IOUtils.toByteArray(metadataStream)); formParts.add( new FilePart(METADATA_FILE_REQUEST_PARAMETER + cntFiles, metadataPart)); } catch (IOException e) { LOGGER.error("Error while creating input stream with content metadata", e); baps = null; stringPart = null; filePart = null; formParts = null; } finally { IOUtils.closeQuietly(metadataStream); metadataPart = null; } } } cntFiles++; eventItems.add(eventItem); } } if (sbDeletedFiles.length() > 0) { formParts.add(new StringPart(DELETED_FILES_REQUEST_PARAMETER, sbDeletedFiles.toString())); } LOGGER.debug( "Create http request to deploy bucket {0} for target {1}", bucketIndex + 1, target.getName()); PostMethod postMethod = null; HttpClient client = null; try { LOGGER.debug("Create HTTP Post Method"); postMethod = new PostMethod(requestUrl.toString()); postMethod.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); Part[] parts = new Part[formParts.size()]; for (int i = 0; i < formParts.size(); i++) parts[i] = formParts.get(i); postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); client = new HttpClient(); LOGGER.debug("Execute HTTP POST request \"{0}\"", postMethod.getURI()); int status = client.executeMethod(postMethod); if (status == HttpStatus.SC_OK) { LOGGER.info( "Successfully deployed bucket number {0} on target {1}", bucketIndex + 1, target.getName()); } else { LOGGER.error( "Deployment failed for bucket number {0} on target {1}. Deployment agent returned status {2}", bucketIndex + 1, target.getName(), HttpStatus.getStatusText(status)); throw new UploadFailedException(site, target.getName(), target.getServerUrl()); } } catch (HttpException e) { LOGGER.error( "Publish failed for target {0} due to http protocol exception", target.getName()); throw new UploadFailedException(site, target.getName(), target.getServerUrl(), e); } catch (IOException e) { LOGGER.error( "Publish failed for target {0} due to I/O (transport) exception", target.getName()); throw new UploadFailedException(site, target.getName(), target.getServerUrl(), e); } finally { LOGGER.debug("Release http connection and release resources"); if (client != null) { HttpConnectionManager mgr = client.getHttpConnectionManager(); if (mgr instanceof SimpleHttpConnectionManager) { ((SimpleHttpConnectionManager) mgr).shutdown(); } } if (postMethod != null) { postMethod.releaseConnection(); postMethod = null; client = null; } baps = null; stringPart = null; filePart = null; formParts = null; } } LOGGER.debug( "Publishing deployment event for target \"{0}\" with \"{1}\" items.", target.getName(), eventItems.size()); _contentRepository.publishDeployEvent(target.getName(), eventItems); LOGGER.info("Deployment successful on target {0}", target.getName()); LOGGER.debug( "Finished deploying items for site \"{0}\", target \"{1}\", number of items \"{2}\"", site, target.getName(), filteredItems.size()); }
public void run() { AccountManager am = AccountManager.get(getActivity()); Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity()); OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION)); String url = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + AccountUtils.getWebdavPath(ocv); Log.d("share", "sharing for version " + ocv.toString()); if (ocv.compareTo(new OwnCloudVersion(0x040000)) >= 0) { String APPS_PATH = "/apps/files_sharing/"; String SHARE_PATH = "ajax/share.php"; String SHARED_PATH = "/apps/files_sharing/get.php?token="; final String WEBDAV_SCRIPT = "webdav.php"; final String WEBDAV_FILES_LOCATION = "/files/"; WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient( account, getActivity().getApplicationContext()); HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setMaxConnectionsPerHost(wc.getHostConfiguration(), 5); // wc.getParams().setParameter("http.protocol.single-cookie-header", true); // wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); PostMethod post = new PostMethod( am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + APPS_PATH + SHARE_PATH); post.addRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); post.addRequestHeader( "Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL)); List<NameValuePair> formparams = new ArrayList<NameValuePair>(); Log.d("share", mPath + ""); formparams.add(new BasicNameValuePair("sources", mPath)); formparams.add(new BasicNameValuePair("uid_shared_with", "public")); formparams.add(new BasicNameValuePair("permissions", "0")); post.setRequestEntity( new StringRequestEntity(URLEncodedUtils.format(formparams, HTTP.UTF_8))); int status; try { PropFindMethod find = new PropFindMethod(url + "/"); find.addRequestHeader( "Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL)); Log.d("sharer", "" + url + "/"); for (org.apache.commons.httpclient.Header a : find.getRequestHeaders()) { Log.d("sharer-h", a.getName() + ":" + a.getValue()); } int status2 = wc.executeMethod(find); Log.d("sharer", "propstatus " + status2); GetMethod get = new GetMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + "/"); get.addRequestHeader( "Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL)); status2 = wc.executeMethod(get); Log.d("sharer", "getstatus " + status2); Log.d("sharer", "" + get.getResponseBodyAsString()); for (org.apache.commons.httpclient.Header a : get.getResponseHeaders()) { Log.d("sharer", a.getName() + ":" + a.getValue()); } status = wc.executeMethod(post); for (org.apache.commons.httpclient.Header a : post.getRequestHeaders()) { Log.d("sharer-h", a.getName() + ":" + a.getValue()); } for (org.apache.commons.httpclient.Header a : post.getResponseHeaders()) { Log.d("sharer", a.getName() + ":" + a.getValue()); } String resp = post.getResponseBodyAsString(); Log.d("share", "" + post.getURI().toString()); Log.d("share", "returned status " + status); Log.d("share", " " + resp); if (status != HttpStatus.SC_OK || resp == null || resp.equals("") || resp.startsWith("false")) { return; } JSONObject jsonObject = new JSONObject(resp); String jsonStatus = jsonObject.getString("status"); if (!jsonStatus.equals("success")) throw new Exception("Error while sharing file status != success"); String token = jsonObject.getString("data"); String uri = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + SHARED_PATH + token; Log.d("Actions:shareFile ok", "url: " + uri); } catch (Exception e) { e.printStackTrace(); } } else if (ocv.compareTo(new OwnCloudVersion(0x030000)) >= 0) { } }