The US District judge, William Martini, of the District of New Jersey, dismissed the lawsuit brought by a group of Muslim residents in 2012, including a decorated Iraq war veteran, leaders of local mosques and small businesses, and university student organizations. The residents had argued that the NYPD – working across the Hudson River and outside its municipal borders – had singled them out simply because of the way they prayed, violating their First Amendment right to religious freedom and their 14th Amendment right to equal treatment under the law.

Judge Martini’s dismissal of their case highlighted, once again, America’s ongoing grappling with a clash of civic values – the “security versus liberty” conundrum in a post-9/11 world. But it also brought attention to the NYPD’s more-than-a-decade-long history of aggressive investigative tactics and to the politically charged question of how far the state may go in pursuing broad-based, blanket surveillance of a community to preempt possible, rather than actual, criminal and terrorist activities.

In addition to its contentious practice of stop-and-frisk, in which hundreds of thousands black and Latino men were targeted in police sweeps of high-crime areas, the Bloomberg administration’s NYPD also set up a special intelligence unit, directed by a retired CIA veteran. That unit became one of the nation’s most aggressive collectors of domestic intelligence, spanning out to surrounding states and even overseas.

“As we have always said, the NYPD's Intelligence division followed the law – and today's decision vindicates both the Department and the City,” said former Commissioner Kelly, in a statement on Thursday.

Since 2002, the NYPD intelligence division spied on dozens of New Jersey mosques, restaurants, and stores, as well as two Muslim grade schools and two Muslim student associations. The department used “rakers” and “crawlers” in Muslim neighborhoods, infiltrating religious and civic groups, as well as setting up video surveillance and mapping neighborhoods' ethnic makeups.

Martini dismissed the case on a technicality, however. The plaintiffs did not have proper “standing” to sue, because their injuries were caused not by the NYPD’s secret surveillance per se, but rather by a Pulitzer-prize winning exposé of their tactics by The Associated Press, he said.

“While this surveillance program may have had adverse effects upon the Muslim community after the Associated Press published its articles,” Martini wrote, “the motive for the program was not solely to discriminate against Muslims, but rather to find Muslim terrorists hiding among ordinary, law-abiding Muslims.”

The judge also rejected the claim that these New Jersey residents were illegally profiled simply because of their religion.

“Plaintiffs in this case have not alleged facts from which it can be plausibly inferred that they were targeted solely because of their religion,” he wrote. “The more likely explanation for the surveillance was a desire to locate budding terrorist conspiracies. The most obvious reason for so concluding is that surveillance of the Muslim community began just after the attacks of Sept. 11, 2001. The police could not have monitored New Jersey for Muslim terrorist activities without monitoring the Muslim community itself.”

AP's exposé of the NYPD’s spying on New Jersey Muslims, which was based on confidential internal documents, included a list of at least two dozen “ancestries of interest” to target likely Muslims. According to plaintiffs' attorneys, the program, after a decade of such surveillance, did not generate a single lead that uncovered a terrorist threat.

“By upholding the NYPD’s blunderbuss Muslim surveillance practices, the court’s decision gives legal sanction to the targeted discrimination of Muslims anywhere and everywhere in this country, without limitation, for no other reason than their religion,” said Baher Azmy, legal director of the Center for Constitutional Rights, in a statement.

The AP’s reports also sparked protests from New Jersey Gov. Chris Christie (R) and then-Newark Mayor Corey Booker (D). Moreover, it prompted a rare public rebuke from FBI officials in New Jersey, who said the NYPD's spying damaged public trust and created added risk for agencies gathering domestic intelligence about possible terrorism suspects.

Some plaintiffs in the case expressed frustration with Martini’s action.

“I have dedicated my career to serving my country, and this just feels like a slap in the face – all because of the way I pray,” said Farhaj Hassan, the US soldier who served in Iraq and joined the suit in 2012.

The case is Hassan v. City of New York, 2:12-cv-3401.

Another group of New York Muslims filed a similar lawsuit last June in federal district court in Manhattan. The case is pending.