List of usage examples for java.util.concurrent.atomic AtomicReference set
public final void set(V newValue)
From source file:no.barentswatch.fiskinfo.BaseActivity.java
/** * This functions creates a dialog which allows the user to export different * map layers.//from w w w .jav a2 s . com * * @param ActivityContext * The context of the current activity. * @return True if the export succeeded, false otherwise. */ public boolean exportMapLayerToUser(Context activityContext) { LayoutInflater layoutInflater = getLayoutInflater(); View view = layoutInflater.inflate(R.layout.dialog_export_metadata, (null)); Button downloadButton = (Button) view.findViewById(R.id.metadataDownloadButton); Button cancelButton = (Button) view.findViewById(R.id.cancel_button); final AlertDialog builder = new AlertDialog.Builder(activityContext).create(); builder.setTitle(R.string.map_export_metadata_title); builder.setView(view); final AtomicReference<String> selectedHeader = new AtomicReference<String>(); final AtomicReference<String> selectedFormat = new AtomicReference<String>(); final ExpandableListView expListView = (ExpandableListView) view .findViewById(R.id.exportMetadataMapServices); final List<String> listDataHeader = new ArrayList<String>(); final HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>(); final Map<String, String> nameToApiNameResolver = new HashMap<String, String>(); JSONArray availableSubscriptions = getSharedCacheOfAvailableSubscriptions(); if (availableSubscriptions == null) { availableSubscriptions = authenticatedGetRequestToBarentswatchAPIService( getString(R.string.my_page_geo_data_service)); setSharedCacheOfAvailableSubscriptions(availableSubscriptions); } for (int i = 0; i < availableSubscriptions.length(); i++) { try { JSONObject currentSub = availableSubscriptions.getJSONObject(i); nameToApiNameResolver.put(currentSub.getString("Name"), currentSub.getString("ApiName")); listDataHeader.add(currentSub.getString("Name")); List<String> availableDownloadFormatsOfCurrentLayer = new ArrayList<String>(); JSONArray availableFormats = currentSub.getJSONArray("Formats"); for (int j = 0; j < availableFormats.length(); j++) { availableDownloadFormatsOfCurrentLayer.add(availableFormats.getString(j)); } listDataChild.put(listDataHeader.get(i), availableDownloadFormatsOfCurrentLayer); System.out .println("item: " + currentSub.getString("Name") + ", " + currentSub.getString("ApiName")); } catch (JSONException e) { e.printStackTrace(); Log.d("ExportMapLAyerToUser", "Invalid JSON returned from API CALL"); return false; } } final ExpandableListAdapter listAdapter = new ExpandableListAdapter(activityContext, listDataHeader, listDataChild); expListView.setAdapter(listAdapter); // Listview on child click listener expListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { selectedHeader.set(nameToApiNameResolver.get(listDataHeader.get(groupPosition))); selectedHeader.set(listDataHeader.get(groupPosition)); selectedFormat.set(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition)); LinearLayout currentlySelected = (LinearLayout) parent.findViewWithTag("currentlySelectedRow"); if (currentlySelected != null) { currentlySelected.getChildAt(0).setBackgroundColor(Color.WHITE); currentlySelected.setTag(null); } ((LinearLayout) v).getChildAt(0).setBackgroundColor(Color.rgb(214, 214, 214)); v.setTag("currentlySelectedRow"); return true; } }); downloadButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { new DownloadMapLayerFromBarentswatchApiInBackground() .execute(nameToApiNameResolver.get(selectedHeader.get()), selectedFormat.get()); builder.dismiss(); } }); cancelButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { builder.dismiss(); } }); builder.setCanceledOnTouchOutside(false); builder.show(); return true; }
From source file:de.hybris.platform.servicelayer.tx.ItemModelTransactionTest.java
@Test public void testManualInvalidationInsideTxRollbacking() throws Exception { if (Config.isHSQLDBUsed()) { return; //should work, but doesn't at the moment. thomas has stacktrace from bamboo. it hangs inside hsqldb }// w w w .j av a 2 s .co m final MediaModel media = setupMedia("cat", "media", MIME_BEFORE); final Media jaloMedia = modelService.getSource(media); final PK mediaPK = media.getPk(); assertEquals(mediaPK, jaloMedia.getPK()); // check before assertEquals(MIME_BEFORE, media.getMime()); assertEquals(MIME_BEFORE, jaloMedia.getMime()); // read in other thread String[] outerMimes = getMimeFromOtherThread(mediaPK); assertEquals(MIME_BEFORE, outerMimes[0]); assertEquals(MIME_BEFORE, outerMimes[1]); // read direct assertEquals(MIME_BEFORE, readMimeViaJDBC(mediaPK)); final Transaction tx = Transaction.current(); final AtomicReference<Exception> rollbackExc = new AtomicReference<Exception>(null); try { tx.execute(// new TransactionBody() { @Override public Object execute() throws Exception { // check before (again) inside tx assertEquals(MIME_BEFORE, media.getMime()); assertEquals(MIME_BEFORE, jaloMedia.getMime()); // read direct inide tx assertEquals(MIME_BEFORE, readMimeViaJDBC(mediaPK)); // update direct (not committed) -> caches stay untouched changeMimeViaJDBC(mediaPK, MIME_AFTER); // read direct again inside tx assertEquals(MIME_AFTER, readMimeViaJDBC(mediaPK)); // read direct again outside tx assertEquals(MIME_BEFORE, readMimeViaJDBC(mediaPK, false)); // must have the old values here assertEquals(MIME_BEFORE, jaloMedia.getMime()); assertEquals(MIME_BEFORE, media.getMime()); modelService.refresh(media); // even after refreshing ! assertEquals(MIME_BEFORE, media.getMime()); // read in other thread String[] outerMimes = getMimeFromOtherThread(mediaPK); assertEquals(MIME_BEFORE, outerMimes[0]); assertEquals(MIME_BEFORE, outerMimes[1]); // now invalidate manually -> should be only effective inside this tx !!! Utilities.invalidateCache(mediaPK); // jalo must show new value immediately assertEquals(MIME_AFTER, jaloMedia.getMime()); // model is still old but that's correct since it has not been refreshed yet assertEquals(MIME_BEFORE, media.getMime()); modelService.refresh(media); // now refresh assertEquals(MIME_AFTER, media.getMime()); // read in other thread-> both must show old value outerMimes = getMimeFromOtherThread(mediaPK); assertEquals(MIME_BEFORE, outerMimes[0]); assertEquals(MIME_BEFORE, outerMimes[1]); final RuntimeException ex = new RuntimeException("rollback please"); rollbackExc.set(ex); throw ex; } }); } catch (final Exception e) { assertEquals(rollbackExc.get(), e); } // after commit all values must be correct assertEquals(MIME_BEFORE, readMimeViaJDBC(mediaPK)); assertEquals(MIME_BEFORE, jaloMedia.getMime()); // please note that media *is stale* after rollback !!! assertEquals(MIME_AFTER, media.getMime()); modelService.refresh(media); // now refresh should correct it assertEquals(MIME_BEFORE, media.getMime()); // read in other thread-> both must show old value outerMimes = getMimeFromOtherThread(mediaPK); assertEquals(MIME_BEFORE, outerMimes[0]); assertEquals(MIME_BEFORE, outerMimes[1]); }
From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.localworkspace.LocalDataAccessLayer.java
private static boolean reconcileLocalWorkspaceHelper(final Workspace workspace, final WebServiceLayer webServiceLayer, final boolean unscannedReconcile, final boolean reconcileMissingFromDisk, final AtomicReference<Failure[]> failures, final AtomicBoolean pendingChangesUpdatedByServer) { Check.notNull(workspace, "workspace"); //$NON-NLS-1$ pendingChangesUpdatedByServer.set(false); final List<PendingChange> convertedAdds = new ArrayList<PendingChange>(); final boolean throwOnProjectRenamed; if (EnvironmentVariables.isDefined(EnvironmentVariables.DD_SUITES_PROJECT_RENAME_UNPATCHED_CLIENT)) { throwOnProjectRenamed = false;/*from ww w. j a v a 2 s .c o m*/ } else { throwOnProjectRenamed = true; } final AtomicReference<GUID> serverPendingChangeSignature = new AtomicReference<GUID>(GUID.EMPTY); // No optimization away of reconciles when sending up MissingFromDisk // rows, since the bit in the header (lvHeader.PendingReconcile) may be // false when we actually have work to do (there are rows in the table // marked MissingFromDisk). if ((unscannedReconcile || !workspace.getWorkspaceWatcher().isScanNecessary()) && !reconcileMissingFromDisk) { // Pre-reconcile final AtomicBoolean hasPendingLocalVersionRows = new AtomicBoolean(true); LocalWorkspaceTransaction transaction = new LocalWorkspaceTransaction(workspace); try { transaction.execute(new LocalVersionHeaderTransaction() { @Override public void invoke(final WorkspaceVersionTableHeader lvh) { hasPendingLocalVersionRows.set(lvh.getPendingReconcile()); } }); } finally { try { transaction.close(); } catch (final IOException e) { throw new VersionControlException(e); } } final AtomicReference<GUID> clientPendingChangeSignature = new AtomicReference<GUID>(GUID.EMPTY); if (!hasPendingLocalVersionRows.get()) { transaction = new LocalWorkspaceTransaction(workspace); try { transaction.execute(new PendingChangesHeaderTransaction() { @Override public void invoke(final LocalPendingChangesTableHeader pch) { clientPendingChangeSignature.set(pch.getClientSignature()); } }); } finally { try { transaction.close(); } catch (final IOException e) { throw new VersionControlException(e); } } final GUID lastServerPendingChangeGuid = workspace.getOfflineCacheData() .getLastServerPendingChangeSignature(); final Calendar lastReconcileTime = workspace.getOfflineCacheData().getLastReconcileTime(); lastReconcileTime.add(Calendar.SECOND, 8); if (lastServerPendingChangeGuid != GUID.EMPTY && clientPendingChangeSignature.get().equals(lastServerPendingChangeGuid) && lastReconcileTime.after(Calendar.getInstance())) { // This reconcile was optimized away with no server call. failures.set(new Failure[0]); return false; } serverPendingChangeSignature.set( webServiceLayer.queryPendingChangeSignature(workspace.getName(), workspace.getOwnerName())); if (serverPendingChangeSignature.get() != GUID.EMPTY && clientPendingChangeSignature.get().equals(serverPendingChangeSignature)) { // This reconcile was optimized away. workspace.getOfflineCacheData() .setLastServerPendingChangeSignature(serverPendingChangeSignature.get()); workspace.getOfflineCacheData().setLastReconcileTime(Calendar.getInstance()); failures.set(new Failure[0]); return false; } } } final AtomicBoolean toReturn = new AtomicBoolean(true); final LocalWorkspaceTransaction transaction = new LocalWorkspaceTransaction(workspace); try { final AtomicReference<Failure[]> delegateFailures = new AtomicReference<Failure[]>(new Failure[0]); final AtomicBoolean delegatePCUpdated = new AtomicBoolean(false); transaction.execute(new AllTablesTransaction() { @Override public void invoke(final LocalWorkspaceProperties wp, final WorkspaceVersionTable lv, final LocalPendingChangesTable pc) { if (!unscannedReconcile) { // The line below has been commented out because we // decided not to force a full scan here, because it // causes significant degradation in UI performance. // // workspace.getWorkspaceWatcher().markPathChanged(null); // // It was an attempt to fix the bug: // Bug 6191: When using local workspaces, get latest // does not get a file that has been deleted from disk. // // The customer has to explicitly invoke // Pending Changes>Actions>Detect local changes // in Team Explorer. // // Note that none of customers reported that as an // issue. // It was detected on our tests only. workspace.getWorkspaceWatcher().scan(wp, lv, pc); } // Pre-reconcile if (!lv.getPendingReconcile() && !reconcileMissingFromDisk && GUID.EMPTY == serverPendingChangeSignature.get()) { serverPendingChangeSignature.set(webServiceLayer .queryPendingChangeSignature(workspace.getName(), workspace.getOwnerName())); if (serverPendingChangeSignature.get() != GUID.EMPTY && pc.getClientSignature().equals(serverPendingChangeSignature.get())) { // This reconcile was optimized away. delegateFailures.set(new Failure[0]); workspace.getOfflineCacheData() .setLastServerPendingChangeSignature(serverPendingChangeSignature.get()); workspace.getOfflineCacheData().setLastReconcileTime(Calendar.getInstance()); toReturn.set(true); return; } } // Acknowledgment of team project renames, if any have been // completed if (wp.getNewProjectRevisionId() > 0) { webServiceLayer.promotePendingWorkspaceMappings(workspace.getName(), workspace.getOwnerName(), wp.getNewProjectRevisionId()); wp.setNewProjectRevisionId(0); } final LocalPendingChange[] pendingChanges = pc.queryByTargetServerItem(ServerPath.ROOT, RecursionType.FULL, null); /* * TEE-specific Code * * In order to support offline property changes, which * cannot be reconciled with * WebServiceLayer.reconcileLocalWorkspace (the property * values can't be sent), we have to pull out the pended * property changes and send them to the server before * reconciling. */ final List<ChangeRequest> propertyRequests = new ArrayList<ChangeRequest>(); for (final LocalPendingChange lpc : pendingChanges) { if (lpc.getChangeType().contains(ChangeType.PROPERTY)) { final PropertyValue[] pv = lpc.getPropertyValues(); final String serverItem = lpc.getTargetServerItem(); if (pv != null && pv.length > 0 && serverItem != null) { final ChangeRequest request = new ChangeRequest( new ItemSpec(serverItem, RecursionType.NONE), new WorkspaceVersionSpec(workspace), RequestType.PROPERTY, ItemType.ANY, VersionControlConstants.ENCODING_UNCHANGED, LockLevel.UNCHANGED, 0, null, false); request.setProperties(pv); propertyRequests.add(request); } } } if (propertyRequests.size() > 0) { ((WebServiceLayerLocalWorkspaces) webServiceLayer).pendChangesInLocalWorkspace( workspace.getName(), workspace.getOwnerName(), propertyRequests.toArray(new ChangeRequest[propertyRequests.size()]), PendChangesOptions.NONE, SupportedFeatures.ALL, new AtomicReference<Failure[]>(), null, null, new AtomicBoolean(), new AtomicReference<ChangePendedFlags>()); // TODO handle failures? } // Back to normal, non-TEE behavior final AtomicBoolean outClearLocalVersionTable = new AtomicBoolean(); final ServerItemLocalVersionUpdate[] lvUpdates = lv.getUpdatesForReconcile(pendingChanges, reconcileMissingFromDisk, outClearLocalVersionTable); ReconcileResult result = webServiceLayer.reconcileLocalWorkspace(workspace.getName(), workspace.getOwnerName(), pc.getClientSignature(), pendingChanges, lvUpdates, outClearLocalVersionTable.get(), throwOnProjectRenamed); // report any failures Failure[] reconcileFailures = result.getFailures(); workspace.getClient().reportFailures(workspace, reconcileFailures); if (reconcileFailures.length > 0) { throw new ReconcileFailedException(reconcileFailures); } GUID newSignature = new GUID(result.getNewSignature()); PendingChange[] newPendingChanges = result.getNewPendingChanges(); // If the local version rows for this local workspace have // been purged from the server, then the server will set // this flag on the result of the next reconcile. if (result.isReplayLocalVersionsRequired()) { // Reconcile a second time. This time, set the // clearLocalVersionTable flag. This way, we know // we have cleared out any lingering local version rows // for this workspace. if (!outClearLocalVersionTable.get()) { result = webServiceLayer.reconcileLocalWorkspace(workspace.getName(), workspace.getOwnerName(), pc.getClientSignature(), pendingChanges, lvUpdates, true /* clearLocalVersionTable */, throwOnProjectRenamed); // Report any failures reconcileFailures = result.getFailures(); workspace.getClient().reportFailures(workspace, reconcileFailures); if (reconcileFailures.length > 0) { throw new ReconcileFailedException(reconcileFailures); } // Grab the new signature and new pending changes newSignature = new GUID(result.getNewSignature()); newPendingChanges = result.getNewPendingChanges(); } // Now, go through the local version table and replay // every row that we have. final List<ServerItemLocalVersionUpdate> replayUpdates = new ArrayList<ServerItemLocalVersionUpdate>( Math.min(lv.getLocalItemsCount(), 1000)); for (final WorkspaceLocalItem lvEntry : lv.queryByServerItem(ServerPath.ROOT, RecursionType.FULL, null, true /* includeDeleted */)) { final ServerItemLocalVersionUpdate replayUpdate = lvEntry .getLocalVersionUpdate(reconcileMissingFromDisk, true /* force */); if (replayUpdate != null) { replayUpdates.add(replayUpdate); // Batch these updates in groups of 1000 items. if (replayUpdates.size() >= 1000) { webServiceLayer.updateLocalVersion(workspace.getName(), workspace.getOwnerName(), replayUpdates.toArray( new ServerItemLocalVersionUpdate[replayUpdates.size()])); replayUpdates.clear(); } } } if (replayUpdates.size() > 0) { webServiceLayer.updateLocalVersion(workspace.getName(), workspace.getOwnerName(), replayUpdates.toArray(new ServerItemLocalVersionUpdate[replayUpdates.size()])); } } if (result.isPendingChangesUpdated()) { delegatePCUpdated.set(true); final Map<String, ItemType> newPendingDeletes = new TreeMap<String, ItemType>( String.CASE_INSENSITIVE_ORDER); for (final PendingChange pendingChange : newPendingChanges) { if (pendingChange.isAdd()) { final LocalPendingChange oldPendingChange = pc .getByTargetServerItem(pendingChange.getServerItem()); if (null == oldPendingChange || !oldPendingChange.isAdd()) { // Before calling ReconcileLocalWorkspace, // we did not have a pending add at this // target server item. convertedAdds.add(pendingChange); } } else if (pendingChange.isDelete()) { // If the server removed any of our presented // pending deletes, we want to know about it so // we can get rid of the local version rows that // we have in the deleted state. The server will // remove our pending deletes when the item has // been destroyed on the server. newPendingDeletes.put( pendingChange.getSourceServerItem() == null ? pendingChange.getServerItem() : pendingChange.getSourceServerItem(), pendingChange.getItemType()); } } for (final LocalPendingChange oldPendingChange : pc .queryByCommittedServerItem(ServerPath.ROOT, RecursionType.FULL, null)) { if (oldPendingChange.isDelete() && !newPendingDeletes.containsKey(oldPendingChange.getCommittedServerItem())) { // We presented this delete to the server for // Reconcile, but the server removed it from the // pending changes manifest. We need to get rid // of the LV rows for // oldPendingChange.CommittedServerItem since // this item is now destroyed. final List<ServerItemIsCommittedTuple> lvRowsToRemove = new ArrayList<ServerItemIsCommittedTuple>(); final RecursionType recursion = oldPendingChange.isRecursiveChange() ? RecursionType.FULL : RecursionType.NONE; // Aggregate up the deleted local version // entries at this committed server item // (or below if it's a folder), and we'll remove // them. for (final WorkspaceLocalItem lvEntry : lv.queryByServerItem( oldPendingChange.getCommittedServerItem(), recursion, null, true /* includeDeleted */)) { if (lvEntry.isDeleted()) { lvRowsToRemove.add(new ServerItemIsCommittedTuple(lvEntry.getServerItem(), lvEntry.isCommitted())); } } for (final ServerItemIsCommittedTuple tuple : lvRowsToRemove) { // We don't need to reconcile the removal of // LV entries marked IsDeleted since they // don't exist on the server anyway. lv.removeByServerItem(tuple.getCommittedServerItem(), tuple.isCommitted(), false); } } } pc.replacePendingChanges(newPendingChanges); } // If all we're doing to LV is marking it reconciled, then // don't use TxF to commit // both tables atomically as this is slower if (!lv.isDirty()) { transaction.setAllowTxF(false); } if (lvUpdates.length > 0) { lv.markAsReconciled(wp, reconcileMissingFromDisk); // If we removed all missing-from-disk items from the // local version table, then we need to remove // the corresponding candidate delete rows for those // items as well. if (reconcileMissingFromDisk) { List<String> candidatesToRemove = null; for (final LocalPendingChange candidateChange : pc .queryCandidatesByTargetServerItem(ServerPath.ROOT, RecursionType.FULL, null)) { if (candidateChange.isDelete()) { if (null == candidatesToRemove) { candidatesToRemove = new ArrayList<String>(); } candidatesToRemove.add(candidateChange.getTargetServerItem()); } } if (null != candidatesToRemove) { for (final String candidateDeleteTargetServerItem : candidatesToRemove) { pc.removeCandidateByTargetServerItem(candidateDeleteTargetServerItem); } // Set the candidates changed to true so that it // refreshes the UI LocalWorkspaceTransaction.getCurrent().setRaisePendingChangeCandidatesChanged(true); } } } newSignature = webServiceLayer.queryPendingChangeSignature(workspace.getName(), workspace.getOwnerName()); if (!newSignature.equals(pc.getClientSignature())) { pc.setClientSignature(newSignature); workspace.getOfflineCacheData().setLastServerPendingChangeSignature(newSignature); } if (!newSignature.equals(pc.getClientSignature())) { pc.setClientSignature(newSignature); workspace.getOfflineCacheData().setLastServerPendingChangeSignature(newSignature); } workspace.getOfflineCacheData().setLastReconcileTime(Calendar.getInstance()); } }); failures.set(delegateFailures.get()); pendingChangesUpdatedByServer.set(delegatePCUpdated.get()); } finally { try { transaction.close(); } catch (final IOException e) { throw new VersionControlException(e); } } if (convertedAdds.size() > 0) { final UpdateLocalVersionQueueOptions options = UpdateLocalVersionQueueOptions.UPDATE_BOTH; final UpdateLocalVersionQueue ulvq = new UpdateLocalVersionQueue(workspace, options); try { for (final PendingChange pc : convertedAdds) { // Every item in this list has a ChangeType of Add. As a // result they are uncommitted items with no committed hash // value, no committed length, and no baseline file GUID. ulvq.queueUpdate(new ClientLocalVersionUpdate(pc.getServerItem(), pc.getItemID(), pc.getLocalItem(), 0 /* localVersion */, DotNETDate.MIN_CALENDAR, pc.getEncoding(), null /* committedHashValue */, 0 /* committedLength */, null /* baselineFileGuid */, null /* pendingChangeTargetServerItem */, null /* properties */)); } } finally { ulvq.close(); } } return toReturn.get(); }
From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.localworkspace.LocalDataAccessLayer.java
/** * * * * @param workspace//from w w w . jav a 2s .co m * @param lv * @param pc * @param changeRequests * @param silent * @param failures * @param onlineOperationRequired * @param invalidateWorkspaceAfterServerCall * @return */ public static GetOperation[] pendRename(final Workspace workspace, final LocalWorkspaceProperties wp, final WorkspaceVersionTable lv, final LocalPendingChangesTable pc, final ChangeRequest[] changeRequests, final boolean silent, final AtomicReference<Failure[]> failures, final AtomicBoolean onlineOperationRequired, final String[] itemPropertyFilters) { Check.notNull(workspace, "workspace"); //$NON-NLS-1$ Check.notNull(changeRequests, "changeRequests"); //$NON-NLS-1$ workspace.getWorkspaceWatcher().scan(wp, lv, pc); final List<Failure> failureList = new ArrayList<Failure>(); final List<GetOperation> getOps = new ArrayList<GetOperation>(); failures.set(null); final WorkingFolder[] workspaceMappings = wp.getWorkingFolders(); for (int i = 0; i < changeRequests.length; i++) { if (null == changeRequests[i]) { continue; } final ParsedItemSpec parsedItemSpec = ParsedItemSpec.fromItemSpec(changeRequests[i].getItemSpec(), wp, lv, pc, ParsedItemSpecOptions.NONE, failureList); if (null == parsedItemSpec) { continue; } final String sourceRoot = parsedItemSpec.getTargetItem(); String targetRoot = changeRequests[i].getTargetItem(); final String sourceServerRoot = tryGetServerItem(sourceRoot, wp, lv, pc); String targetServerRoot = null; boolean isCloaked = false; if (ServerPath.isServerPath(targetRoot)) { targetServerRoot = targetRoot; } else { final PathTranslation translation = WorkingFolder.translateLocalItemToServerItem(targetRoot, workspaceMappings); if (translation != null) { targetServerRoot = translation.getTranslatedPath(); isCloaked = translation.isCloaked(); } } if (isCloaked) { failureList.add(createItemCloakedFailure(targetRoot)); continue; } else if (targetServerRoot == null) { // Error is consistent with server workspaces when target path // is non-server and unmapped, but not cloaked failureList.add(createItemNotMappedFailure(targetRoot)); continue; } final String targetLocalRoot = ServerPath.isServerPath(targetRoot) ? WorkingFolder.getLocalItemForServerItem(targetRoot, workspaceMappings) : targetRoot; final int maxServerPathLength = workspace.getClient().getMaxServerPathLength(); // For consistency in error messages with server workspaces, check // the provided itemspec first, then the translated path. final AtomicReference<String> outTargetRoot = new AtomicReference<String>(targetRoot); ItemPath.checkItem(outTargetRoot, "changeRequest.ItemSpec.Item", //$NON-NLS-1$ false, false, false, true, maxServerPathLength); targetRoot = outTargetRoot.get(); if (ServerPath.isServerPath(targetRoot)) { if (targetLocalRoot != null && targetLocalRoot.length() > 0) { LocalPath.checkLocalItem(targetLocalRoot, "TargetItem", false, false, false, true); //$NON-NLS-1$ } } else { final AtomicReference<String> outTargetServerRoot = new AtomicReference<String>(targetServerRoot); ServerPath.checkServerItem(outTargetServerRoot, "TargetItem", //$NON-NLS-1$ false, false, false, true, maxServerPathLength); targetServerRoot = outTargetServerRoot.get(); } // server path minus all renames that exists on its parents final String targetUnrenamedServerRoot = pc.getCommittedServerItemForTargetServerItem(targetServerRoot); if (ItemPath.isWildcard(targetRoot)) { failureList.add( new Failure(Messages.getString("LocalDataAccessLayer.WildcardNotAllowedForRenameTarget"), //$NON-NLS-1$ FailureCodes.WILDCARD_NOT_ALLOWED_EXCEPTION, SeverityType.ERROR, targetRoot)); continue; } final ItemType targetItemType = changeRequests[i].getTargetItemType(); final boolean isMove = isMove(targetUnrenamedServerRoot, targetItemType, lv); final boolean targetIsFolder = targetItemType == ItemType.ANY ? isDirectory(lv, pc, targetServerRoot) : targetItemType == ItemType.FOLDER; // if we move dir\*.cs, root (dir will not be included), if we move // dir, root is included final boolean rootIncludedInRename = parsedItemSpec.getPattern() == null; if (parsedItemSpec.getPattern() != null && ItemPath.isWildcard(parsedItemSpec.getPattern()) && !isMove) { failureList.add( new Failure(Messages.getString("LocalDataAccessLayer.WildcardNotAllowedForRenameSource"), //$NON-NLS-1$ FailureCodes.WILDCARD_NOT_ALLOWED_EXCEPTION, SeverityType.ERROR, sourceRoot)); continue; } if (ServerPath.isRootFolder(targetServerRoot)) { failureList .add(new Failure(Messages.getString("LocalDataAccessLayer.CanNotChangeRootFolderException"), //$NON-NLS-1$ FailureCodes.CANNOT_CHANGE_ROOT_FOLDER_EXCEPTION, SeverityType.ERROR, parsedItemSpec.getTargetItem())); continue; } if (!targetIsFolder) { // we validate if target is a team project only if it doesn't // exist in local workspace - if it does, we will move files // under it final Failure teamProjectValidationFailure = TeamProject.validateChange(targetServerRoot, ItemType.FOLDER); if (teamProjectValidationFailure != null) { failureList.add(teamProjectValidationFailure); continue; } } for (final WorkspaceLocalItem lvEntry : parsedItemSpec.expandFrom(lv, pc, failureList)) { final String sourceCommittedServerItem = lvEntry.getServerItem(); String sourceCurrentServerItem = sourceCommittedServerItem; if (lvEntry.isCommitted()) { sourceCurrentServerItem = pc .getTargetServerItemForCommittedServerItem(sourceCommittedServerItem); } final String targetServerItem = calculateTargetServerItem(sourceServerRoot, sourceCurrentServerItem, targetServerRoot, targetIsFolder, rootIncludedInRename); // targetLocalItem may be null if we move file into not mapped // location final String targetLocalItem = WorkingFolder.getLocalItemForServerItem(targetServerItem, wp.getWorkingFolders()); final boolean caseOnlyRename = ServerPath.equals(sourceCurrentServerItem, targetServerItem) && !ServerPath.equals(sourceCurrentServerItem, targetServerItem, false); if (ServerPath.isRootFolder(sourceCurrentServerItem)) { failureList.add( new Failure(Messages.getString("LocalDataAccessLayer.CanNotChangeRootFolderException"), //$NON-NLS-1$ FailureCodes.CANNOT_CHANGE_ROOT_FOLDER_EXCEPTION, SeverityType.ERROR, sourceCurrentServerItem)); continue; } final Failure teamProjectValidationFailure = TeamProject.validateChange(sourceCommittedServerItem, lvEntry.isDirectory() ? ItemType.FOLDER : ItemType.FILE); if (teamProjectValidationFailure != null) { failureList.add(teamProjectValidationFailure); continue; } if (targetServerItem.length() > VersionControlConstants.MAX_SERVER_PATH_SIZE) { failureList.add(createPathTooLongFailure(targetServerItem)); continue; } // validate mappings boolean workspaceMappingFailure = false; for (final WorkingFolder mapping : workspaceMappings) { if (ServerPath.equals(mapping.getServerItem(), sourceCurrentServerItem)) { final String format = Messages .getString("LocalDataAccessLayer.RenameWorkingFolderExceptionFormat"); //$NON-NLS-1$ failureList.add(new Failure(MessageFormat.format(format, sourceCurrentServerItem), FailureCodes.RENAME_WORKING_FOLDER_EXCEPTION, SeverityType.ERROR, sourceCurrentServerItem)); workspaceMappingFailure = true; break; } if (ServerPath.isChild(sourceCurrentServerItem, mapping.getServerItem())) { return sendToServer(failures, onlineOperationRequired); } } if (workspaceMappingFailure) { continue; } if (!caseOnlyRename && pc.getByTargetServerItem(targetServerItem) != null) { final String format = Messages .getString("LocalDataAccessLayer.ChangeAlreadyPendingExceptionFormat"); //$NON-NLS-1$ failureList.add(new Failure(MessageFormat.format(format, targetServerItem), FailureCodes.CHANGE_ALREADY_PENDING_EXCEPTION, SeverityType.ERROR, sourceCurrentServerItem)); continue; } if (pc.getRecursiveChangeTypeForTargetServerItem(targetServerItem).contains(ChangeType.DELETE)) { failureList.add(createPendingParentDeleteFailure(targetServerItem)); continue; } LocalPendingChange mainPendingChange = pc.getByLocalVersion(lvEntry); if (null != mainPendingChange) { if (mainPendingChange.isLock()) { // Cannot offline rename a pending lock. return sendToServer(failures, onlineOperationRequired); } if (mainPendingChange.isDelete()) { final String format = Messages .getString("LocalDataAccessLayer.IncompatibleChangeExceptionFormat"); //$NON-NLS-1$ failureList.add(new Failure(MessageFormat.format(format, targetServerItem), FailureCodes.INCOMPATIBLE_CHANGE_EXCEPTION, SeverityType.ERROR, changeRequests[i].getItemSpec().getItem())); continue; } // target server item minus all parent renames // if we have both parent renamed (folder->folder2) and item // renamed(a->b), renaming folder2\a to folder2\b // should undo rename on bar final String targetParent = ServerPath.getParent(targetServerItem); final String committedParent = pc.getCommittedServerItemForTargetServerItem(targetParent); String targetUnrenamedParent = null; if (committedParent != null && committedParent.length() > 0) { targetUnrenamedParent = committedParent + targetServerItem.substring(targetParent.length()); } if ((targetUnrenamedParent != null && ServerPath.equals(targetUnrenamedParent, sourceCommittedServerItem, false)) || ServerPath.equals(sourceCommittedServerItem, targetServerItem, false)) { // Selective undo final AtomicReference<Failure[]> undoFailures = new AtomicReference<Failure[]>(); final List<LocalPendingChange> changes = new ArrayList<LocalPendingChange>(); changes.add(mainPendingChange); final GetOperation[] undoOps = undoPendingChanges(workspace, wp, lv, pc, changes, ChangeType.RENAME, undoFailures, onlineOperationRequired); if (onlineOperationRequired.get()) { return sendToServer(failures, onlineOperationRequired); } failureList.addAll(Arrays.asList(undoFailures.get())); getOps.addAll(Arrays.asList(undoOps)); continue; } } WorkspaceLocalItem conflictingItem = (targetLocalItem == null || targetLocalItem.length() == 0) ? null : lv.getByLocalItem(targetLocalItem); if (conflictingItem != null && !caseOnlyRename) { final String format = Messages.getString("LocalDataAccessLayer.ItemExistsExceptionFormat"); //$NON-NLS-1$ failureList.add(new Failure(MessageFormat.format(format, targetServerItem), FailureCodes.ITEM_EXISTS_EXCEPTION, SeverityType.ERROR, conflictingItem.getLocalItem())); continue; } final List<GetOperation> affectedItems = new ArrayList<GetOperation>(); final Map<String, LocalPendingChange> affectedCommittedPendingChanges = new HashMap<String, LocalPendingChange>(); final Map<String, LocalPendingChange> affectedNotCommittedPendingChanges = new HashMap<String, LocalPendingChange>(); // we should not updated pending changes in the main loop, since // we use them to calculate target path for each item // we need to do that afterwards final List<KeyValuePair<String, LocalPendingChange>> updatedPendingChanges = new ArrayList<KeyValuePair<String, LocalPendingChange>>(); // Create or update main pending change (on the item itself) if (mainPendingChange == null) { mainPendingChange = new LocalPendingChange(lvEntry, targetServerItem, ChangeType.RENAME); affectedCommittedPendingChanges.put(mainPendingChange.getCommittedServerItem(), mainPendingChange); } else { if (mainPendingChange.isAdd() || mainPendingChange.isBranch()) { affectedNotCommittedPendingChanges.put(mainPendingChange.getTargetServerItem(), mainPendingChange); } else { mainPendingChange .setChangeType(mainPendingChange.getChangeType().combine(ChangeType.RENAME)); affectedCommittedPendingChanges.put(mainPendingChange.getCommittedServerItem(), mainPendingChange); } } boolean abort = false; if (lvEntry.isDirectory()) { // build lookup of pending changes, so we can update them // and populate GetOps with the right change for (final LocalPendingChange lpEntry : pc.queryByTargetServerItem(sourceCurrentServerItem, RecursionType.FULL, "*")) //$NON-NLS-1$ { if (lpEntry.isAdd() || lpEntry.isBranch()) { affectedNotCommittedPendingChanges.put(lpEntry.getTargetServerItem(), lpEntry); } else { affectedCommittedPendingChanges.put(lpEntry.getCommittedServerItem(), lpEntry); } } } for (final WorkspaceLocalItem childEntry : ParsedItemSpec.queryLocalVersionsByTargetServerItem(lv, pc, sourceCurrentServerItem, RecursionType.FULL, null, ParsedItemSpecOptions.INCLUDE_DELETED)) { String childTargetServerItem; GetOperation childGetOp; LocalPendingChange associatedPendingChange = null; if (childEntry.isCommitted()) { associatedPendingChange = affectedCommittedPendingChanges.get(childEntry.getServerItem()); } else if (!childEntry.isCommitted()) { associatedPendingChange = affectedNotCommittedPendingChanges .get(childEntry.getServerItem()); } if (associatedPendingChange != null) { if (associatedPendingChange.isLock()) { // Cannot offline rename a pending lock. return sendToServer(failures, onlineOperationRequired); } if (!ServerPath.equals(targetServerItem, associatedPendingChange.getTargetServerItem(), false)) { // do not update if this is new pending change we // just created (mainPendingChange) childTargetServerItem = calculateTargetServerItem(sourceServerRoot, associatedPendingChange.getTargetServerItem(), targetServerRoot, targetIsFolder, rootIncludedInRename); } else { childTargetServerItem = associatedPendingChange.getTargetServerItem(); } updatedPendingChanges.add(new KeyValuePair<String, LocalPendingChange>( childTargetServerItem, associatedPendingChange)); childGetOp = childEntry.toGetOperation(associatedPendingChange, itemPropertyFilters); // SourceServerItem should be identical with // TargetServerItem for not committed changes (add and // branch) childGetOp.setSourceServerItem(childEntry.isCommitted() ? childGetOp.getSourceServerItem() : childTargetServerItem); childGetOp.setTargetServerItem(childTargetServerItem); childGetOp.setChangeType(childEntry.isCommitted() ? associatedPendingChange.getChangeType().combine(ChangeType.RENAME) : childGetOp.getChangeType()); } else { final String currentServerItem = childEntry.isCommitted() ? pc.getTargetServerItemForCommittedServerItem(childEntry.getServerItem()) : childEntry.getServerItem(); childTargetServerItem = calculateTargetServerItem(sourceServerRoot, currentServerItem, targetServerRoot, targetIsFolder, rootIncludedInRename); childGetOp = childEntry.toGetOperation(itemPropertyFilters); childGetOp.setTargetServerItem(childTargetServerItem); childGetOp.setChangeType(ChangeType.RENAME); } // TODO we include deletes only to add entry to // updatedPendingChanges and update them later. We should // check how costly it is when we rename folder that // contains big deletes subfolder if (childEntry.isDeleted()) { continue; } if (childTargetServerItem.length() > VersionControlConstants.MAX_SERVER_PATH_SIZE) { abort = true; failureList.add(createPathTooLongFailure(childTargetServerItem)); break; } final String childTargetLocalItem = WorkingFolder .getLocalItemForServerItem(childTargetServerItem, wp.getWorkingFolders()); conflictingItem = (childTargetLocalItem == null || childTargetLocalItem.length() == 0) ? null : lv.getByLocalItem(childTargetLocalItem); if (conflictingItem != null && !ServerPath.equals(conflictingItem.getServerItem(), childEntry.getServerItem())) { abort = true; final String format = Messages.getString("LocalDataAccessLayer.ItemExistsExceptionFormat"); //$NON-NLS-1$ failureList.add(new Failure(MessageFormat.format(format, targetServerItem), FailureCodes.ITEM_EXISTS_EXCEPTION, SeverityType.ERROR, conflictingItem.getLocalItem())); break; } if ((childTargetLocalItem == null || childTargetLocalItem.length() == 0) && childGetOp.getChangeType().contains(ChangeType.EDIT)) { abort = true; final String format = Messages .getString("LocalDataAccessLayer.TargetCloakedExceptionFormat"); //$NON-NLS-1$ failureList.add(new Failure(MessageFormat.format(format, sourceCurrentServerItem), FailureCodes.TARGET_CLOAKED_EXCEPTION, SeverityType.ERROR, sourceCurrentServerItem)); break; } if (silent) { // we generated all possible warnings, now bail since // it's a silent operation continue; } childGetOp.setTargetLocalItem(childTargetLocalItem); affectedItems.add(childGetOp); } if (abort) { continue; } // update pending changes for (final KeyValuePair<String, LocalPendingChange> updatedPendingChangeInfo : updatedPendingChanges) { final String updateServerPath = updatedPendingChangeInfo.getKey(); final LocalPendingChange updatedPendingChange = updatedPendingChangeInfo.getValue(); pc.remove(updatedPendingChange); updatedPendingChange.setTargetServerItem(updateServerPath); pc.pendChange(updatedPendingChange); } if (!silent) { // update local versions of not committed items (adds and // branches) for (final String originalServerItem : affectedNotCommittedPendingChanges.keySet()) { final LocalPendingChange pendingChange = affectedNotCommittedPendingChanges .get(originalServerItem); final WorkspaceLocalItem addEntry = lv.getByServerItem(originalServerItem, false); if (addEntry != null) { lv.removeByServerItem(originalServerItem, false, true); addEntry.setServerItem(pendingChange.getTargetServerItem()); // TODO what about renaming pending branch into not // mapped location? // TODO we already calculated this local path once, // we should store it in // affectedNotCommittedPendingChanges and reuse it addEntry.setLocalItem(WorkingFolder.getLocalItemForServerItem( pendingChange.getTargetServerItem(), wp.getWorkingFolders())); lv.add(addEntry); } } } getOps.addAll(affectedItems); } } for (final Failure failure : failureList) { failure.setRequestType(RequestType.RENAME); } onlineOperationRequired.set(false); failures.set(failureList.toArray(new Failure[failureList.size()])); return getOps.toArray(new GetOperation[getOps.size()]); }
From source file:org.apache.nifi.processors.standard.PostHTTP.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { final boolean sendAsFlowFile = context.getProperty(SEND_AS_FLOWFILE).asBoolean(); final int compressionLevel = context.getProperty(COMPRESSION_LEVEL).asInteger(); final String userAgent = context.getProperty(USER_AGENT).getValue(); final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout( context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setConnectTimeout( context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setRedirectsEnabled(false); requestConfigBuilder//from w w w . ja v a 2 s . c o m .setSocketTimeout(context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); final RequestConfig requestConfig = requestConfigBuilder.build(); final StreamThrottler throttler = throttlerRef.get(); final ComponentLog logger = getLogger(); final Double maxBatchBytes = context.getProperty(MAX_BATCH_SIZE).asDataSize(DataUnit.B); String lastUrl = null; long bytesToSend = 0L; final List<FlowFile> toSend = new ArrayList<>(); DestinationAccepts destinationAccepts = null; CloseableHttpClient client = null; final String transactionId = UUID.randomUUID().toString(); final AtomicReference<String> dnHolder = new AtomicReference<>("none"); while (true) { FlowFile flowFile = session.get(); if (flowFile == null) { break; } final String url = context.getProperty(URL).evaluateAttributeExpressions(flowFile).getValue(); try { new java.net.URL(url); } catch (final MalformedURLException e) { logger.error( "After substituting attribute values for {}, URL is {}; this is not a valid URL, so routing to failure", new Object[] { flowFile, url }); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); continue; } // If this FlowFile doesn't have the same url, throw it back on the queue and stop grabbing FlowFiles if (lastUrl != null && !lastUrl.equals(url)) { session.transfer(flowFile); break; } lastUrl = url; toSend.add(flowFile); if (client == null || destinationAccepts == null) { final Config config = getConfig(url, context); final HttpClientConnectionManager conMan = config.getConnectionManager(); final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(conMan); clientBuilder.setUserAgent(userAgent); clientBuilder.addInterceptorFirst(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException { final HttpCoreContext coreContext = HttpCoreContext.adapt(httpContext); final ManagedHttpClientConnection conn = coreContext .getConnection(ManagedHttpClientConnection.class); if (!conn.isOpen()) { return; } final SSLSession sslSession = conn.getSSLSession(); if (sslSession != null) { final Certificate[] certChain = sslSession.getPeerCertificates(); if (certChain == null || certChain.length == 0) { throw new SSLPeerUnverifiedException("No certificates found"); } try { final X509Certificate cert = CertificateUtils .convertAbstractX509Certificate(certChain[0]); dnHolder.set(cert.getSubjectDN().getName().trim()); } catch (CertificateException e) { final String msg = "Could not extract subject DN from SSL session peer certificate"; logger.warn(msg); throw new SSLPeerUnverifiedException(msg); } } } }); clientBuilder.disableAutomaticRetries(); clientBuilder.disableContentCompression(); final String username = context.getProperty(USERNAME).getValue(); final String password = context.getProperty(PASSWORD).getValue(); // set the credentials if appropriate if (username != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (password == null) { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username)); } else { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); } clientBuilder.setDefaultCredentialsProvider(credentialsProvider); } // Set the proxy if specified if (context.getProperty(PROXY_HOST).isSet() && context.getProperty(PROXY_PORT).isSet()) { final String host = context.getProperty(PROXY_HOST).getValue(); final int port = context.getProperty(PROXY_PORT).asInteger(); clientBuilder.setProxy(new HttpHost(host, port)); } client = clientBuilder.build(); // determine whether or not destination accepts flowfile/gzip destinationAccepts = config.getDestinationAccepts(); if (destinationAccepts == null) { try { destinationAccepts = getDestinationAcceptance(sendAsFlowFile, client, url, getLogger(), transactionId); config.setDestinationAccepts(destinationAccepts); } catch (final IOException e) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); logger.error( "Unable to communicate with destination {} to determine whether or not it can accept " + "flowfiles/gzip; routing {} to failure due to {}", new Object[] { url, flowFile, e }); context.yield(); return; } } } bytesToSend += flowFile.getSize(); if (bytesToSend > maxBatchBytes.longValue()) { break; } // if we are not sending as flowfile, or if the destination doesn't accept V3 or V2 (streaming) format, // then only use a single FlowFile if (!sendAsFlowFile || !destinationAccepts.isFlowFileV3Accepted() && !destinationAccepts.isFlowFileV2Accepted()) { break; } } if (toSend.isEmpty()) { return; } final String url = lastUrl; final HttpPost post = new HttpPost(url); final List<FlowFile> flowFileList = toSend; final DestinationAccepts accepts = destinationAccepts; final boolean isDestinationLegacyNiFi = accepts.getProtocolVersion() == null; final EntityTemplate entity = new EntityTemplate(new ContentProducer() { @Override public void writeTo(final OutputStream rawOut) throws IOException { final OutputStream throttled = throttler == null ? rawOut : throttler.newThrottledOutputStream(rawOut); OutputStream wrappedOut = new BufferedOutputStream(throttled); if (compressionLevel > 0 && accepts.isGzipAccepted()) { wrappedOut = new GZIPOutputStream(wrappedOut, compressionLevel); } try (final OutputStream out = wrappedOut) { for (final FlowFile flowFile : flowFileList) { session.read(flowFile, new InputStreamCallback() { @Override public void process(final InputStream rawIn) throws IOException { try (final InputStream in = new BufferedInputStream(rawIn)) { FlowFilePackager packager = null; if (!sendAsFlowFile) { packager = null; } else if (accepts.isFlowFileV3Accepted()) { packager = new FlowFilePackagerV3(); } else if (accepts.isFlowFileV2Accepted()) { packager = new FlowFilePackagerV2(); } else if (accepts.isFlowFileV1Accepted()) { packager = new FlowFilePackagerV1(); } // if none of the above conditions is met, we should never get here, because // we will have already verified that at least 1 of the FlowFile packaging // formats is acceptable if sending as FlowFile. if (packager == null) { StreamUtils.copy(in, out); } else { final Map<String, String> flowFileAttributes; if (isDestinationLegacyNiFi) { // Old versions of NiFi expect nf.file.name and nf.file.path to indicate filename & path; // in order to maintain backward compatibility, we copy the filename & path to those attribute keys. flowFileAttributes = new HashMap<>(flowFile.getAttributes()); flowFileAttributes.put("nf.file.name", flowFile.getAttribute(CoreAttributes.FILENAME.key())); flowFileAttributes.put("nf.file.path", flowFile.getAttribute(CoreAttributes.PATH.key())); } else { flowFileAttributes = flowFile.getAttributes(); } packager.packageFlowFile(in, out, flowFileAttributes, flowFile.getSize()); } } } }); } out.flush(); } } }) { @Override public long getContentLength() { if (compressionLevel == 0 && !sendAsFlowFile && !context.getProperty(CHUNKED_ENCODING).asBoolean()) { return toSend.get(0).getSize(); } else { return -1; } } }; if (context.getProperty(CHUNKED_ENCODING).isSet()) { entity.setChunked(context.getProperty(CHUNKED_ENCODING).asBoolean()); } post.setEntity(entity); post.setConfig(requestConfig); final String contentType; if (sendAsFlowFile) { if (accepts.isFlowFileV3Accepted()) { contentType = APPLICATION_FLOW_FILE_V3; } else if (accepts.isFlowFileV2Accepted()) { contentType = APPLICATION_FLOW_FILE_V2; } else if (accepts.isFlowFileV1Accepted()) { contentType = APPLICATION_FLOW_FILE_V1; } else { logger.error( "Cannot send data to {} because the destination does not accept FlowFiles and this processor is " + "configured to deliver FlowFiles; rolling back session", new Object[] { url }); session.rollback(); context.yield(); IOUtils.closeQuietly(client); return; } } else { final String contentTypeValue = context.getProperty(CONTENT_TYPE) .evaluateAttributeExpressions(toSend.get(0)).getValue(); contentType = StringUtils.isBlank(contentTypeValue) ? DEFAULT_CONTENT_TYPE : contentTypeValue; } final String attributeHeaderRegex = context.getProperty(ATTRIBUTES_AS_HEADERS_REGEX).getValue(); if (attributeHeaderRegex != null && !sendAsFlowFile && flowFileList.size() == 1) { final Pattern pattern = Pattern.compile(attributeHeaderRegex); final Map<String, String> attributes = flowFileList.get(0).getAttributes(); for (final Map.Entry<String, String> entry : attributes.entrySet()) { final String key = entry.getKey(); if (pattern.matcher(key).matches()) { post.setHeader(entry.getKey(), entry.getValue()); } } } post.setHeader(CONTENT_TYPE_HEADER, contentType); post.setHeader(FLOWFILE_CONFIRMATION_HEADER, "true"); post.setHeader(PROTOCOL_VERSION_HEADER, PROTOCOL_VERSION); post.setHeader(TRANSACTION_ID_HEADER, transactionId); if (compressionLevel > 0 && accepts.isGzipAccepted()) { if (sendAsFlowFile) { post.setHeader(GZIPPED_HEADER, "true"); } else { post.setHeader(CONTENT_ENCODING_HEADER, CONTENT_ENCODING_GZIP_VALUE); } } // Do the actual POST final String flowFileDescription = toSend.size() <= 10 ? toSend.toString() : toSend.size() + " FlowFiles"; final String uploadDataRate; final long uploadMillis; CloseableHttpResponse response = null; try { final StopWatch stopWatch = new StopWatch(true); response = client.execute(post); // consume input stream entirely, ignoring its contents. If we // don't do this, the Connection will not be returned to the pool EntityUtils.consume(response.getEntity()); stopWatch.stop(); uploadDataRate = stopWatch.calculateDataRate(bytesToSend); uploadMillis = stopWatch.getDuration(TimeUnit.MILLISECONDS); } catch (final IOException e) { logger.error("Failed to Post {} due to {}; transferring to failure", new Object[] { flowFileDescription, e }); context.yield(); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } finally { if (response != null) { try { response.close(); } catch (final IOException e) { getLogger().warn("Failed to close HTTP Response due to {}", new Object[] { e }); } } } // If we get a 'SEE OTHER' status code and an HTTP header that indicates that the intent // of the Location URI is a flowfile hold, we will store this holdUri. This prevents us // from posting to some other webservice and then attempting to delete some resource to which // we are redirected final int responseCode = response.getStatusLine().getStatusCode(); final String responseReason = response.getStatusLine().getReasonPhrase(); String holdUri = null; if (responseCode == HttpServletResponse.SC_SEE_OTHER) { final Header locationUriHeader = response.getFirstHeader(LOCATION_URI_INTENT_NAME); if (locationUriHeader != null) { if (LOCATION_URI_INTENT_VALUE.equals(locationUriHeader.getValue())) { final Header holdUriHeader = response.getFirstHeader(LOCATION_HEADER_NAME); if (holdUriHeader != null) { holdUri = holdUriHeader.getValue(); } } } if (holdUri == null) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error( "Failed to Post {} to {}: sent content and received status code {}:{} but no Hold URI", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } return; } } if (holdUri == null) { if (responseCode == HttpServletResponse.SC_SERVICE_UNAVAILABLE) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error( "Failed to Post {} to {}: response code was {}:{}; will yield processing, " + "since the destination is temporarily unavailable", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } context.yield(); return; } if (responseCode >= 300) { for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); logger.error("Failed to Post {} to {}: response code was {}:{}", new Object[] { flowFile, url, responseCode, responseReason }); session.transfer(flowFile, REL_FAILURE); } return; } logger.info("Successfully Posted {} to {} in {} at a rate of {}", new Object[] { flowFileDescription, url, FormatUtils.formatMinutesSeconds(uploadMillis, TimeUnit.MILLISECONDS), uploadDataRate }); for (final FlowFile flowFile : toSend) { session.getProvenanceReporter().send(flowFile, url, "Remote DN=" + dnHolder.get(), uploadMillis, true); session.transfer(flowFile, REL_SUCCESS); } return; } // // the response indicated a Hold URI; delete the Hold. // // determine the full URI of the Flow File's Hold; Unfortunately, the responses that are returned have // changed over the past, so we have to take into account a few different possibilities. String fullHoldUri = holdUri; if (holdUri.startsWith("/contentListener")) { // If the Hold URI that we get starts with /contentListener, it may not really be /contentListener, // as this really indicates that it should be whatever we posted to -- if posting directly to the // ListenHTTP component, it will be /contentListener, but if posting to a proxy/load balancer, we may // be posting to some other URL. fullHoldUri = url + holdUri.substring(16); } else if (holdUri.startsWith("/")) { // URL indicates the full path but not hostname or port; use the same hostname & port that we posted // to but use the full path indicated by the response. int firstSlash = url.indexOf("/", 8); if (firstSlash < 0) { firstSlash = url.length(); } final String beforeSlash = url.substring(0, firstSlash); fullHoldUri = beforeSlash + holdUri; } else if (!holdUri.startsWith("http")) { // Absolute URL fullHoldUri = url + (url.endsWith("/") ? "" : "/") + holdUri; } final HttpDelete delete = new HttpDelete(fullHoldUri); delete.setHeader(TRANSACTION_ID_HEADER, transactionId); while (true) { try { final HttpResponse holdResponse = client.execute(delete); EntityUtils.consume(holdResponse.getEntity()); final int holdStatusCode = holdResponse.getStatusLine().getStatusCode(); final String holdReason = holdResponse.getStatusLine().getReasonPhrase(); if (holdStatusCode >= 300) { logger.error( "Failed to delete Hold that destination placed on {}: got response code {}:{}; routing to failure", new Object[] { flowFileDescription, holdStatusCode, holdReason }); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } logger.info("Successfully Posted {} to {} in {} milliseconds at a rate of {}", new Object[] { flowFileDescription, url, uploadMillis, uploadDataRate }); for (final FlowFile flowFile : toSend) { session.getProvenanceReporter().send(flowFile, url); session.transfer(flowFile, REL_SUCCESS); } return; } catch (final IOException e) { logger.warn("Failed to delete Hold that destination placed on {} due to {}", new Object[] { flowFileDescription, e }); } if (!isScheduled()) { context.yield(); logger.warn( "Failed to delete Hold that destination placed on {}; Processor has been stopped so routing FlowFile(s) to failure", new Object[] { flowFileDescription }); for (FlowFile flowFile : toSend) { flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } return; } } }
From source file:org.apache.juddi.webconsole.hub.UddiHub.java
/** * Gets a list of all assertions for all businesses owned by the current * user/*from w w w .j a va 2s. c o m*/ * * The get_assertionStatusReport API call provides administrative * support for determining the status of current and outstanding * publisher assertions that involve any of the business registrations * managed by the individual publisher. Using this API, a publisher can * see the status of assertions that they have made, as well as see * assertions that others have made that involve businessEntity * structures controlled by the requesting publisher. See Appendix A * Relationships and Publisher Assertions for more information. * * @param msg * @return list */ public List<AssertionStatusItem> GetPublisherAssertions(AtomicReference<String> msg) { List<AssertionStatusItem> out = new ArrayList<AssertionStatusItem>(); if (GetToken() == null) { msg.set(ResourceLoader.GetResource(session, "errors.notsignedin")); return null; } List<AssertionStatusItem> STATUS_COMPLETE = null; try { try { STATUS_COMPLETE = publish.getAssertionStatusReport(GetToken(), CompletionStatus.STATUS_COMPLETE); } catch (Exception ex) { if (isExceptionExpiration(ex)) { token = null; STATUS_COMPLETE = publish.getAssertionStatusReport(GetToken(), CompletionStatus.STATUS_COMPLETE); } else { throw ex; } } } catch (Exception ex) { msg.set(HandleException(ex)); } if (STATUS_COMPLETE != null) { out.addAll(STATUS_COMPLETE); } List<AssertionStatusItem> STATUS_FROM_KEY_INCOMPLETE = null; try { try { STATUS_FROM_KEY_INCOMPLETE = publish.getAssertionStatusReport(GetToken(), CompletionStatus.STATUS_FROM_KEY_INCOMPLETE); } catch (Exception ex) { if (isExceptionExpiration(ex)) { token = null; STATUS_FROM_KEY_INCOMPLETE = publish.getAssertionStatusReport(GetToken(), CompletionStatus.STATUS_FROM_KEY_INCOMPLETE); } else { throw ex; } } } catch (Exception ex) { msg.set(HandleException(ex)); } if (STATUS_FROM_KEY_INCOMPLETE != null) { out.addAll(STATUS_FROM_KEY_INCOMPLETE); } List<AssertionStatusItem> STATUS_TO_KEY_INCOMPLETE = null; try { try { STATUS_TO_KEY_INCOMPLETE = publish.getAssertionStatusReport(GetToken(), CompletionStatus.STATUS_TO_KEY_INCOMPLETE); } catch (Exception ex) { if (isExceptionExpiration(ex)) { token = null; STATUS_TO_KEY_INCOMPLETE = publish.getAssertionStatusReport(GetToken(), CompletionStatus.STATUS_TO_KEY_INCOMPLETE); } else { throw ex; } } } catch (Exception ex) { msg.set(HandleException(ex)); } if (STATUS_TO_KEY_INCOMPLETE != null) { out.addAll(STATUS_TO_KEY_INCOMPLETE); } return out; //return publisherAssertions; }
From source file:org.eclipse.hono.adapter.http.AbstractVertxBasedHttpProtocolAdapter.java
/** * Opens a command receiver link for a device by creating a command and control consumer. * * @param ctx The routing context of the HTTP request. * @param tenant The tenant of the device for that a command may be received. * @param deviceId The id of the device for that a command may be received. * @param commandReceivedHandler Handler to be called after a command was received or the timer expired. * The link was closed at this time already. * If the timer expired, the passed command message is null, otherwise the received command message is passed. * * @return Optional An optional handler that cancels a timer that might have been started to close the receiver link again. *///w w w . jav a 2s.c o m private Future<Handler<Void>> openCommandReceiverLink(final RoutingContext ctx, final String tenant, final String deviceId, final Handler<Message> commandReceivedHandler) { final Future<Handler<Void>> resultWithLinkCloseHandler = Future.future(); final AtomicReference<MessageConsumer> messageConsumerRef = new AtomicReference<>(null); final Integer timeToDelayResponse = Optional.ofNullable(HttpUtils.getTimeTilDisconnect(ctx)).orElse(-1); if (timeToDelayResponse > 0) { // create a handler for being invoked if a command was received final Handler<Message> commandHandler = commandMessage -> { // if a link close handler was set, invoke it Optional.ofNullable(resultWithLinkCloseHandler.result()).map(linkCloseHandler -> { linkCloseHandler.handle(null); return null; }); // if desired, now invoke the passed commandReceivedHandler and pass the message Optional.ofNullable(commandReceivedHandler).map(h -> { h.handle(commandMessage); return null; }); final Optional<String> replyIdOpt = getReplyToIdFromCommand(tenant, deviceId, commandMessage); if (!replyIdOpt.isPresent()) { // from Java 9 on: switch to opt.ifPresentOrElse LOG.debug( "Received command without valid replyId for device [tenantId: {}, deviceId: {}] - no reply will be sent to the application", tenant, deviceId); } else { replyIdOpt.map(replyId -> { // send answer to caller via sender link final Future<CommandResponseSender> responseSender = createCommandResponseSender(tenant, deviceId, replyId); responseSender.compose(commandResponseSender -> commandResponseSender.sendCommandResponse( getCorrelationIdFromMessage(commandMessage), null, null, HttpURLConnection.HTTP_OK)) .map(delivery -> { LOG.debug("acknowledged command [message-id: {}]", commandMessage.getMessageId()); responseSender.result().close(v -> { }); return null; }).otherwise(t -> { LOG.debug("could not acknowledge command [message-id: {}]", commandMessage.getMessageId(), t); return Optional.ofNullable(responseSender.result()).map(r -> { r.close(v -> { }); return null; }).orElse(null); }); return replyId; }); } }; // create the commandMessageConsumer that handles an incoming command message final BiConsumer<ProtonDelivery, Message> commandMessageConsumer = createCommandMessageConsumer(ctx, tenant, deviceId, commandHandler); createCommandConsumer(tenant, deviceId, commandMessageConsumer, v -> onCloseCommandConsumer(tenant, deviceId, commandMessageConsumer)).map(messageConsumer -> { // remember message consumer for later usage messageConsumerRef.set(messageConsumer); // let only one command reach the adapter (may change in the future) messageConsumer.flow(1); // create a timer that is invoked if no command was received until timeToDelayResponse is expired final long timerId = getVertx().setTimer(timeToDelayResponse * 1000L, delay -> { closeCommandReceiverLink(messageConsumerRef); // command finished, invoke handler Optional.ofNullable(commandReceivedHandler).map(h -> { h.handle(null); return null; }); }); // define the cancel code as closure resultWithLinkCloseHandler.complete(v -> { getVertx().cancelTimer(timerId); closeCommandReceiverLink(messageConsumerRef); }); return messageConsumer; }).recover(t -> { closeCommandReceiverLink(messageConsumerRef); resultWithLinkCloseHandler.fail(t); return Future.failedFuture(t); }); } else { resultWithLinkCloseHandler.complete(); } return resultWithLinkCloseHandler; }
From source file:de.schildbach.pte.AbstractEfaProvider.java
protected SuggestLocationsResult xmlStopfinderRequest(final Location constraint) throws IOException { final HttpUrl.Builder url = stopFinderEndpoint.newBuilder(); appendStopfinderRequestParameters(url, constraint, "XML"); final AtomicReference<SuggestLocationsResult> result = new AtomicReference<>(); final HttpClient.Callback callback = new HttpClient.Callback() { @Override/* w ww . j a v a 2 s. co m*/ public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException { try { final XmlPullParser pp = parserFactory.newPullParser(); pp.setInput(body.byteStream(), null); // Read encoding from XML declaration final ResultHeader header = enterItdRequest(pp); final List<SuggestedLocation> locations = new ArrayList<>(); XmlPullUtil.enter(pp, "itdStopFinderRequest"); processItdOdv(pp, "sf", new ProcessItdOdvCallback() { @Override public void location(final String nameState, final Location location, final int matchQuality) { locations.add(new SuggestedLocation(location, matchQuality)); } }); XmlPullUtil.skipExit(pp, "itdStopFinderRequest"); result.set(new SuggestLocationsResult(header, locations)); } catch (final XmlPullParserException x) { throw new ParserException("cannot parse xml: " + bodyPeek, x); } } }; if (httpPost) httpClient.getInputStream(callback, url.build(), url.build().encodedQuery(), "application/x-www-form-urlencoded", httpReferer); else httpClient.getInputStream(callback, url.build(), httpReferer); return result.get(); }
From source file:org.apache.juddi.webconsole.hub.UddiHub.java
/** * This function returns all businesses that the current user * owns<br><br>/*from w w w .j a va2 s .c o m*/ * The get_registeredInfo API call is used to get an abbreviated list of * all businessEntity and tModel data that are controlled by a * publisher. When the registry distinguishes between publishers, this * is the individual associated with the credentials passed in the * authInfo element. This returned information is intended, for example, * for driving tools that display lists of registered information and * then provide drill-down features. This is the recommended API to use * after a network problem results in an unknown status of saved * information. * * @return RegisteredInfo */ public RegisteredInfo GetNodeInformation(AtomicReference<String> outmsg) { if (outmsg == null) { outmsg = new AtomicReference<String>(); } try { GetRegisteredInfo r = new GetRegisteredInfo(); r.setAuthInfo(GetToken()); if (r.getAuthInfo() == null) { outmsg.set(ResourceLoader.GetResource(session, "errors.notsignedin")); return null; } r.setInfoSelection(InfoSelection.ALL); RegisteredInfo registeredInfo = null; try { registeredInfo = publish.getRegisteredInfo(r); } catch (Exception ex) { if (isExceptionExpiration(ex)) { token = null; r.setAuthInfo(GetToken()); registeredInfo = publish.getRegisteredInfo(r); } else { throw ex; } } return registeredInfo; } catch (Exception ex) { outmsg.set(HandleException(ex)); } return null; }
From source file:com.ccserver.digital.service.LOSService.java
private ConfigureDocumentHandlingOperationCreditCardRequestType getRequestOfDoc(CreditCardApplication app, List<CreditCardApplicationDocumentThumbNailDTO> entities, String messageId, AtomicReference<Object> error) { ConfigureDocumentHandlingOperationCreditCardRequestType request = new ConfigureDocumentHandlingOperationCreditCardRequestType(); request.setHeader(getHeaderOfRequest(messageId)); com.vpbank.entity.vn.ba251.bd324.documentservice.configuredocumenthandlingoperationcreditcard._1.BodyRequestType bodyRequestType = new com.vpbank.entity.vn.ba251.bd324.documentservice.configuredocumenthandlingoperationcreditcard._1.BodyRequestType(); ApplicationInfoType applicationInfoType = new ApplicationInfoType(); // ApplicationId applicationInfoType.setApplicationId(app.getApplicationLOS().getLosApplicationId()); // Stage Name ApplicationStageType applicationStageType = new ApplicationStageType(); applicationStageType.setName(getConfiguration("LOS_SUBMISSION", "SUBMISSION")); applicationInfoType.getStage().add(applicationStageType); bodyRequestType.setApplicationInfo(applicationInfoType); List<DocumentInfoType> documentInfoTypes = bodyRequestType.getDocumentInfo(); Map<Long, Long> docByIds = new HashMap<Long, Long>(); entities.forEach(x -> addSubTypeContent(docByIds, x.getSubTypeId())); int totalSize = 0; for (Map.Entry<Long, Long> entry : docByIds.entrySet()) { Map<String, byte[]> docBySubType = new HashMap<String, byte[]>(); String documentTypeId = StringUtils.EMPTY; String documentTypeCode = StringUtils.EMPTY; int indexOfDoc = 1; for (CreditCardApplicationDocumentThumbNailDTO item : entities) { if (entry.getKey() == item.getSubTypeId()) { byte[] itemDocument = item.getDocument(); if (itemDocument != null) { docBySubType.put(String.valueOf(indexOfDoc + "_" + item.getFileName()), itemDocument); indexOfDoc++;/*from w ww . j a v a 2 s .com*/ documentTypeId = item.getDocumentTypeId(); documentTypeCode = item.getDocumentTypeCode(); } } } if (StringUtils.isEmpty(documentTypeId)) { continue; } DocumentInfoType documentInfoType = new DocumentInfoType(); try { byte[] contentZip = zipFileService.zipIt(docBySubType); documentInfoType.setContent(contentZip); // DocumentType documentInfoType.setDocumentType(documentTypeId); FileInfoType fileInfoType = new FileInfoType(); fileInfoType.setType(getConfiguration("LOS_DOCUMENT_TYPE", "copy")); // Size fileInfoType.setSize("" + contentZip.length); if (contentZip != null) { totalSize = totalSize + contentZip.length; } // File Name fileInfoType.setName(documentTypeCode + ".zip"); documentInfoType.setFileInfo(fileInfoType); documentInfoTypes.add(documentInfoType); } catch (IOException e) { logger.error(Utils.getErrorFormatLog("LOSService", "getRequestOfDoc", "", "Can not zip file", e.toString())); } } // int limitSize = Integer.parseInt(getConfiguration("TOTAL_FILE_SIZE_LIMIT", "104857600")); if (totalSize > limitSize) { error.set("OVER LIMIT"); } request.setBodyRequest(bodyRequestType); return request; }