Media playback is unsupported on your device Media caption David Miranda and Glenn Greenwald said they would appeal the ruling

The nine-hour detention at Heathrow Airport of an ex-Guardian journalist's partner has been ruled lawful.

David Miranda lives with reporter Glenn Greenwald who has written articles about state surveillance based on leaked documents.

At the High Court, Mr Miranda claimed his detention under anti-terrorism laws was unlawful and breached human rights.

But judges said it was a "proportionate measure in the circumstances" and in the interests of national security.

In his judgement, Lord Justice Laws, sitting with Mr Justice Ouseley and Mr Justice Openshaw, said: "Its objective was not only legitimate, but very pressing."

Whistleblower

Mr Miranda's lawyers said he had applied for permission to appeal against the decision.

The 28-year-old Brazilian was in transit from Germany to Brazil when he was stopped at the airport, detained, questioned and searched by police.

He was carrying computer files for Mr Greenwald at the time and had items, including his laptop, mobile phone, memory cards and DVDs, taken from him.

Mr Greenwald has written a series of stories about spying in the US and UK after receiving material from US whistleblower Edward Snowden, who has been given temporary asylum in Russia.

Image copyright Reuters Image caption Edward Snowden leaked information about state surveillance

Steven Kovats QC, representing the UK home secretary, previously told the High Court that the secret material seized from Mr Miranda could have ended up in the hands of al-Qaeda.

But Mr Miranda's lawyers argued the detention at Heathrow was illegal because it was carried out under the wrong law: Schedule 7 of the Terrorism Act 2000.

They said that in reality he was detained on the say-so of the security services so they could seize journalistic material.

Mr Miranda was carrying 58,000 highly classified Government Communications Headquarters (GCHQ) files, the judge said.

He added that Oliver Robbins, the UK's deputy national security adviser at the Cabinet Office, had stated that "release or compromise of such data would be likely to cause very great damage to security interests and possible loss of life".

What is Schedule 7? Image copyright PA Schedule 7 of the Terrorism Act 2000 allows UK police to stop, examine and search passengers at ports, airports and international rail terminals

Police officers do not need to have a "reasonable suspicion" that someone is involved in terrorism before they are stopped

A passenger can be held for questioning for up to nine hours and must give the examining officer any information requested Terror laws: What is Schedule 7?

In his ruling, Lord Justice Laws said: "The claimant was not a journalist; the stolen GCHQ intelligence material he was carrying was not 'journalistic material', or if it was, only in the weakest sense.

"But he was acting in support of Mr Greenwald's activities as a journalist. I accept that the Schedule 7 stop constituted an indirect interference with press freedom, though no such interference was asserted by the claimant at the time.

"In my judgement, however, it is shown by compelling evidence to have been justified."

Lord Justice Laws added that he had to balance press freedom against national security, and concluded: "On the facts of this case, the balance is plainly in favour of the latter."

Following the judgement, Mr Miranda said the ruling would hurt the UK more than him.

He said he did not know at the time what material he was carrying, but he knew it was for journalists.

"So if your government thinks that material for journalists is terrorism material now, I mean, there's no press freedom for your country," he said.

"We're going to appeal, we're going to appeal even though the courts say we cannot appeal we're going to try to appeal."

The High Court has refused Mr Miranda permission to take the case to the Court of Appeal but he can petition the court directly and ask them to hear the case.

'Very concerned'

Home Secretary Theresa May said the judgement "overwhelmingly supports the wholly proportionate action taken by the police in this case to protect national security".

"If the police believe any individual is in possession of highly-sensitive stolen information that would aid terrorism, then they should act. We are pleased that the court agrees," she said.

She added that the work of police and intelligence agencies was "made much harder as a result of intelligence leaks".

Metropolitan Police Deputy Assistant Commissioner Helen Ball described the ruling as "very important"

She told the BBC: "There was information that David Miranda was carrying stolen, highly classified documents that had been stolen by Edward Snowden.

"We were very concerned that if those documents reached the public domain people's safety would be put at risk."

'Abuse of power'

However, Mr Greenwald again criticised the UK government, adding that he would not be deterred "by this kind of thuggish behaviour".

He said: "I wouldn't want to be a journalist inside of the UK given how notorious that country is, especially now, for its contempt towards press freedom.

"But the rest of the world, I think, looks at this decision and views the UK government with a lot lower esteem. But it's not going to have any effect on anyone's journalism outside the UK and certainly not ours."

Mr Greenwald said he had been very careful with the material and kept it secure.

"There's no evidence that any of this information has ever helped terrorists in any way," he said.

Civil liberties and freedom of expression groups condemned the High Court decision.

Rosie Brighouse, legal officer at campaign group Liberty, said: "If such a barefaced abuse of power is lawful then the law must change.

"Miranda's treatment showed Schedule 7 for what it is - a chillingly over-broad power, routinely misused."